连连看


Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25573 Accepted Submission(s): 6346



Problem Description


“连连看”相信很多人都玩过。没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子。如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去。不好意思,由于我以前没有玩过连连看,咨询了同学的意见,连线不能从外面绕过去的,但事实上这是错的。现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过。
玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。




Input


输入数据有多组。每组数据的第一行有两个正整数n,m(0<n<=1000,0<m<1000),分别表示棋盘的行数与列数。在接下来的n行中,每行有m个非负整数描述棋盘的方格分布。0表示这个位置没有棋子,正整数表示棋子的类型。接下来的一行是一个正整数q(0<q<50),表示下面有q次询问。在接下来的q行里,每行有四个正整数x1,y1,x2,y2,表示询问第x1行y1列的棋子与第x2行y2列的棋子能不能消去。n=0,m=0时,输入结束。
注意:询问之间无先后关系,都是针对当前状态的!




Output


每一组输入数据对应一行输出。如果能消去则输出"YES",不能则输出"NO"。




Sample Input


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




Sample Output


YES
NO
NO
NO
NO
YES


//好坑,判断条件太多了,写了一晚上,还是错的,明天再看


#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,flag;
int a[1010][1010];
int b[1010][1010];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ex,ey,sx,sy;
struct zz
{
	int x,y,turn;
}f1,f2;
void bfs(int x,int y)
{
	queue<zz>q;
	memset(b,0,sizeof(b));
	f1.x=x;f1.y=y;f1.turn=-1;
	q.push(f1);
	b[f1.x][f1.y]=1;
	while(!q.empty())
	{
		f1=q.front();
		q.pop();
		if(f1.turn>=2)
			continue;
		
		for(int i=0;i<4;i++)
		{
			f2.x=f1.x+dx[i];
			f2.y=f1.y+dy[i];
			f2.turn=f1.turn+1;
			if(f2.x>=1&&f2.x<=n&&f2.y>=1&&f2.y<=m&&(a[f2.x][f2.y]==0||(f2.x==ex&&f2.y==ey))&&f2.turn<=2)
			{
				if(f2.x==ex&&f2.y==ey&&f2.turn<=2)
				{
					flag=1;
					return ;
				}
				b[f2.x][f2.y]=1;
				q.push(f2);
				//b[f2.x][f2.y]=0;
			}
		}
	}
}
int main()
{
	int i,j,t;
	while(scanf("%d%d",&n,&m),n|m)
	{
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				scanf("%d",&a[i][j]);
			}
		}
		scanf("%d",&t);
		while(t--)
		{
			scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
			if((a[sx][sy]!=a[ex][ey])||a[sx][sy]==0||a[ex][ey]==0||(sx==ex&&sy==ey))
			{
				printf("NO\n");
				continue;
			}
			flag=0;
			bfs(ex,ey);
			if(flag)
				printf("YES\n");
			else
				printf("NO\n");
		}
	}
	return 0;
}



//这个是正确的,上面那个判断条件错误,而且没有真正统计拐弯次数。。。技术不行。。。见谅


<pre class="cpp" name="code">#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,flag;
int a[1010][1010];
int b[1010][1010];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ex,ey,sx,sy;
struct zz
{
	int x,y,turn;
}f1,f2;
int find(int x,int y)
{
	if(x<1||x>n||y<1||y>m)
		return 1;
	if(x==ex&&y==ey)//这块很重要 
		return 0;
	if(a[x][y]==0)
		return 0;
	return 1;
}
void bfs(int x,int y)
{
	queue<zz>q;
	f1.x=x;f1.y=y;f1.turn=-1;
	b[x][y]=1;
	q.push(f1);
	while(!q.empty())
	{
		f1=q.front();
		q.pop();
		if(f1.turn>=2)
			continue;		
		for(int i=0;i<4;i++)
		{
			f2.x=f1.x+dx[i];
			f2.y=f1.y+dy[i];
			f2.turn=f1.turn+1;//将一条路(一个方向)遍历完,i变化表示拐了一次弯,所以拐弯次数加1 
			if(find(f2.x,f2.y))//如果不满足继续遍历的条件,就重新开始遍历 
				continue;
			while(find(f2.x,f2.y)==0)//如果满足条件就将这条路遍历完 
			{
				if(f2.x==ex&&f2.y==ey)
				{
					flag=1;
					return ;
				}
				if(b[f2.x][f2.y]==0)
				{
					q.push(f2);
					b[f2.x][f2.y]=1;
				}
				f2.x+=dx[i];//一直将这条路走到头,不拐弯 
				f2.y+=dy[i];
			}
		}
	}
}
int main()
{
	int i,j,t;
	while(scanf("%d%d",&n,&m),n&&m)
	{
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				scanf("%d",&a[i][j]);
			}
		}
		scanf("%d",&t);
		while(t--)
		{
			flag=0;
			scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
			if((a[sx][sy]!=a[ex][ey])||a[sx][sy]==0||a[ex][ey]==0||(sx==ex&&sy==ey))//这些都是不符合条件的 
			{
				printf("NO\n");
				continue;
			}
			memset(b,0,sizeof(b));
			bfs(sx,sy);
			if(flag)
				printf("YES\n");
			else
				printf("NO\n");
		}
	}
	return 0;
}