Gym 100971J-Robots at Warehouse

题目链接:http://codeforces.com/gym/100971/problem/J

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
5 3

###

#1#

#.#

#2#

###
Output
NO
Input
3 5

#...#

#1.2#

#####
Output
YES
题意:简单的来说就是给我们一个n*m的地图,其中里面包括'#'表示不能走,还有'.'表示能走,还有有且仅有一个‘1’和一个‘2’分别表示两个机器人,而我们的任务就是判断是否可以让两个机器人交换位置,两个机器人是不能出现在同一个位置的,如果可以的话输出'YES',否则输出’NO‘。还有最最重要的一点就是所有的'.'都是连在一起的,可能很多人都没看到这个,好吧其实我开始也没看到,这也就表明1和2一定是互通的。可能很多人没看到都会去用BFS和DFS去搜索判断路径吧,这样就复杂多了。
解题思路:既然机器人1和机器人2一定是互通的,我们就只要判断他们是否可以互换了,而不是就只有一条单独宽度为1的道路,这样肯定是会相撞的没法交换的。然后怎么样才能交换呢?这里就可以分成两种的情况了,第一种就是有那种T字路口,最简单的T字路口当然就是直接一条单道旁边有一个空的位置了,只要有一个机器人挺在那个空位就可以实现位置交换了,而另一种情况就是有两条不一样的道路或者说就是个环吧,这样两个机器人分别走两条道就可以实现交换了。现在情况已经分析完了,我们要怎么转换成代码呢?我们知道所有'.'都是连通的,我们就将这些可以去的地方打上标记,定义一个数组标记一下,然后再进行枚举判断是否存在T字路口或者环,判断T字路口的话一般情况道路某个'.'周围应该是有两个'.'的,而如果存在第三个'.'则就可以认为是T字路口了。而判断是否有环的话,因为超过两个'.'的已经在T字路口中判断完了,所以只还有周围存在0、1或2个'.'的情况,因为是连通的 不存在周围0个的情况,而又要形成环的话,我们简单想想就知道应该周围只可以是两个'.'的,所以我们就直接判断道路中的'.'周围是否存在只有一个'.'的,有的话就不是环,没有的话就是了。
附上AC代码:
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<string.h>
 5 using namespace std;
 6 int n,m,x1,y1,x2,y2;
 7 bool flag=false;
 8 int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
 9 int main()
10 {
11     cin>>n>>m;
12     getchar();
13     char map[n+5][m+5];
14     int vis[n+5][m+5];
15     int num[n+5][m+5];
16     memset(num,0,sizeof(num));
17     memset(vis,0,sizeof(vis));
18     for(int i=1;i<=n;i++)
19     {
20         for(int j=1;j<=m;j++)
21         {
22             scanf("%c",&map[i][j]);
23             if(map[i][j]=='1')
24             {
25                 x1=i; y1=j;
26                 vis[x1][y1]=1;
27             }
28             if(map[i][j]=='2')
29             {
30                 x2=i; y2=j;
31                 vis[x2][y2]=1;
32             }
33             if(map[i][j]=='.')  vis[i][j]=1;
34         }
35         getchar();
36     }
37     //判断是否存在T字路口或者环(圈圈) 
38     int tot=0;
39     for(int i=1;i<=n;i++)
40         for(int j=1;j<=m;j++)
41         {
42             if(vis[i][j])
43             {
44                 if(vis[i][j])
45                 {
46                 for(int k=0;k<4;k++)
47                 {
48                     int dx=i+dir[k][0];
49                     int dy=j+dir[k][1];
50                     if(vis[dx][dy]) num[i][j]++;
51                 }
52                 if(num[i][j]>=3) flag=true;  //T字路口 
53                 if(num[i][j]==1) tot++;  //圈圈
54                 }
55             }
56         }
57     if(tot==0) flag=true;   //全部被标记的点周围只有两个点
58     if(flag) cout<<"YES"<<endl;
59     else cout<<"NO"<<endl;
60     return 0;
61 }
原文地址:https://www.cnblogs.com/zjl192628928/p/9326505.html