连连看
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26372 Accepted Submission(s): 6540
玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。
注意:询问之间无先后关系,都是针对当前状态的!
We have carefully selected several similar problems for you: 1180 1072 1016 1026 1240
解答:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
using namespace std;
const int N = 1005;
int _map[N][N];
int vis[N][N];
int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
int x1,y1,x2,y2,n,m,q;
int ok;
void Fun(int x,int y,int cnt,int a)
{
if(cnt > 2)return ;
if(ok)return ;
if(x==x2 && y==y2)
{
ok=1;
return ;
}
vis[x][y]=1;
for(int j=0; j<4; j++)
{
int nx=x+dir[j][0];
int ny=y+dir[j][1];
if(!vis[nx][ny]&&nx>0&&nx<=n&&ny>0&&ny<=m&&_map[nx][ny]==0)
{
if(a==j)
Fun(nx,ny,cnt,j);
else
Fun(nx,ny,cnt+1,j);
}
}
vis[x][y]=0;
}
int main()
{
while(1)
{
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",&_map[i][j]);
scanf("%d",&q);
for(int i=0; i<q; i++)
{
ok=0;
memset(vis,0,sizeof(vis));
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
if(_map[x1][y1]==_map[x2][y2]&&_map[x1][y1]!=0)
{
int t=_map[x2][y2];
_map[x2][y2]=0;
Fun(x1,y1,-1,-1);
_map[x2][y2]=t;
}
if(ok)printf("YES
");
else printf("NO
");
}
}
return 0;
}