Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
Http://codeforces.com/gym/100187/problem/E
Description
A Labyrinth is the rectangular grid, each of the cells of which are either free or wall, and it's possible to move only bet Ween free cells sharing a side.
Constantine and Mike is the world leaders of composing the labyrinths. Each of the them have just composed one labyrinth of size nxm, and now they is 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's the shortest for both labyrinths. Resolve their conflict and say if the plagiarism took place.
Input
The first line of integers n and M (1≤n, m≤500) is written-the height and the width of the labyrinths.
The next n lines the labyrinth composed by Constantine is written. Each of these n lines consists of M characters. Each character are equal either to«#», which denotes a wall, or to«.», which denotes a free cell.
The next line was empty, and the next n lines the labyrinth composed by Mike was written in the same format. It is guaranteed, 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's the shortest for both Labyrin Ths. Otherwise output«no»
Sample Input
3 5
.....
.#.#.
.....
.....
#.#.#
.....
Sample Output
YES
HINT
Test instructions
Ask you if there is a road, in two mazes are legal, and are the shortest way?
Exercises
3 times BFS, first BFS find the first labyrinth of the shortest, the second BFS find the second labyrinth of the shortest, the third BFS to seek common shortest
Code
#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineTest Freopen ("Test.txt", "R", stdin)#defineMAXN 2000001#defineMoD 10007#defineEPS 1e-9Const intinf=0x3f3f3f3f;Constll infll =0x3f3f3f3f3f3f3f3fll;inline ll Read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//**************************************************************************************inta1[510][510];inta2[510][510];strings;structnode{intx, Y, z;};intdx[4]={1,-1,0,0};intdy[4]={0,0,1,-1};intvis[510][510];intMain () {intN=read (), m=read (); for(intI=0; i<n;i++) {cin>>s; for(intj=0; j<m;j++) { if(s[j]=='.') A1[i][j]=1; ElseA1[i][j]=0; } } for(intI=0; i<n;i++) {cin>>s; for(intj=0; j<m;j++) { if(s[j]=='.') A2[i][j]=1; ElseA2[i][j]=0; }} Queue<node>Q; Q.push (node) {0,0,0}); intflag=0; intans1=inf; memset (Vis,0,sizeof(VIS)); vis[0][0]=1; while(!Q.empty ()) {Node now=Q.front (); Q.pop (); if(now.x==n-1&&now.y==m-1&&ans1>now.z) {ans1=now.z; Continue; } for(intI=0;i<4; i++) {node Next; Next.x=now.x+Dx[i]; Next.y=now.y+Dy[i]; Next.z=now.z; if(next.x<0|| next.x>=N)Continue; if(next.y<0|| next.y>=m)Continue; if(vis[next.x][next.y]| | a1[next.x][next.y]==0) Continue; VIS[NEXT.X][NEXT.Y]=1; Q.push (node) {next.x,next.y,next.z+1}); } } while(!q.empty ()) Q.pop (); Q.push (node) {0,0,0}); intAns2=inf; memset (Vis,0,sizeof(VIS)); vis[0][0]=1; while(!Q.empty ()) {Node now=Q.front (); Q.pop (); if(now.x==n-1&&now.y==m-1&&ans2>now.z) {ans2=now.z; Continue; } for(intI=0;i<4; i++) {node Next; Next.x=now.x+Dx[i]; Next.y=now.y+Dy[i]; Next.z=now.z; if(next.x<0|| next.x>=N)Continue; if(next.y<0|| next.y>=m)Continue; if(vis[next.x][next.y]| | a2[next.x][next.y]==0) Continue; VIS[NEXT.X][NEXT.Y]=1; Q.push (node) {next.x,next.y,next.z+1}); } } if(ans1!=ans2) {Puts ("NO"); return 0; } while(!q.empty ()) Q.pop (); Q.push (node) {0,0,0}); memset (Vis,0,sizeof(VIS)); vis[0][0]=1; while(!Q.empty ()) { if(flag) Break; Node now=Q.front (); Q.pop (); if(now.x==n-1&&now.y==m-1&&now.z==ans1) Flag=1; if(flag) Break; for(intI=0;i<4; i++) {node Next; Next.x=now.x+Dx[i]; Next.y=now.y+Dy[i]; Next.z=now.z+1; if(next.x<0|| next.x>=N)Continue; if(next.y<0|| next.y>=m)Continue; if(vis[next.x][next.y]| | a1[next.x][next.y]==0|| a2[next.x][next.y]==0) Continue; VIS[NEXT.X][NEXT.Y]=1; Q.push (node) {next.x,next.y,next.z}); } } if(flag) puts ("YES"); Elseputs ("NO");}
Codeforces Gym 100187E E. Labyrinths BFS