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求共同的最短路

```#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>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2000001
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
{
ll x=,f=;char ch=getchar();
;ch=getchar();}
+ch-';ch=getchar();}
return x*f;
}
//**************************************************************************************

][];
][];
string s;
struct node
{
int x,y,z;
};
]={,-,,};
]={,,,-};
][];
int main()
{
;i<n;i++)
{
cin>>s;
;j<m;j++)
{
if(s[j]=='.')
a1[i][j]=;
else
a1[i][j]=;
}
}
;i<n;i++)
{
cin>>s;
;j<m;j++)
{
if(s[j]=='.')
a2[i][j]=;
else
a2[i][j]=;
}
}
queue<node> q;
q.push((node){,,});
;
int ans1=inf;
memset(vis,,sizeof(vis));
vis[][]=;
while(!q.empty())
{
node now=q.front();
q.pop();
&&now.y==m-&&ans1>now.z)
{
ans1=now.z;
continue;
}
;i<;i++)
{
node next;
next.x=now.x+dx[i];
next.y=now.y+dy[i];
next.z=now.z;
||next.x>=n)
continue;
||next.y>=m)
continue;
)
continue;
vis[next.x][next.y]=;
q.push((node){next.x,next.y,next.z+});
}
}
while(!q.empty())
q.pop();
q.push((node){,,});
int ans2=inf;
memset(vis,,sizeof(vis));
vis[][]=;
while(!q.empty())
{
node now=q.front();
q.pop();
&&now.y==m-&&ans2>now.z)
{
ans2=now.z;
continue;
}
;i<;i++)
{
node next;
next.x=now.x+dx[i];
next.y=now.y+dy[i];
next.z=now.z;
||next.x>=n)
continue;
||next.y>=m)
continue;
)
continue;
vis[next.x][next.y]=;
q.push((node){next.x,next.y,next.z+});
}
}

if(ans1!=ans2)
{
puts("NO");
;
}
while(!q.empty())
q.pop();
q.push((node){,,});
memset(vis,,sizeof(vis));
vis[][]=;

while(!q.empty())
{
if(flag)
break;
node now=q.front();
q.pop();
&&now.y==m-&&now.z==ans1)
flag=;
if(flag)
break;
;i<;i++)
{
node next;
next.x=now.x+dx[i];
next.y=now.y+dy[i];
next.z=now.z+;
||next.x>=n)
continue;
||next.y>=m)
continue;
||a2[next.x][next.y]==)
continue;
vis[next.x][next.y]=;
q.push((node){next.x,next.y,next.z});
}
}
if(flag)
puts("YES");
else
puts("NO");

}```

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

1. Codeforces gym 100685 F. Flood bfs

F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...

2. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

3. 【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)

题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

4. ACM: Gym 101047E Escape from Ayutthaya - BFS

Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

5. codeforces Gym 100187J J. Deck Shuffling dfs

J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

6. Codeforces Gym 100187K K. Perpetuum Mobile 构造

K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

7. Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

8. Codeforces Gym 100342C Problem C. Painting Cottages 转化题意

Problem C. Painting CottagesTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

9. CodeForces Gym 100500A A. Poetry Challenge DFS

Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

随机推荐

1. HTML基础

HTML指的是超文本标记语言 (Hyper Text Markup Language), HTML不是一种编程语言,而是一种标记语言 (markup language) ,HTML使用标记标签来描述网 ...

2. Duilib源码分析(四)绘制管理器—CPaintManagerUI—(前期准备一)

上节中提到在遍历创建控件树后,执行了以下操作:      1. CDialogBuilder构建各控件对象并形成控件树,并返回第一个控件对象pRoot:     2. m_pm.AttachDialo ...

3. Java集合框架

集合框架体系如图所示 Java 集合框架提供了一套性能优良,使用方便的接口和类,java集合框架位于java.util包中, 所以当使用集合框架的时候需要进行导包. Map接口的常用方法 Map接口提 ...

4. Can&#39;t connect to MySQL server on localhost (0)

配置双主的时候,由于一台始终连不上另一台. 于是我使用root账号远程登录查看,填入账户密码点击连接,结果就提示这样的错误. 一开始以为是权限的问题,于是就授与所有权,结果还是报一样的错. 重新创建一 ...

5. 使用php来访问操作sql server

使用php来访问操作sql server 在此分成三步走: 第一部:查看配置,下载文件 首先查看自己的php和sql server版本 Php文件输入echo PHP_VERSION  运行脚本就可以 ...

6. HDU1853 Cyclic Tour（最小费用最大流）

题目大概说给一张有向图,每条边都有权值,要选若干条边使其形成若干个环且图上各个点都属于且只属于其中一个环,问选的边的最少权值和是多少. 各点出度=入度=1的图是若干个环,考虑用最小费用最大流: 每个点 ...

7. ES6中的Class

对于javascript来说,类是一种可选(而不是必须)的设计模式,而且在JavaScript这样的[[Prototype]] 语言中实现类是很蹩脚的. 这种蹩脚的感觉不只是来源于语法,虽然语法是很重 ...

8. vsftp 用无法登陆的用户名登陆（也叫虚拟用户）并进入到自己的文件夹test