E. Two Labyrinths

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100187/problem/E

Description

A labyrinth is the rectangular grid, each of the cells of which is either free or wall, and it's possible to move only between free cells sharing a side.

Constantine and Mike are the world leaders of composing the labyrinths. Each of them has just composed one labyrinth of size n × m, and now they are blaming each other for the plagiarism. They consider that the plagiarism takes place if there exists such a path from the upper-left cell to the lower-right cell that is the shortest for both labyrinths. Resolve their conflict and say if the plagiarism took place.

Input

In the first line two integers n and m (1 ≤ n, m ≤ 500) are written — the height and the width of the labyrinths.

In the next n lines the labyrinth composed by Constantine is written. Each of these n lines consists of m characters. Each character is equal either to «#», which denotes a wall, or to «.», which denotes a free cell.

The next line is empty, and in the next n lines the labyrinth composed by Mike is written in the same format. It is guaranteed that the upper-left and the lower-right cells of both labyrinths are free.

Output

Output «YES» if there exists such a path from the upper-left to the lower-right cell that is the shortest for both labyrinths. Otherwise output «NO»

Sample Input

3 5
.....
.#.#.
.....

.....
#.#.#
.....

Sample Output

YES

HINT

题意

问你是否存在一条路,在两个迷宫都合法,而且都是最短路呢?

题解:

3次bfs,第一次bfs找到第一个迷宫的最短路,第二次bfs找到第二个迷宫的最短路,第三次bfs求共同的最短路

代码

  1. #include <cstdio>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <ctime>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <set>
  8. #include <vector>
  9. #include <sstream>
  10. #include <queue>
  11. #include <typeinfo>
  12. #include <fstream>
  13. #include <map>
  14. #include <stack>
  15. typedef long long ll;
  16. using namespace std;
  17. //freopen("D.in","r",stdin);
  18. //freopen("D.out","w",stdout);
  19. #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
  20. #define test freopen("test.txt","r",stdin)
  21. #define maxn 2000001
  22. #define mod 10007
  23. #define eps 1e-9
  24. const int inf=0x3f3f3f3f;
  25. const ll infll = 0x3f3f3f3f3f3f3f3fLL;
  26. inline ll read()
  27. {
  28. ll x=,f=;char ch=getchar();
  29. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  30. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  31. return x*f;
  32. }
  33. //**************************************************************************************
  34.  
  35. int a1[][];
  36. int a2[][];
  37. string s;
  38. struct node
  39. {
  40. int x,y,z;
  41. };
  42. int dx[]={,-,,};
  43. int dy[]={,,,-};
  44. int vis[][];
  45. int main()
  46. {
  47. int n=read(),m=read();
  48. for(int i=;i<n;i++)
  49. {
  50. cin>>s;
  51. for(int j=;j<m;j++)
  52. {
  53. if(s[j]=='.')
  54. a1[i][j]=;
  55. else
  56. a1[i][j]=;
  57. }
  58. }
  59. for(int i=;i<n;i++)
  60. {
  61. cin>>s;
  62. for(int j=;j<m;j++)
  63. {
  64. if(s[j]=='.')
  65. a2[i][j]=;
  66. else
  67. a2[i][j]=;
  68. }
  69. }
  70. queue<node> q;
  71. q.push((node){,,});
  72. int flag=;
  73. int ans1=inf;
  74. memset(vis,,sizeof(vis));
  75. vis[][]=;
  76. while(!q.empty())
  77. {
  78. node now=q.front();
  79. q.pop();
  80. if(now.x==n-&&now.y==m-&&ans1>now.z)
  81. {
  82. ans1=now.z;
  83. continue;
  84. }
  85. for(int i=;i<;i++)
  86. {
  87. node next;
  88. next.x=now.x+dx[i];
  89. next.y=now.y+dy[i];
  90. next.z=now.z;
  91. if(next.x<||next.x>=n)
  92. continue;
  93. if(next.y<||next.y>=m)
  94. continue;
  95. if(vis[next.x][next.y]||a1[next.x][next.y]==)
  96. continue;
  97. vis[next.x][next.y]=;
  98. q.push((node){next.x,next.y,next.z+});
  99. }
  100. }
  101. while(!q.empty())
  102. q.pop();
  103. q.push((node){,,});
  104. int ans2=inf;
  105. memset(vis,,sizeof(vis));
  106. vis[][]=;
  107. while(!q.empty())
  108. {
  109. node now=q.front();
  110. q.pop();
  111. if(now.x==n-&&now.y==m-&&ans2>now.z)
  112. {
  113. ans2=now.z;
  114. continue;
  115. }
  116. for(int i=;i<;i++)
  117. {
  118. node next;
  119. next.x=now.x+dx[i];
  120. next.y=now.y+dy[i];
  121. next.z=now.z;
  122. if(next.x<||next.x>=n)
  123. continue;
  124. if(next.y<||next.y>=m)
  125. continue;
  126. if(vis[next.x][next.y]||a2[next.x][next.y]==)
  127. continue;
  128. vis[next.x][next.y]=;
  129. q.push((node){next.x,next.y,next.z+});
  130. }
  131. }
  132.  
  133. if(ans1!=ans2)
  134. {
  135. puts("NO");
  136. return ;
  137. }
  138. while(!q.empty())
  139. q.pop();
  140. q.push((node){,,});
  141. memset(vis,,sizeof(vis));
  142. vis[][]=;
  143.  
  144. while(!q.empty())
  145. {
  146. if(flag)
  147. break;
  148. node now=q.front();
  149. q.pop();
  150. if(now.x==n-&&now.y==m-&&now.z==ans1)
  151. flag=;
  152. if(flag)
  153. break;
  154. for(int i=;i<;i++)
  155. {
  156. node next;
  157. next.x=now.x+dx[i];
  158. next.y=now.y+dy[i];
  159. next.z=now.z+;
  160. if(next.x<||next.x>=n)
  161. continue;
  162. if(next.y<||next.y>=m)
  163. continue;
  164. if(vis[next.x][next.y]||a1[next.x][next.y]==||a2[next.x][next.y]==)
  165. continue;
  166. vis[next.x][next.y]=;
  167. q.push((node){next.x,next.y,next.z});
  168. }
  169. }
  170. if(flag)
  171. puts("YES");
  172. else
  173. puts("NO");
  174.  
  175. }

