“连连看”相信很多人都玩过。没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子。如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去。不好意思,由于我以前没有玩过连连看,咨询了同学的意见,连线不能从外面绕过去的,但事实上这是错的。现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过。
玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。
玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。
注意:询问之间无先后关系,都是针对当前状态的!
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#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; int n,m,a1,b1,a2,b2,flag; int a[1010][1010],v[1010][1010][4]; int t[4][2]= {1,0,-1,0,0,1,0,-1}; struct p { int x,y,z,dir; }; void bfs() { queue<p>Q; p now,tmp; now.x=a1; now.y=b1; //记录位置 now.z=0; //记录拐弯次数 now.dir=-1; //记录方向 Q.push(now); while(!Q.empty()) { now=Q.front(); Q.pop(); if(now.x==a2&&now.y==b2) { flag=1; return; } for(int i=0; i<4; i++) { int tx=now.x+t[i][0]; int ty=now.y+t[i][1]; tmp.z=now.z; //不需要拐弯 if(i!=now.dir&&now.dir!=-1) //需要拐弯 { tmp.z=now.z+1; if(tmp.z>2) //拐弯不能超过两次 continue; } if(tx>0&&tx<=n&&ty<=m&&ty>0&&(v[tx][ty][i]==0||v[tx][ty][i]>tmp.z)&&a[tx][ty]==0) //不超范围,走到此点此方向这种情况没有走过或者已经走过但是拐弯次数超过当前,并且没有棋子挡住,可以走 { tmp.dir=i; tmp.x=tx; tmp.y=ty; v[tx][ty][i]=tmp.z; //标记,到此地点此方向花了几步 Q.push(tmp); } } } return ; } int main() { int T; while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) scanf("%d",&a[i][j]); scanf("%d",&T); while(T--) { flag=0; scanf("%d%d%d%d",&a1,&b1,&a2,&b2); if(a1==a2&&b1==b2||a[a1][b1]!=a[a2][b2]||a[a1][b1]==0||a[a2][b2]==0) { printf("NO\n"); continue; } else { memset(v,0,sizeof(v)); int s=a[a2][b2]; a[a2][b2]=0; bfs(); if(flag==1) printf("YES\n"); else printf("NO\n"); a[a2][b2]=s; //变回来,每个询问之间相互独立 } } } return 0; }