dfs (hdu1175 Lianliankan)

Lianliankan

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


Problem Description
"Lianliankan" is believed to have been played by many people. It doesn't matter if you haven't played it before. Let me introduce the rules of the game to you: On a chessboard, a lot of pieces are placed. If two identical pieces can be connected by a line (the line cannot pass other pieces), and the number of turns of the line does not exceed two times, then the two pieces can be eliminated on the chessboard. I'm sorry, because I haven't played Lianliankan before and consulted my classmates, the connection cannot be bypassed from the outside, but in fact this is wrong. Now that it has caused a catastrophe, it can only be wrong, and the connection cannot be bypassed from the periphery.
The player clicks two pieces with the mouse successively, trying to eliminate them, and then the background of the game judges whether the two squares can be eliminated. Now your task is to write this daemon.
 

Input
There are multiple sets of input data. The first row of each set of data has two positive integers n, m (0<n<=1000, 0<m<1000), which represent the number of rows and columns of the chessboard respectively. In the next n lines, each line has m non-negative integers describing the grid distribution of the chessboard. 0 means there is no pawn in this position, and a positive integer indicates the type of pawn. The next line is a positive integer q (0<q<50), indicating that there are q queries below. In the next q lines, each line has four positive integers x1, y1, x2, y2, which means to ask whether the pawns in the x1 row, y1 column and the x2 row, y2 column can be eliminated. When n=0, m=0, the input ends.
Note: There is no precedence between inquiries, they are all for the current state!
 

Output
Each set of input data corresponds to a line of output. If it can be eliminated, output "YES", if not, output "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<iostream>
#include<cstring>
using namespace std;
int n,m;
int map[1005][1005];
bool fish [1005] [1005];
struct node
{
	int x;
	int y;
};
node a,b;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,1,-1};
bool ok(int x,int y)
{
	if(x>0&&x<=n&&y>0&&y<=m&&!vis[x][y]&&(map[x][y]==0||(map[x][y]!=0&&x==b.x&&y==b.y)))
	return true;
	else return false;
}
bool dfs(int x,int y,int dre,int num)//dre represents the current direction
{
	bool flag;
	vis[x][y]=true;
	if(num>2) return false;
	if(x==b.x&&y==b.y) return true;
	if(num==2&&x!=b.x&&y!=b.y) return false;
	for(int i=0;i<4;i++)
	{
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(ok(xx,yy))
		{
			if(dre==-1) flag=dfs(xx,yy,i,0);
			else if(dre==i) flag=dfs(xx,yy,dre,num);
			else flag=dfs(xx,yy,i,num+1);
			vis[xx][yy]=false;
			if(flag) return true;
		}
	}
	return false;
}
intmain()
{
	while(cin>>n>>m&&(n||m))
	{
		for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			cin>>map[i][j];
		}
		int k;
		cin>>k;
		for(int i=0;i<k;i++)
		{
			cin>>a.x>>a.y>>b.x>>b.y;
			if((a.x==b.x&&a.y==b.y)||map[a.x][a.y]==0||map[b.x][b.y]==0||map[a.x][a.y]!=map[b.x][b.y])
			{
				cout<<"NO"<<endl;
				continue;
			}
			memset(vis,0,sizeof(vis));
			bool flag=dfs(a.x,a.y,-1,0);
			if(flag) cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325945462&siteId=291194637