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 ...
随机推荐
- IntelliJ IDEA下自动生成Hibernate映射文件以及实体类
来自:https://blog.csdn.net/chenyunqiang/article/details/81026823 1.构建项目并添加项目结构配置以及配置初始参数 1.1.如图将基本的架子搭 ...
- 如何解决jersey框架中以json格式返回数组,当数组中元素一个时json格式不对
原文地址:http://www.cnblogs.com/swpk/p/3566536.html?utm_source=tuicool jersey 是oracle 出的一个较好的REST框架.使用此框 ...
- Python-习题-11
1,内容回顾列表:增 append insert extend 删 remove pop clear del 改 li[索引] = '被修改的内容' li[切片]:'被修改的内容' 查 for循环 r ...
- 四则运算coding
https://coding.net/u/ztf1641429293/p/sizeyunshuan/git/blob/master/Sizenyunsuan.java
- 团队作业Week14——源代码管理
0. 在吹牛之前,先回答这个问题: 如果你的团队来了一个新队员,有一台全新的机器, 你们是否有一个文档,只要设置了相应的权限,她就可以根据文档,从头开始搭建环境,并成功地把最新.最稳定版本的软件编译出 ...
- 《Linux内核设计与实现》 第一二章学习笔记
<Linux内核设计与实现> 第一二章学习笔记 第一章 Linux内核简介 1.1 Unix的历史 Unix的特点 Unix很简洁,所提供的系统调用都有很明确的设计目的. Unix中一切皆 ...
- 实验--使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用(杨光)
使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 攥写人:杨光 学号:20135233 ( *原创作品转载请注明出处*) ( 学习课程:<Linux内核分析>MOOC课程 ...
- debug网页时小问题The source attachment does not contain the source for the file
第一次debug总是出现下图问题,提示我没加源码... The source attachment does not contain the source for the file ... 解决方法: ...
- spring学习总结(一)_Ioc基础(中)
本篇文章继续上篇文章讲解Ioc基础,这篇文章主要介绍使用spring注解配置Ioc 上篇文章主要是通过xml配置文件进行Ioc的配置.这次进行改造下,通过注解进行配置 首先先看一个简单的demo 简单 ...
- python之itemgetter函数:对字典列表进行多键排序
itemgetter函数:对字典列表进行多键排序 from operator import itemgetter list_people = [ {'name': 'Mike', 'age': 22, ...