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 ...
随机推荐
- poj_1952最大下降子序列,统计个数
其实不算难的一道题,但憋了我好久,嗯,很爽. #include<iostream> #include<cstdio> #include<string.h> #inc ...
- apiCloud中Frame框的操作,显示与隐藏Frame
Frame是一层一层的概念, 有的位于上层,有的位于下层. 1.加载菜单 2.加载页面层 3.首页拆分出内容层,这个时候内容层位于页面层的上方,当点击其他页面的时候,内容层遮挡住了他们 解决方案一 判 ...
- oracle 11g sql优化之行迁移处理(加大BLOCK块)
行链接 产生原因:当一行数据大于一个数据块,ORACLE会同时分配两个数据块,并在第一个块上登记第二个块的地址,从而形成行链接. 预防方法:针对表空间扩大数据块大小.检查:analyze table ...
- Charles抓取微信小程序数据 以及 其它应用网站数据
为了抓取小程序数据所以使用Charles来抓取,下面介绍下使用方法(mac环境下使用).使用Charles可以非常方便的抓取Http/Https请求.官方dmg下载地址:点击此处下载 Charles抓 ...
- Set集合[HashSet,TreeSet,LinkedHashSet],Map集合[HashMap,HashTable,TreeMap]
------------ Set ------------------- 有序: 根据添加元素顺序判定, 如果输出的结果和添加元素顺序是一样 无序: 根据添加元素顺序判定,如果输出的结果和添加元素的顺 ...
- 模拟post提交
模拟post提交 function post(URL, PARAMS) { var temp = document.createElement("form"); temp.acti ...
- 快速沃尔什变换(FWT)笔记
开头Orz hy,Orz yrx 部分转载自hy的博客 快速沃尔什变换,可以快速计算两个多项式的位运算卷积(即and,or和xor) 问题模型如下: 给出两个多项式$A(x)$,$B(x)$,求$C( ...
- Microsoft Visual Studio 2015打开TFS大量报错问题解决方案
用vs2015打开项目出现如图错误,尝试对XX项执行 添加 操作时遇到 XX 个错误,因为公司项目比较大,这个错误一直在弹,搜索了下,没有找到出现类似问题的解决方案. 因为这个路径是TFS(Team ...
- 紫书 例题 10-3 UVa 10375 (唯一分解定理)
这道题感觉非常的秀 因为结果会很大,所以就质因数分解分开来算 非常的巧妙! #include<cstdio> #include<vector> #include<cstr ...
- ASP.NET-缓存基本知识点
asp.net cache是一种缓存技术,然而,我们在asp.net程序中还可以使用其他的缓存技术,这些不同的缓存也各有所长.由于asp.net cache不能提供对外访问能力,因此,它不可能取代以m ...