题目来源
代码:
#include<bits/stdc++.h>
using namespace std;
struct node {
int pt;
int xp;
int yp;
};
node a[101][101];
int n,sx,sy,ex,ey,cnt,d[2][4]= {{0,1,0,-1},{-1,0,1,0}};
queue <node> q;
int main() {
scanf("%d",&n);
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
scanf("%d",&a[i][j].pt);
a[i][j].xp=i;
a[i][j].yp=j;
}
}
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
q.push(a[sx][sy]);
a[sx][sy].pt++;
while(!q.empty()) {
if(q.front().xp==a[ex][ey].xp&&q.front().yp==a[ex][ey].yp){
cout<<"YES"<<endl;
exit(0);
}
for(int i=0; i<4; i++) {
int fx=q.front().xp+d[0][i];
int fy=q.front().yp+d[1][i];
if(fx>0&&fx<=n&&fy>0&&fy<=n&&a[fx][fy].pt==0){
a[fx][fy].pt++;
q.push(a[fx][fy]);
}
}
q.pop();
}
cout<<"NO"<<endl;
return 0;
}