Gym - 100971J (思维+简单bfs)

题目链接:http://codeforces.com/gym/100971/problem/J
J. Robots at Warehouse
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output

Vitaly works at the warehouse. The warehouse can be represented as a grid of n × m cells, each of which either is free or is occupied by a container. From every free cell it's possible to reach every other free cell by moving only through the cells sharing a side. Besides that, there are two robots in the warehouse. The robots are located in different free cells.

Vitaly wants to swap the robots. Robots can move only through free cells sharing a side, moreover, they can't be in the same cell at the same time or move through each other. Find out if the swap can be done.

Input

The first line contains two positive integers n and m (2 ≤ n·m ≤ 200000) — the sizes of the warehouse.

Each of the next n lines contains m characters. The j-th character of the i-th line is «.» if the corresponding cell is free, «#» if there is a container on it, «1» if it's occupied by the first robot, and «2» if it's occupied by the second robot. The characters «1» and «2» appear exactly once in these lines.

Output

Output «YES» (without quotes) if the robots can be swapped, and «NO» (without quotes) if that can't be done.

Examples
input
Copy
5 3

###

#1#

#.#

#2#

###
output
NO
input
3 5

#...#

#1.2#

#####
output
YES
题意:给你一个n*m的图,“#”为墙,“。”是可以走的路图中有两个机器人1和2,问你是否可以通过通过1,和 2 的移动使得 1 和 2 的位置互换,1和2不能同时在一个位置。
可以输出YES,不能输出NO。
思路:刚看到这道题就想到用bfs做,但是不知道怎么通过bfs来判断它是否可以使他们的位置互换。然后通过画图分析才知道只要它满足几种情况中的一种就可以了

1.1,2有一条通路,且与1,2相通的所有点(包括1,2)中有一个点的四个方向有三个不是墙就可以了。

2.1,2有两条通路
代码:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct st{
	int x;
	int y;
};
queue<st> q;
int x[5]={0,1,0,-1};// 
int y[5]={1,0,-1,0};
char mp[200010];
int vt[200010];
int n,m,lg,lg1;
int x1,y1,x2,y2;
int check(int x,int y){//判断是否越界 
	if(x>0&&x<=n&&y>0&&y<=m)
	return true;
	return false;
}
int bfs(int x3,int y3){
	int i,j;
	st a,b;
	a.x=x3;
	a.y=y3;
	q.push(a);
	int sum;
	vt[(x3-1)*m+y3]=1;
	while(!q.empty()){
		a=q.front();
		q.pop();
		sum=0;
		//printf("%d %d ",a.x,a.y);
		for(i=0;i<4;i++){
			b.x=a.x+x[i];
			b.y=a.y+y[i];
			if(check(b.x,b.y)&&mp[(b.x-1)*m+b.y]!='#'){
				sum++;//计算a这个点的四周可以走的地方的数量 
				if(!vt[(b.x-1)*m+b.y])
				{
					q.push(b);
					vt[(b.x-1)*m+b.y]=1;
				}
			}
		}
		//printf("%d\n",sum);
/*3 6
#.##.#
.1..2.
#.##.#*/
		if(sum>=3)
		lg=1;
	}
	return 0;
}
int main(){
	
	int i,j;
	int sum1=0;
	lg=0;
	lg1=0;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++){
		getchar();
		char c;
		for(j=1;j<=m;j++)
		{
			scanf("%c",&c);
			if(c=='1')
			x1=i,y1=j;
			else if(c=='2')
			x2=i,y2=j;
			mp[(i-1)*m+j]=c;
		}	
	}
	for(i=0;i<4;i++)//从1点往四个方向bfs 
	{	
		if(check(x1+x[i],y1+y[i])&&(mp[(x1+x[i]-1)*m+y1+y[i]]!='#')){
			sum1++;		
			memset(vt,0,sizeof(vt));
			vt[(x1-1)*m+y1]=1;
			bfs(x1+x[i],y1+y[i]);
			if(vt[(x2-1)*m+y2])
			lg1++;//记录每个方向的是否可以到达2,不用记录一个方向一共有几条可以到2的路,因为如果它有多条一定至少有一个点三个方向是非墙的 
		}		
	}
	if(sum1>=3)
	lg=1;
	if((lg+lg1)>1) 
	printf("YES\n");
	else
	printf("NO\n");
	return 0;
}

  




posted @ 2018-07-18 14:07  cglong  阅读(181)  评论(0编辑  收藏  举报