Gym - 100971J (思维+简单bfs)
2.0 s
256 MB
standard input
standard output
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.
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 «YES» (without quotes) if the robots can be swapped, and «NO» (without quotes) if that can't be done.
5 3 ### #1# #.# #2# ###
NO
3 5 #...# #1.2# #####
YES
题意:给你一个n*m的图,“#”为墙,“。”是可以走的路图中有两个机器人1和2,问你是否可以通过通过1,和 2 的移动使得 1 和 2 的位置互换,1和2不能同时在一个位置。
可以输出YES,不能输出NO。
思路:刚看到这道题就想到用bfs做,但是不知道怎么通过bfs来判断它是否可以使他们的位置互换。然后通过画图分析才知道只要它满足几种情况中的一种就可以了 1.1,2有一条通路,且与1,2相通的所有点(包括1,2)中有一个点的四个方向有三个不是墙就可以了。 2.1,2有两条通路
代码:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct st{
int x;
int y;
};
queue<st> q;
int x[5]={0,1,0,-1};//
int y[5]={1,0,-1,0};
char mp[200010];
int vt[200010];
int n,m,lg,lg1;
int x1,y1,x2,y2;
int check(int x,int y){//判断是否越界
if(x>0&&x<=n&&y>0&&y<=m)
return true;
return false;
}
int bfs(int x3,int y3){
int i,j;
st a,b;
a.x=x3;
a.y=y3;
q.push(a);
int sum;
vt[(x3-1)*m+y3]=1;
while(!q.empty()){
a=q.front();
q.pop();
sum=0;
//printf("%d %d ",a.x,a.y);
for(i=0;i<4;i++){
b.x=a.x+x[i];
b.y=a.y+y[i];
if(check(b.x,b.y)&&mp[(b.x-1)*m+b.y]!='#'){
sum++;//计算a这个点的四周可以走的地方的数量
if(!vt[(b.x-1)*m+b.y])
{
q.push(b);
vt[(b.x-1)*m+b.y]=1;
}
}
}
//printf("%d\n",sum);
/*3 6
#.##.#
.1..2.
#.##.#*/
if(sum>=3)
lg=1;
}
return 0;
}
int main(){ int i,j;
int sum1=0;
lg=0;
lg1=0;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
getchar();
char c;
for(j=1;j<=m;j++)
{
scanf("%c",&c);
if(c=='1')
x1=i,y1=j;
else if(c=='2')
x2=i,y2=j;
mp[(i-1)*m+j]=c;
}
}
for(i=0;i<4;i++)//从1点往四个方向bfs
{
if(check(x1+x[i],y1+y[i])&&(mp[(x1+x[i]-1)*m+y1+y[i]]!='#')){
sum1++;
memset(vt,0,sizeof(vt));
vt[(x1-1)*m+y1]=1;
bfs(x1+x[i],y1+y[i]);
if(vt[(x2-1)*m+y2])
lg1++;//记录每个方向的是否可以到达2,不用记录一个方向一共有几条可以到2的路,因为如果它有多条一定至少有一个点三个方向是非墙的
}
}
if(sum1>=3)
lg=1;
if((lg+lg1)>1)
printf("YES\n");
else
printf("NO\n");
return 0;
}
Gym - 100971J (思维+简单bfs)的更多相关文章
- LightOJ 1012 简单bfs,水
1.LightOJ 1012 Guilty Prince 简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...
- POJ3185(简单BFS,主要做测试使用)
没事做水了一道POJ的简单BFS的题目 这道题的数据范围是20,所以状态总数就是(1<<20) 第一次提交使用STL的queue,并且是在队首判断是否达到终点,达到终点就退出,超时:(其实 ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- hdu1312 Red and Black 简单BFS
简单BFS模版题 不多说了..... 直接晒代码哦.... #include<cstdlib> #include<iostream> #include<cstdio> ...
- 逃脱 (简单BFS)
题目传送门 G逃脱 题目描述 这是mengxiang000和Tabris来到幼儿园的第四天,幼儿园老师在值班的时候突然发现幼儿园某处发生火灾,而且火势蔓延极快,老师在第一时间就发出了警报,位于幼儿园 ...
- Gym - 101572D Distinctive Character bfs 思维
题目传送门 题目大意: 给出n个01串,让你构造一个字符串,使这个字符串和这些字符串中相似程度最高 尽可能低.如果两个字符串对应位置相同,则相似程度加一. 思路: 每一个01串更改自己的一部分后,都可 ...
- Gym 100971A Treasure Island BFS 思维题
A - Treasure Island Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- Gym 101917 E 简单计算几何,I 最大流
题目链接 https://codeforces.com/gym/101917 E 题意:给定一个多边形(n个点),然后逆时针旋转A度,然后对多边形进行规约,每个点的x规约到[0,w]范围内,y规约到[ ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
随机推荐
- LeetCode--342--4的幂
问题描述: 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方. 示例 1: 输入: 16 输出: true 示例 2: 输入: 5 输出: false 进阶:你能不使用 ...
- LOJ6036编码
每个串拆成两个,都插入trie数. 把trie树建出来后,每一条从根到叶子的链上最多只能有一个变量为1. 这是个经典的前后缀优化2-sat建图的套路. 树上的做法也就是边dfs边做而已. #inclu ...
- 3月25 JavaScript 练习题
一个关于找7的题 <script type="text/javascript" language="javascript"> for(var i=1 ...
- 牛客练习赛32-D-MST+tarjin割边
链接:https://ac.nowcoder.com/acm/contest/272/D来源:牛客网 题目描述 小p和他的朋友约定好去游乐场游玩,但是他们到了游乐场后却互相找不到对方了. 游乐场可以看 ...
- hdu-6333-莫队
Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K ...
- C++ leetcode::two sum
上完C++的课就在也没用过C++了,最近要找实习,发现自己没有一门语言称得上是熟练,所以就重新开始学C++.记录自己从入门到放弃的过程,论C++如何逼死花季少女. 题目:Given an array ...
- maven聚合工程tomcat插件启动没有 Starting ProtocolHandler ["http-bio-8081"]
Starting ProtocolHandler ["http-bio-8081"]无法显示,一般有三个原因: (1)数据库连不上: (2)注册中心连不上(我这里用的是zookee ...
- SQL调优(SQL TUNING)并行查询提示(Hints)之pq_distribute的使用
pq_distribute提示通常被用于提升数据仓库中分区表间的连接操作性能. pq_distribute提示允许你确定参与连接的表数据行在生产和消费并行查询服务进程间如何分配. pq_distrib ...
- zookeeper 的心跳
假定:主机 A, B 通过 tcp 连接发送数据,如果拔掉 A 主机的网线,B 是无法感知到的.但是如果 A 定时给 B 发送心跳,则能根据心跳的回复来判断连接的状态. 以 zookeeper 为例: ...
- ActiveMQ 集群和主从
举例说明:假设有 3 个 broker 节点,分别是61616,61618, 61620,其中 61616 和 61618 组成主.从节点,而 61616(或61618)和 61620 构成集群.61 ...