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 ...
随机推荐
- js判断是对象还是集合
/*============================================ 函数功能:对返回数据中的列表数据进行非空处理 ============================== ...
- Oracle 10g RAC (linux) ASM 共享存储的管理详解
---------ASM 的管理(共享磁盘的管理)1.以 instance 的方式管理 ASM,启动 database 之前必须先启动 ASM instance,ASM instance 启动后,挂载 ...
- javascript系列-class3.循环语句
欢迎加入前端交流群来py: 转载请标明出处! while循环 while(循环终止条件){ } 案例 输出0~100个数字 1 . 3 . 5 不输出 输出一百数字 ...
- maven关于pom文件配置详解(转载)
转载:http://www.cnblogs.com/hafiz <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...
- 【原创】JMS生产者和消费者【PTP同步接收消息】
一般步骤: 请求一个JMS连接工i厂. 是用连接工厂创建连接. 启动JMS连接. 通过连接创建session. 获取一个目标. 创建一个生产者,或a.创建一个生产者,b.创建一条JMS消息并发送到目标 ...
- springmvc两种非注解的处理器适配器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...
- 洛谷P2045 方格取数加强版 最小费用流
Code: #include<cstdio> #include<cstring> #include<algorithm> #include<queue> ...
- WordPress 自动草稿和文章修定版本
写文章的时候发现 WordPress 有两个有意思的地方, WordPress 自动草稿和文章修定版本: 1.点击创建新文章的时候,会在数据库自动生成一条草稿数据: 2.修改数据的时候会将历史文章当做 ...
- [转载]vim常用命令总结
内容出处https://www.jianshu.com/p/a8ab13cff1ea 如有侵权请告知 移动.跳转 h.j.k.l:分别对应左.下.上.右.按键盘分布,从左到右,逆时针. w:移动到下一 ...
- 地图底图的类型——MapView,SceneView
MapView用于创建二维地图平面. 引用“esri/Map”,"esri/views/MapView" 具体做法并举例:var map = new Map({basemap:&q ...