3 4 1 2 3 4 0 0 0 0 4 3 2 1 4 1 1 3 4 1 1 2 4 1 1 3 3 2 1 2 4 3 4 0 1 4 3 0 2 4 1 0 0 0 0 2 1 1 2 4 1 3 2 3 0 0
YES NO NO NO NO YES
前几天做这道题的时候主要是没理解题意。。wa了许多次。我以为拐弯是走在两个数的缝隙里。。。。
气人啊。。
今天才发现是走在数字上。而且只能走0.。也就是只能走在没有棋子的地方。
8642ms险过。。
#include <stdio.h> #include <string.h> int flag,st1,st2,ed1,ed2,m,n; int dir[4][2]={1,0,-1,0,0,1,0,-1},map[1005][1005],vis[1005][1005]; bool no(int x,int y) { if(x<0||y<0||x==m||y==n) return true; else return false; } void dfs(int x,int y,int cor,int cnt) { if(cnt>2||flag||x<0||x==m||y<0||y==n) return ; if(x==ed1&&y==ed2) { flag=1; return ; } if(map[x][y]!=0&&cor!=-1) return ; for(int i=0;i<4;i++) { int temp_x=x+dir[i][0]; int temp_y=y+dir[i][1]; if(vis[temp_x][temp_y]||no(temp_x,temp_y)) continue; vis[temp_x][temp_y]=1; if(cor!=i&&cor!=-1) cnt++; dfs(temp_x,temp_y,i,cnt); if(cor!=i&&cor!=-1) cnt--; vis[temp_x][temp_y]=0; } } int main() { while(scanf("%d %d",&m,&n)!=EOF) { if(m==0&&n==0) break; for(int i=0;i<m;i++) for(int j=0;j<n;j++) scanf("%d",&map[i][j]); int ncase; scanf("%d",&ncase); while(ncase--) { scanf("%d %d %d %d",&st1,&st2,&ed1,&ed2); st1--,st2--,ed1--,ed2--; if(st1<0||st2<0||ed1<0||ed2<0||st1>=m||ed1>=m||st2>=n||ed2>=n) { printf("NO\n"); continue; } if(map[st1][st2]!=map[ed1][ed2]||!map[st1][st2]||!map[ed1][ed2]) { printf("NO\n"); continue; } memset(vis,0,sizeof(vis)); flag=0; vis[st1][st2]=1; dfs(st1,st2,-1,0); if(flag) printf("YES\n"); else printf("NO\n"); } } return 0; }