Pagini recente » Cod sursa (job #2390841) | Cod sursa (job #1404727) | Cod sursa (job #1644419) | Cod sursa (job #1270488) | Cod sursa (job #715613)
Cod sursa(job #715613)
#include <fstream>
#include <queue>
#define nmax 305
using namespace std;
ifstream f("matrice2.in");
ofstream g("matrice2.out");
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int n, Q, a[nmax][nmax], viz[nmax][nmax];
queue<pair<int,int> > q;
void citeste(){
f >> n >> Q;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
f >> a[i][j];
}
}
}
int check(int x, int y){
if (x <= n && y <= n && x > 0 && y > 0) return 1;
return 0;
}
int drum(int H, int x1, int y1, int x2, int y2){//fac drumul de la x la y cu fiecare casuta avand costul >= H
if (a[x1][y1] < H) return 0;
q.push(make_pair(x1,y1));
for(; q.size(); ){
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int k=0; k<4; k++){
int i = x + dx[k];
int j = y + dy[k];
if (check(i,j) && a[i][j] >= H && viz[i][j] == 0){
viz[i][j] = 1;
q.push(make_pair(i,j));
}
}
}
for(; q.size(); q.pop());
if (viz[x2][y2]) return 1;
return 0;
}
void rezolva(){
for(int i=1; i<=Q; i++){
int x1, y1, x2, y2;
f >> x1 >> y1 >> x2 >> y2;
int st = 1;
int dr = a[x2][y2];
int rez = 0;
for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) viz[i][j] = 0;
while (st <= dr){
int mij = (st + dr) / 2;
if (drum(mij, x1, y1, x2, y2)){
rez = mij;
st = mij + 1;
}
else dr = mij-1;
}
g << rez << "\n";
}
}
int main(){
citeste();
rezolva();
f.close();
g.close();
}