Vitaly works at the warehouse. The warehouse can be represented as a grid of n × mcells, 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.

Example

Input
5 3

####1##.##2####

Output
NO

Input
3 5

#...##1.2######

Output
YES题意:1和2要进行位置的交换,问你是否能够成功?思路:如果在1能够到达2,并且他们之间的存在有一个点能够连接三个方向,或者只有一个方向的点的个数不是2个,则是YES,否则是NO。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <stack>
  5 #include <vector>
  6 #include <algorithm>
  7 #include<queue>
  8 using namespace std;
  9 const int maxn=200005;
 10 string s[maxn];
 11 typedef pair<int,int>P;
 12 P p;
 13 queue<P>que;
 14 vector<int>vis[maxn];
 15 int n,m,sx,sy,ex,ey;
 16 int a[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
 17 void bfs()
 18 {
 19     for(int i=0; i<n; i++)
 20     {
 21         vis[i].clear();
 22         for(int j=0; j<m; j++)
 23             vis[i].push_back(0);
 24     }
 25     while(!que.empty())
 26         que.pop();
 27     vis[sx][sy]=1;
 28     p.first=sx,p.second=sy;
 29     que.push(p);
 30     int flag=0;
 31     while(!que.empty())
 32     {
 33         p=que.front();
 34         que.pop();
 35         int tx=p.first;
 36         int ty=p.second;
 37         for(int i=0; i<4; i++)
 38         {
 39             int xx=tx+a[i][0],yy=ty+a[i][1];
 40             if(!(0<=xx&&xx<n&&0<=yy&&yy<m)||s[xx][yy]=='#'||vis[xx][yy])
 41                 continue;
 42             vis[xx][yy]=1;
 43             p.first=xx;
 44             p.second=yy;
 45             que.push(p);
 46         }
 47     }
 48 }
 49 int main()
 50 {
 51     while(~scanf("%d%d",&n,&m))
 52     {
 53         for(int i=0; i<n; i++)
 54         {
 55             cin>>s[i];
 56             for(int j=0; j<m; j++)
 57             {
 58                 if(s[i][j]=='1')
 59                 {
 60                     sx=i,sy=j;
 61                 }
 62                 else if(s[i][j]=='2')
 63                 {
 64                     ex=i,ey=j;
 65                 }
 66             }
 67         }
 68         bfs();
 69         if(!vis[ex][ey])
 70         {
 71             printf("NO\n");
 72             continue;
 73         }
 74         int flag=0,ans=0,cnt;
 75         for(int i=0;i<n;i++)
 76         {
 77             for(int j=0;j<m;j++)
 78             {
 79                 if(flag)
 80                     break;
 81                 cnt=0;
 82                 if(vis[i][j])
 83                 {
 84                     for(int k=0;k<4;k++)
 85                     {
 86                         if(!(0<=i+a[k][0]&&i+a[k][0]<n&&0<=j+a[k][1]&&j+a[k][1]<m)||s[i+a[k][0]][j+a[k][1]]=='#')
 87                             continue;
 88                         if(vis[i+a[k][0]][j+a[k][1]])
 89                             cnt++;
 90                     }
 91                     if(cnt>2)
 92                     {
 93                         flag=1;
 94                         printf("YES\n");
 95                         break;
 96                     }
 97                     if(cnt==1) ans++;
 98                 }
 99             }
100             if(flag)
101                 break;
102         }
103         if(flag==0)
104         {
105             if(ans==2) printf("NO\n");
106             else printf("YES\n");
107         }
108     }
109     return 0;
110 }

View Code

知识点:

由于n,m<=200000,所以无法定义flag数组进行标记。使用vector容器能够很好的解决这一问题。

转载于:https://www.cnblogs.com/wang-ya-wei/p/6894941.html

Robots at Warehouse(搜索+vector的使用)相关推荐

  1. PTA:7-102 喊山 (30分)---解析(bfs广度优先搜索,vector)

    7-102 喊山 (30分) 喊山,是人双手围在嘴边成喇叭状,对着远方高山发出"喂-喂喂-喂喂喂--"的呼唤.呼唤声通过空气的传递,回荡于深谷之间,传送到人们耳中,发出约定俗成的& ...

  2. 百度/Google等搜索引擎的信息检索搜索技巧总结

    搜索引擎工作原理 爬虫.索引.检索.排序 爬虫协议:/robots.txt 准确搜索 给关键词加上英文双引号 "deformable convolution" 排除关键词 空格加减 ...

  3. 字典树/Trie/前缀树-LeetCode总结:720词典中最长的单词;127. 单词接龙;677. 键值映射;面试题 17.17. 多次搜索;648. 单词替换

    MyTrie结构体和相关操作函数 typedef struct MyTrie {bool is_word;vector<MyTrie*> next;MyTrie():is_word(fal ...

  4. 渗透测试之Google搜索语法

    目录 intitle allintitle inurl  allinurl intext allintext filetype site link index of info define 通配符 i ...

  5. TSP问题禁忌搜索c++实现

    今天作业汇报完,直接开源! 1.数据准备: 1.1随机初始化版: 初始化城市数量以及维度: pair<int, int> initial::read_private_i() {cout & ...

  6. 4.html 头部随笔

    1.html头部标记 <base>:当前文档的URL全称(基底地址) <basefont>:设定基准的文字字体.字号和颜色 <title>:网页标题 <isi ...

  7. web安全之信息刺探防范(上)

    #----------------------------------------------------------#  # ====> 红色字体 -特指煮酒个人所见.加粗则为需要重点注意.  ...

  8. java基础系列:集合入门

    集合框架: Java中的集合框架大类可分为Collection和Map:两者的区别: Collection是单列集合:Map是双列集合 Collection中只有Set系列要求元素唯一:Map中键需要 ...

  9. 【CyberSecurityLearning 70】DC系列之DC-1渗透测试(Drupal)

    目录 DC-1 靶机渗透测试实战 背景资料: 知识点 一.实验环境 二.实验要求 三.渗透过程演示 Flag1: Flag2: Flag3: Flag4: Flag5: DC-1 靶机渗透测试实战 背 ...

最新文章

  1. 按下开机键,计算机背后的故事
  2. memcached ---- 学习笔记
  3. 彻底解决 intellij IDEA 卡顿 优化笔记
  4. 雨课堂显示服务器无法连接,雨课堂用的什么云服务器
  5. java时间提醒微服务器_springcloud中微服务的优雅停机(已验证)
  6. python int转str_用Python生成抖音字符视频!
  7. 苦口之药的拼音及解释
  8. ssis 列转换_SSIS包中的行采样转换和百分比采样转换
  9. Oracle RAC Brain Split Resolution
  10. 不能从const char *转换为LPCWSTR --VS经常碰到
  11. 思科模拟器叫什么_宇宙无敌上帝视角无所不能之星球生成模拟器。
  12. c++解释--百度百科
  13. 从零开始做Vue前端架构(2)
  14. Javascript:radio单击触发事件
  15. Python读取并遍历CSV数据
  16. Hadoop学习之路(一)理论基础和逻辑思维
  17. 蓝桥杯 方格计数 Java
  18. 软件测试工程师应该如何进行职业规划?
  19. php利用phpqrcode生成二维码,并将二维码盖在一张图上实现美化
  20. 05Linux 第2天 进阶指令

热门文章

  1. Linux基础——gcc编译、静态库与动态库(共享库)
  2. C++ 的关键字(保留字)完整介绍
  3. 旷视《人工智能应用准则》全文公布 提倡善用AI技术
  4. 在VMware虚拟机下安装ubuntu16.04,及hackrf one环境配置
  5. java 面试 框架_这份java集合框架面试题,轻松搞定面试官!
  6. aidl生成java文件_Android AIDL自动生成Java文件测试
  7. python无穷大整数_python的特殊数字类型(无穷大、无穷小等)
  8. jdk 1.8 concurrenthashmap扩容原理
  9. redis是单线程的吗?为什么执行速度这么快?
  10. 012_Vue计算属性