题目链接: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代码:
 #include<iostream>
#include<cstdio>
#include<string>
#include<string.h>
using namespace std;
int n,m,x1,y1,x2,y2;
bool flag=false;
int dir[][]={{,},{,-},{-,},{,}};
int main()
{
cin>>n>>m;
getchar();
char map[n+][m+];
int vis[n+][m+];
int num[n+][m+];
memset(num,,sizeof(num));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='')
{
x1=i; y1=j;
vis[x1][y1]=;
}
if(map[i][j]=='')
{
x2=i; y2=j;
vis[x2][y2]=;
}
if(map[i][j]=='.') vis[i][j]=;
}
getchar();
}
//判断是否存在T字路口或者环(圈圈)
int tot=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(vis[i][j])
{
if(vis[i][j])
{
for(int k=;k<;k++)
{
int dx=i+dir[k][];
int dy=j+dir[k][];
if(vis[dx][dy]) num[i][j]++;
}
if(num[i][j]>=) flag=true; //T字路口
if(num[i][j]==) tot++; //圈圈
}
}
}
if(tot==) flag=true; //全部被标记的点周围只有两个点
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return ;
}

最新文章

  1. 好玩的Handler
  2. DDD实施经验分享—价值导向、从上往下进行(圈内第一个吃螃蟹DDD实施方案)
  3. 翻滚吧,Spark (错误记录)
  4. ABP的工作单元
  5. Windows远程桌面连接Mac OS X
  6. 转Class.forName()用法详解
  7. awk处理之案例三:awk去掉不需要的文本行
  8. HD2025查找最大元素
  9. HDU5916
  10. PHP curl_setopt函数用法介绍
  11. Spring中报&quot;Could not resolve placeholder&quot;的解决方案
  12. mysql常用操作(一)
  13. PHP字符串截取函数
  14. [No0000133]c# ERROR:“.”(十六进制值 0x00)是无效的字符
  15. Oracle 常用函数备查
  16. 一些java的部署执行编译等命令
  17. navicat连接mysql出现1251错误
  18. loadrunner录制时,设置能不记录所有的事件
  19. git——分布式版本控制系统
  20. Java notify的使用

热门文章

  1. SAAS云平台搭建札记: (二) Linux Ubutu下.Net Core整套运行环境的搭建
  2. .Net架构篇:思考如何设计一款实用的分布式监控系统?
  3. 【知识整理】这可能是最好的RxJava 2.x 入门教程(一)
  4. php ajax登录注册
  5. 12.16 Daily Scrum
  6. github心得体会 王倩倩 201303014004 计科高职13-1
  7. ### The error may involve defaultParameterMap ### The error occurred while setting parameters
  8. vue路由异步组件案例
  9. CRM 数据查重
  10. SQLServer:介质簇计数 缺失的介质簇序列号