标签:
Description
Input
Output
Sample Input
Sample Output
#include<cstdio> #include<cstring> using namespace std; int n,m,p,ex,ey; int s[1005][1005]; int v[1005][1005]; int yi[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; void dfs(int x,int y,int k,int sx,int sy) { if (p==1||k==3) return ; for (int i=0;i<4;i++) { int rx=x+yi[i][0]; int ry=y+yi[i][1]; if (rx==ex&&ry==ey) { if (k<2) {p=1;return ;} if ((sx==yi[i][0]&&sy==yi[i][1])&&k==2) {p=1;return ;} } if (rx>=1&&rx<=n&&ry>=1&&ry<=m&&!v[rx][ry]&&!s[rx][ry]) { v[rx][ry]=1; if ((sx==0&&sy==0)||(sx==yi[i][0]&&sy==yi[i][1])) dfs(rx,ry,k,yi[i][0],yi[i][1]); else dfs(rx,ry,k+1,yi[i][0],yi[i][1]); v[rx][ry]=0; } } } int main() { int i,j,q,a,b,c,d; while (~scanf("%d%d",&n,&m)) { if (n==0&&m==0) break; for (i=1;i<=n;i++) for (j=1;j<=m;j++) scanf("%d",&s[i][j]); scanf("%d",&q); while (q--) { p=0; memset(v,0,sizeof(v)); scanf("%d%d%d%d",&a,&b,&c,&d); if (s[a][b]!=s[c][d]||s[a][b]==0) {printf("NO\n");continue;} ex=c;ey=d; dfs(a,b,0,0,0); if (p) printf("YES\n"); else printf("NO\n"); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/pblr/p/4696678.html