Pagini recente » Cod sursa (job #952893) | Cod sursa (job #486531) | Rating Carl Adam (Carl896) | Clasament dupa rating | Cod sursa (job #1296776)
#include <fstream>
#include <queue>
using namespace std;
typedef pair <int,int> co;
ifstream f("matrice2.in");
ofstream g("matrice2.out");
int dl[]={-1,0,0,1},j,st,dr,best,m,dc[]={0,1,-1,0},n,q,a[301][301],i,x,y,pi,pf;
bool parcurgere(int m)
{
queue <co> q;
int z,xx,yy;
if (a[x][y]>=m) q.push(make_pair(x,y));
while(!q.empty())
{
xx=q.front().first;
yy=q.front().second;
if (xx==pi && yy==pf) return true;
q.pop();
for (z=0;z<=3;z++)
if (xx+dl[z]>=1 && yy+dc[z]>=1 && xx+dl[z]<=n && yy+dc[z]<=n)
if (a[xx+dl[z]][yy+dc[z]]>=m )
q.push(make_pair(xx+dl[z],yy+dc[z]));
}
return false;
}
int main()
{
f>>n>>q;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
f>>a[i][j];
for (i=1;i<=q;i++)
{
f>>x>>y>>pi>>pf;
st=1,dr=1000000;
while(st<=dr)
{
m=(st+dr)/2;
if (parcurgere(m)==true) best=m,st=m+1;
else dr=m-1;
}
g<<best<<'\n';
}
return 0;
}