Codeforces Gym 100187E E. Two Labyrinths bfs的更多相关文章

  1. Gym - 100187E E - Two Labyrinths —— bfs

    题目链接:http://codeforces.com/gym/100187/problem/E 题解:一开始做的时候是将两幅图合并,然后直接bfs看是否能到达终点.但这种做法的错的,因为走出来的路对于 ...

  2. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  3. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  4. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  5. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  6. 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 ...

  7. codeforces gym 100553I

    codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...

  8. CodeForces Gym 100213F Counterfeit Money

    CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...

  9. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

随机推荐

  1. OSX 10.10安装教程。

    现在苹果已经放出了OS X 10.9 Mavericks第一个开发者预览版,从Mac App Store中获得的安装程序,可以在10.8的系统中直接进行升级,原有文件都会保留.但是要想制作成一个10. ...

  2. AngularJS自定义指令(Directives)在IE8下的一个坑

    在项目中,由于要兼容到IE8,我使用1.2.8版本的angularJS.这个版本是支持自定义指令的.我打算使用自定义指令将顶部的header从其他页面分离.也就是实现在需要header的页面只用在&l ...

  3. Thread .join 的用法一例

    在使用身份证读卡器时,要求 1. 身份证读到身份证 就 停止线程. 2. 关闭界面时会 自动停止调用读身份证的线程.这时候就需要用到 Thead.join 例子如下: Thread thread; p ...

  4. anible包模块管理

    ansible使用包管理模块 一般使用的包管理模块的RPM,和YUM 参数 必填 默认 选择 说明 Conf_file No YUM的配置文件 Disable_dbg_check No No Yes/ ...

  5. sql统计重复数据

    sql代码如下: 统计重复的数据 select MingCheng from tabShouFeiGongShi group by MingCheng having count(MingCheng) ...

  6. BITED数学建模七日谈之七:临近比赛时的准备工作

    经过前面六天的文章分享,相信大家对数学模型的相关准备.学习都有了更新的认识,希望大家能从中有所收获,以便更高效地准备比赛和学习数学模型,本文是数学建模经验谈的最后一天:临近比赛的准备工作,希望在临近比 ...

  7. mysql执行update报错1175解决方法

    mysql执行update报错 update library set status=true where 1=1 Error Code: 1175. You are using safe update ...

  8. UNIX环境下用C语言写静态库与动态库

    静态库,动态库用UNIX 的术语来说,或者叫做归档文件(archive 常以.a 结尾)和共享对象(share object 常以lib 开头.so 结尾)更为准确.静态库,动态库可能是WINDOWS ...

  9. Masonry 实现输入框随键盘位置改变

    Github: https://github.com/saitjr/MasonryDemo 直接上代码: #import "ViewController4.h" #import & ...

  10. geeksforgeeks@ Find sum of different corresponding bits for all pairs (Bit manipulation)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=387 Find sum of different corresponding b ...