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;
inline ll read()
{ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
//**************************************************************************************int a1[510][510];
int a2[510][510];
string s;
struct node
{int x,y,z;
};
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int vis[510][510];
int main()
{int n=read(),m=read();for(int i=0;i<n;i++){cin>>s;for(int j=0;j<m;j++){if(s[j]=='.')a1[i][j]=1;elsea1[i][j]=0;}}for(int i=0;i<n;i++){cin>>s;for(int j=0;j<m;j++){if(s[j]=='.')a2[i][j]=1;elsea2[i][j]=0;}}queue<node> q;q.push((node){0,0,0});int flag=0;int ans1=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(int i=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});int ans2=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(int i=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(int i=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");}

转载于:https://www.cnblogs.com/qscqesze/p/4657681.html

Codeforces Gym 100187E E. Two Labyrinths bfs相关推荐

  1. Codeforces Gym 101173 CERC 16 D BZOJ 4790 Dancing Disks

    Codeforces Gym 101173 CERC 16 D & BZOJ 4790 Dancing Disks 强烈安利这道构造题目,非常有意思. 这里用到的思想是归并排序! 多路归并排序 ...

  2. Codeforces Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven)

    Codeforces Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven) 题目来源: Codeforces 题意: 给出一些比赛, ...

  3. [Codeforces Gym 101651/100725B] Banal Tickets

    Codeforces Gym 100725 题解: 先分两种情况, 积为000与积非0" role="presentation" style="position ...

  4. CF Gym 100187E Two Labyrinths (迷宫问题)

    题意:问两个迷宫是否存在公共最短路. 题解:两个反向bfs建立层次图,一遍正向bfs寻找公共最短路 #include<cstdio> #include<cstring> #in ...

  5. Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest

    比赛链接: http://codeforces.com/gym/100676 题目链接: http://codeforces.com/gym/100676/attachments/download/3 ...

  6. CodeForces - [ACM-ICPC Jiaozuo Onsite D]Honeycomb(BFS)

    题目链接:https://codeforces.com/gym/102028/problem/F Time limit: 4.0 s Memory limit: 1024 MB Problem Des ...

  7. Codeforces Gym 100513G G. FacePalm Accounting 暴力

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

  8. Codeforces Gym 100269 Dwarf Tower (最短路)

    题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game na ...

  9. Codeforces Gym 100676G Training Camp 状压dp

    http://codeforces.com/gym/100676 题目大意是告诉你要修n门课,每门课有一个权值w[i], 在第k天修该课程讲获得k*w[i]的学习点数,给出了课程与先修课程的关系,要修 ...

  10. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

最新文章

  1. 你只写了两行代码,为什么要花两天时间?
  2. Nginx如何限流?
  3. opencv 读写XML YML
  4. 【转】WPF从我炫系列3---内容控件的用法
  5. 【Error】Less-loader 版本过高,TypeError: this.getOptions is not a function
  6. 诺微联盟催生智能手机的三足鼎立
  7. win10开始屏幕计算机,为什么Win10系统开始屏幕没反应?解决Win10系统开始屏幕没反应的方法...
  8. [转载] [Python图像处理] 二十二.Python图像傅里叶变换原理及实现
  9. 剑指Offer——Python答案
  10. AVI视频怎么转换成MOV视频
  11. Hibernate的一对一,一对多/多对一关联保存
  12. 单片机c语言每隔1m闪烁一次,单片机c语言闪烁灯程序
  13. AI制作卷边文字效果
  14. 火爆!联想Z5首售15分钟全网告罄力夺京东单品榜冠军
  15. Python判断字符串是否以字母开头
  16. 航空管理系统c语言程序设计,2018年北京航空航天大学软件院991数据结构与C语言程序设计之C程序设计考研核心题库...
  17. 最新微信三级分销系统源码 分销商城搭建 含完整代码包和安装部署教程
  18. 最新支持的各个版本Visual C ++ 2017 2015 2013 2010 2008等版本下载
  19. PC突然开始只能输入繁体,那可能是这个设置出了问题
  20. 免费的C++库—备用记录

热门文章

  1. Lambda表达式与委托
  2. Redis主从复制知识点
  3. 怎么做应力应变曲线_金属薄板塑性应变比ISO 10113:2020 解读
  4. 只能上qq不能开网页_真实记录女友和我开工作室之旅4:7.1~7.5日志记录和总结...
  5. 浏览器升级怎么升_测评:iOS 13 升不?新老机型跑分对比!
  6. JavaScript语法之语句、字面量、变量
  7. matlab用regress方法求ln函数_高中数学必背50条秒杀型公式和方法!高一高二高三都要看!...
  8. 结束SpringMVC
  9. 【渝粤教育】国家开放大学2018年春季 0049-21T法律文书 参考试题
  10. 深度优先遍历(DFS)例题