题目链接: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 ;
}

Gym 100971J-Robots at Warehouse的更多相关文章

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

    题目链接:http://codeforces.com/gym/100971/problem/J J. Robots at Warehouse time limit per test 2.0 s mem ...

  2. 【Gym 100971J】Robots at Warehouse

    题意 链接给你一个n*m的地图,'#'代表墙,‘.’代表可走的,1代表1号机器人,2代表2号机器人,机器人可以上下左右移动到非墙的位置,但不能走到另一个机器人身上.问能否交换1和2的位置. 分析 如果 ...

  3. gym 100971 J Robots at Warehouse

    Vitaly works at the warehouse. The warehouse can be represented as a grid of n × m cells, each of wh ...

  4. Gym - 100971J ——DFS

    Statements Vitaly works at the warehouse. The warehouse can be represented as a grid of n × mcells, ...

  5. Gym 101915G Robots

    G. Robots time limit per test 5.0 s memory limit per test 256 MB input standard input output standar ...

  6. Gym 101915

    Gym - 101915A  Printing Books 题意:有一本书,从第X页开始,一共用了n位数字,求此书一共多少页.99就是两位数字,100就是三位数字. 思路:直接模拟即可,我用了一个hi ...

  7. 要back的题目 先立一个flag

    要back的题目 目标是全绿!back一题删一题! acmm7 1003 1004 acmm8 1003 1004 sysu20181013 Stat Origin Title Solved A Gy ...

  8. Robots Gym - 101915G

    传送门 The Robotics Olympiad teams were competing in a contest. There was a tree drawn on the floor, co ...

  9. poj2632 Crashing Robots

    Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9859   Accepted: 4209 D ...

随机推荐

  1. 关于LCA

    LCA:最近公共祖先 指在有根树中,找出某两个结点u和v最近的公共祖先 如图,5,7的最近公共祖先就是3 接下来,我们来了解如何求解LCA No.1 暴力 首先想到的肯定是暴力,我们搜索,从两个节点一 ...

  2. 浅谈nornalize.css(含源码)

    Normalize.css是一种CSS reset的替代方案.经过@necolas和@jon_neal花了几百个小时来努力研究不同浏览器的默认样式的差异,这个项目终于变成了现在这样. 我们创造norm ...

  3. 【nodejs】让nodejs像后端mvc框架(asp.net mvc)一样处理请求--参数自动映射篇(6/8)

    文章目录 前情概要 路由.action的扫描.发现.注册搞定之后,后来我发现在我们的action里面获取参数往往都是通过request对象来一个一个获取.同样的一行代码我们不厌其烦的重复写了无数次.遂 ...

  4. 利用 John the Ripper 破解用户登录密码

    一.什么是 John the Ripper ? 看到这个标题,想必大家都很好奇,John the Ripper 是个什么东西呢?如果直译其名字的话就是: John 的撕裂者(工具). 相比大家都会觉得 ...

  5. 第六周分析Linux内核创建一个新进程的过程

    潘恒 原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 task_struct结构: ...

  6. JProfiler的使用

    1.下载地址:http://www.ej-technologies.com/download/jprofiler/files 2.使用过程 1.点击此图的new Session 2.点击左边appli ...

  7. @ModelAttribute注解(SpringMVC)

    在方法定义上使用 @ModelAttribute 注解:Spring MVC 在调用目标处理方法前,会先逐个调用在方法级上标注了 @ModelAttribute 的方法. 在方法的入参前使用 @Mod ...

  8. Character Encoding Issues for tomcat

    https://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8 https://stackoverflow.com/questions/10936846 ...

  9. Windows Server 2008 双网卡 断网问题 总结

    实施现场的情况,一个网卡接得是聚合APN的子网,一个网卡是借得局域网. 运行一份数据收发程序,从APN网接入数据,发送给局域网,程序启动一会儿后就崩溃,此时测试网卡就Ping网关了,或者是时断时续,逐 ...

  10. Windows 安装补丁的另外一种方法

    Windows的补丁安装时经常出现异常提示: 如果安装不上的话 可以使用dism的方式来进行安装: 具体方法: 1. 将补丁包 一般为msu 或者是exe文件,改成rar后缀,并且解压缩,获取cab文 ...