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
5 3 ### #1# #.# #2# ###
NO
3 5 #...# #1.2# #####
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的更多相关文章
- Gym - 100971J (思维+简单bfs)
题目链接:http://codeforces.com/gym/100971/problem/J J. Robots at Warehouse time limit per test 2.0 s mem ...
- 【Gym 100971J】Robots at Warehouse
题意 链接给你一个n*m的地图,'#'代表墙,‘.’代表可走的,1代表1号机器人,2代表2号机器人,机器人可以上下左右移动到非墙的位置,但不能走到另一个机器人身上.问能否交换1和2的位置. 分析 如果 ...
- 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 ...
- Gym - 100971J ——DFS
Statements Vitaly works at the warehouse. The warehouse can be represented as a grid of n × mcells, ...
- Gym 101915G Robots
G. Robots time limit per test 5.0 s memory limit per test 256 MB input standard input output standar ...
- Gym 101915
Gym - 101915A Printing Books 题意:有一本书,从第X页开始,一共用了n位数字,求此书一共多少页.99就是两位数字,100就是三位数字. 思路:直接模拟即可,我用了一个hi ...
- 要back的题目 先立一个flag
要back的题目 目标是全绿!back一题删一题! acmm7 1003 1004 acmm8 1003 1004 sysu20181013 Stat Origin Title Solved A Gy ...
- Robots Gym - 101915G
传送门 The Robotics Olympiad teams were competing in a contest. There was a tree drawn on the floor, co ...
- poj2632 Crashing Robots
Crashing Robots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9859 Accepted: 4209 D ...
随机推荐
- Timer计时不准确的问题及解决方法
在项目中,需要每隔20ms发送一个RTP数据包.一开始使用的是System.Windows.Forms下的Timer类,但是发现明显延迟了.用StopWatch测了一下,发现它的触发间隔居然不是20m ...
- 词频统计 List Array
c# 使用数组进行词频统计 1.先考虑要是使用的数据结构: Array在在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也很简单,但是数组存在一些不足的地方.在数组的两个数据间插入数据 ...
- BugPhobia发布篇章:学霸在线系统测试报告
0x00 :测试报告版本管理 版本号 具体细节 修订时间 V 1.0 整理第一轮迭代用户管理和登陆注册的功能性验证测试,预计将继续网页对浏览器版本的兼容性测试 2015/11/12 V1.0.1 整理 ...
- JavaScript表单验证登录
在登录时,通常是将输入的信息原封不动的传送给后端的处理程序,然后处理之后返回结果,那么可能后端服务器的压力就很大,所以可以先在提交表单之前进行一些简单的检测,然后再发给后端,减小服务器的一部分压力: ...
- Windows10下手工强制清理删掉安装版的JRE8导致java.exe无法运行的解决办法
error:could not open xxxx.jvm.cfg 参考:https://blog.csdn.net/u010102493/article/details/18425267 1.搜索并 ...
- Classification Truth Table
在机器学习中对于分类结果的描述,一般有四种:true positive, true negative, false positive 和 false negative. Precision, Reca ...
- [cnbeta]华为值多少钱,全世界非上市公司中估值最高的巨头
华为值多少钱,全世界非上市公司中估值最高的巨头 https://www.cnbeta.com/articles/tech/808203.htm 小米.美团都曾表达过不想.不急于上市,但没人信,所以 ...
- bat脚本的写法
当你每次都要输入相同的命令时,可以把这么多命令存为一个批处理,从此以后,只要运行这个批处理,就相当于打了几行.几十行命令.下面以Nginx服务的停止脚本为例写一个bat批处理文件: 1.新建nginx ...
- 让 VAGRANT 启动并运行起来
这是一个帮助你快速入门Vagrant的初级教程.官方文档也可以很好的帮助你入门,但是本文更针对完全零基础的初学者并且会对某些问题直接切入正题. 本文在任何方面都不会取代官方文档,而且我建议读完本文的人 ...
- jquery ajax中data属性详解
$.post.$.get是一些简单的方法,如果要处理复杂的逻辑,还是需要用到jQuery.ajax() 一.$.ajax的一般格式 $.ajax({ type: 'POST', url: url , ...