Pagini recente » Cod sursa (job #2975894) | Cod sursa (job #2610199) | Cod sursa (job #2438098) | Cod sursa (job #1659974) | Cod sursa (job #1132760)
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
struct punct
{
int x;int y;
};
int a[309][309];
bool viz[309][309];
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
queue<punct> q;
int main()
{
int i,j,n,qu,k,rasp,xi,xf,yi,yf;
freopen("matrice2.in","r",stdin);
freopen("matrice2.out","w",stdout);
scanf("%d%d",&n,&qu);
for(i=1;i<=n;i++)
for(j=1;j<=n;++j)
scanf("%d",&a[i][j]);
for(k=1;k<=qu;++k)
{
scanf("%d%d%d%d",&xi,&yi,&xf,&yf);
int inc=0,sf=min(a[xi][yi],a[xf][yf]),med;
while(inc<sf-1)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
viz[i][j]=false;
med=(inc+sf)/2;
if(a[xi][yi]>=med)
{
while(q.empty()==0)
q.pop();
punct aux;
aux.x=xi;aux.y=yi;
q.push(aux);
viz[xi][yi]=true;
bool gasit=false;
while(q.empty()==0&&gasit==false)
{
int xc=q.front().x;
int yc=q.front().y;
if(xc==xf&&yc==yf)
{
gasit=true;
}
else
{
for(i=0;i<4;i++)
{
aux.x=xc+dx[i];
aux.y=yc+dy[i];
if(a[aux.x][aux.y]>=med&&viz[aux.x][aux.y]==false)
{
q.push(aux);
viz[aux.x][aux.y]=true;
}
}
}
q.pop();
}
if(gasit==true)
inc=med;
else
sf=med-1;
}
else
sf=med-1;
}
printf("%d\n",sf);
}
return 0;
}