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 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
bfs标记数组的应用
- #include <iostream>
- #include <algorithm>
- #include <cstring>
- #include <cstdio>
- #include <vector>
- #include <queue>
- #include <stack>
- #include <cstdlib>
- #include <iomanip>
- #include <cmath>
- #include <cassert>
- #include <ctime>
- #include <map>
- #include <set>
- using namespace std;
- #pragma comment(linker, "/stck:1024000000,1024000000")
- #pragma GCC diagnostic error "-std=c++11"
- #define lowbit(x) (x&(-x))
- #define max(x,y) (x>=y?x:y)
- #define min(x,y) (x<=y?x:y)
- #define MAX 100000000000000000
- #define MOD 1000000007
- #define pi acos(-1.0)
- #define ei exp(1)
- #define PI 3.1415926535897932384626433832
- #define ios() ios::sync_with_stdio(true)
- #define INF 0x3f3f3f3f
- #define mem(a) (memset(a,0,sizeof(a)))
- typedef long long ll;
- int dir[][]={{,},{,-},{-,},{,}};
- int main()
- {
- int n,m,pos_one_x,pos_one_y,pos_two_x,pos_two_y;
- scanf("%d%d",&n,&m);
- char ch[n+][m+];
- int vis[n+][m+],ans[n+][m+],tot=;
- memset(ans,,sizeof(ans));
- for(int i=;i<=n;i++)
- {
- scanf("%s",ch[i]+);
- for(int j=;j<=m;j++)
- {
- if(ch[i][j]=='') pos_one_x=i,pos_one_y=j;
- if(ch[i][j]=='') pos_two_x=i,pos_two_y=j;
- }
- }
- memset(vis,,sizeof(vis));
- queue<pair<int,int> >q;
- vis[pos_one_x][pos_one_y]=;
- q.push({pos_one_x,pos_one_y});
- while(!q.empty())
- {
- pair<int,int> p=q.front();
- q.pop();
- for(int i=;i<;i++)
- {
- int xx=p.first+dir[i][];
- int yy=p.second+dir[i][];
- if(xx> && xx<=n && yy> && yy<=m && !vis[xx][yy] && ch[xx][yy]!='#')
- {
- vis[xx][yy]=;
- q.push({xx,yy});
- }
- }
- }
- if(!vis[pos_two_x][pos_two_y]) return *printf("NO\n");
- bool flag=false;
- for(int i=;i<=n;i++)
- for(int j=;j<=m;j++)
- {
- if(vis[i][j])
- {
- if(vis[i-][j]) ans[i][j]++;
- if(vis[i+][j]) ans[i][j]++;
- if(vis[i][j-]) ans[i][j]++;
- if(vis[i][j+]) ans[i][j]++;
- if(ans[i][j]>=) flag=true;
- if(ans[i][j]==) tot++;
- }
- }
- if(tot!=) flag=true;
- puts(flag?"YES":"NO");
- return ;
- }
gym 100971 J Robots at Warehouse的更多相关文章
- 【Gym 100971J】Robots at Warehouse
题意 链接给你一个n*m的地图,'#'代表墙,‘.’代表可走的,1代表1号机器人,2代表2号机器人,机器人可以上下左右移动到非墙的位置,但不能走到另一个机器人身上.问能否交换1和2的位置. 分析 如果 ...
- codeforces gym 100971 K Palindromization 思路
题目链接:http://codeforces.com/gym/100971/problem/K K. Palindromization time limit per test 2.0 s memory ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
- 【codeforces.com/gym/100240 J】
http://codeforces.com/gym/100240 J [分析] 这题我搞了好久才搞出样例的11.76....[期望没学好 然后好不容易弄成分数形式.然后我‘+’没打..[于是爆0... ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- codeforces Gym 100500 J. Bye Bye Russia
Problem J. Bye Bye RussiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
随机推荐
- 修改YOLO使其显示自定义类别
基本参考自这篇文章(http://blog.csdn.net/ma3252788/article/details/74659230),主要用来记录下自己遇到的问题 根据@赤战约风 的帖子做如下修改可以 ...
- Recovering unassigned shards on elasticsearch 2.x——副本shard可以设置replica为0在设置回来
Recovering unassigned shards on elasticsearch 2.x 摘自:https://z0z0.me/recovering-unassigned-shards-on ...
- ServiceStack.Redis之IRedisClient<第三篇>【转】
事实上,IRedisClient里面的很多方法,其实就是Redis的命令名.只要对Redis的命令熟悉一点就能够非常快速地理解和掌握这些方法,趁着现在对Redis不是特别了解,我也对着命令来了解一下这 ...
- HTTP报文头解析
HTTP报文头解析 本篇博客我们就来详细的聊一下HTTP协议的常用头部字段,当然我们将其分为请求头和响应头进行阐述.下方是报文头每个字段的格式,首先是头部字段的名称,如Accept,冒号后方紧跟的是该 ...
- JS循环 for while 全局/局部变量 短路
循环语句: For for循环的格式 for(var i = 0; i < 10; i ++){ } for循环的执行顺序: ① ② 若判断为 true 进④ 进③ 进②判断 ……循环 ...
- (转载)Android中的Service:Binder,Messenger,AIDL(2)
前言 前面一篇博文介绍了关于Service的一些基本知识,包括service是什么,怎么创建一个service,创建了一个service之后如何启动它等等.在这一篇博文里有一些需要前一篇铺垫的东西,建 ...
- intellij idea 15 for mac破解版(含注册码)下载
转载 https://blog.csdn.net/dataiyangu/article/details/81163118
- python内置的一些模块
logging模块: 默认情况下,logging将日志打印到屏幕,日志级别为WARNING:日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO & ...
- Python 计算相似度
#计算相似度 #欧式距离 # npvec1, npvec2 = np.array(det_a), np.array(det_b) # similirity=math.sqrt(((npvec1 - n ...
- PL SQL Developer使用总结
如果OS为windows 7 64位系统,Oracle版本为 Oracle 11g 64 安装PL SQL Developer 请参考 http://myskynet.blog.51cto.co ...