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 ...
随机推荐
- xml解析 使用dom4j操作xml
使用dom4j操作xml 1 导入 dom4j,的jar包 2 指定要解析的XML文件 SAXReader sr=new SAXReader(); Document document= sr.r ...
- 【JVM.12】线程安全与锁优化
一.概述 面向过程的编程思想极大地提升了现代软件开发的生产效率和软件可以达到的规模,但是现实世界与计算机世界之间不可避免地存在一些差异,本节就如何保证并发的正确性和如何实现线程安全讲起. 二.线程安全 ...
- Webpack 2 视频教程 002 - NodeJS 安装与配置
原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...
- BGFX 渲染引擎中着色器代码的调试方法
在实时渲染的图形开发中,着色器代码(Shader)越来越复杂,于是单纯的靠经验和不断试错的开发和调试方法早已不能满足实际需求.使用调试工具进行调试,成为开发中重要的方法.Bgfx 是一款跨平台.抽象封 ...
- MariaDB 安装与启动 过程记录
1. 安装之前的准备工作 rpm -qa |grep mysql rpm -qa |grep mariadb 按照查出来的软件包使用 yum remove 全部卸载,当然也可以 yum remov ...
- Linux内核第二节
作者:武西垚 深入理解函数调用堆栈 堆栈是C语言程序运行时必须的一个记录调用路径和参数的空间 堆栈的作用 函数调用框架 传递参数 保存返回地址 提供局部变量空间 堆栈相关的寄存器 esp,堆栈指针,指 ...
- sho
手工编程:hello world 全部用命令行工具和Notepad编辑器,用手工创建并编译一个C的命令行程序:hello world. public class Hello{ publ ...
- 第三次Sprint
Not CHECKED OUT CHECKED OUT DONE!: SPRINT GOAL: BETA-READY 修改bug 完善界面
- Hibernate_core_method
/** * Created by Administrator on 2015/11/30. *HibernateUtil */public class HibernateUtil { private ...
- Golang 入门~~基础知识
变量声明 //通用形式,指定变量名,变量类型,变量值 var name int = 99 fmt.Println(name) //指定变量名,以及变量类型,未指定值的时候默认是类型零值 var age ...