Codeforces Gym 100187E E. Two Labyrinths bfs
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=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** int a1[][];
int a2[][];
string s;
struct node
{
int x,y,z;
};
int dx[]={,-,,};
int dy[]={,,,-};
int vis[][];
int main()
{
int n=read(),m=read();
for(int i=;i<n;i++)
{
cin>>s;
for(int j=;j<m;j++)
{
if(s[j]=='.')
a1[i][j]=;
else
a1[i][j]=;
}
}
for(int i=;i<n;i++)
{
cin>>s;
for(int j=;j<m;j++)
{
if(s[j]=='.')
a2[i][j]=;
else
a2[i][j]=;
}
}
queue<node> q;
q.push((node){,,});
int flag=;
int ans1=inf;
memset(vis,,sizeof(vis));
vis[][]=;
while(!q.empty())
{
node now=q.front();
q.pop();
if(now.x==n-&&now.y==m-&&ans1>now.z)
{
ans1=now.z;
continue;
}
for(int i=;i<;i++)
{
node next;
next.x=now.x+dx[i];
next.y=now.y+dy[i];
next.z=now.z;
if(next.x<||next.x>=n)
continue;
if(next.y<||next.y>=m)
continue;
if(vis[next.x][next.y]||a1[next.x][next.y]==)
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();
if(now.x==n-&&now.y==m-&&ans2>now.z)
{
ans2=now.z;
continue;
}
for(int i=;i<;i++)
{
node next;
next.x=now.x+dx[i];
next.y=now.y+dy[i];
next.z=now.z;
if(next.x<||next.x>=n)
continue;
if(next.y<||next.y>=m)
continue;
if(vis[next.x][next.y]||a2[next.x][next.y]==)
continue;
vis[next.x][next.y]=;
q.push((node){next.x,next.y,next.z+});
}
} if(ans1!=ans2)
{
puts("NO");
return ;
}
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();
if(now.x==n-&&now.y==m-&&now.z==ans1)
flag=;
if(flag)
break;
for(int i=;i<;i++)
{
node next;
next.x=now.x+dx[i];
next.y=now.y+dy[i];
next.z=now.z+;
if(next.x<||next.x>=n)
continue;
if(next.y<||next.y>=m)
continue;
if(vis[next.x][next.y]||a1[next.x][next.y]==||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的更多相关文章
- Gym - 100187E E - Two Labyrinths —— bfs
题目链接:http://codeforces.com/gym/100187/problem/E 题解:一开始做的时候是将两幅图合并,然后直接bfs看是否能到达终点.但这种做法的错的,因为走出来的路对于 ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- 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 ...
- codeforces gym 100553I
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
- CodeForces Gym 100213F Counterfeit Money
CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
随机推荐
- asp.net使用httphandler打包多CSS或JS文件以加快页面加载速度
介绍 使用许多小得JS.CSS文件代替一个庞大的JS或CSS文件来让代码获得更好的可维 护性,这是一个很好的实践.但这样做反过来却损失了网站的性能.虽然你应该将你的Javascript代码写在小文件中 ...
- boost的link 和 runtime-link,搭配shared 和 static
转自:http://blog.csdn.net/yasi_xi/article/details/8660549 link:生成动态链接库/静态链接库.生成动态链接库需使用shared方式,生成静态链接 ...
- 记录一次cefsharp1输入法在win7下异常解决定位
最近几天都被基于cefSharp封装的浏览器控件搞疯了!对于cefSharp基本满足当前所做项目的需求,但是有一个问题一直困扰我,那就是系统中偶尔会出现输入法不能转换到中文.而且这个问题似乎没有什么规 ...
- spring初探1
spring初探1 关于新建对象,对象依赖的三种方式比较 场景 某个交易的业务组建拆分,为原先的功能模块新写了一个业务组件 使用new. 修改上层代码的对象生成部分( 如果不是面向接口编程,简直就是灾 ...
- 25个CSS3 渐变和动画效果教程
随着最新版CSS3渐变和动画功能发布,Web开发者在开发的过程中有了更多的选择.实际上,已经有了一些替代的技术,目的都是使网站的建设变得简易,高效和快速.不过CSS3所提供的渐变功能有着显著的优点,特 ...
- Hadoop第三天---分布式文件系统HDFS(大数据存储实战)
1.开机启动Hadoop,输入命令: 检查相关进程的启动情况: 2.对Hadoop集群做一个测试: 可以看到新建的test1.txt和test2.txt已经成功地拷贝到节点上(伪分布式只有一个节 ...
- Strassen算法
如题,该算法是来自德国的牛逼的数学家strassen搞出来的,因为把n*n矩阵之间的乘法复杂度降低到n^(lg7)(lg的底是2),一开始想当然地认为朴素的做法是n^3,哪里还能有复杂度更低的做法,但 ...
- labview图形和图表的类型
http://zone.ni.com/reference/zhs-XX/help/371361L-0118/lvconcepts/types_of_graphs_and_charts/ LabVIEW ...
- oracle根据正则表达式查找对应的字段
语法如下: SELECT * FROM 表名WHERE regexp_like(表字段,'正则') 例如: 查找某字段小数点后有两个小数以上的信息 SELECT * FROM A TWHERE reg ...
- ACM之递推递归
Hdu 2569 突破蝙蝠的包围,yifenfei来到一处悬崖面前,悬崖彼岸就是前进的方向,好在现在的yifenfei已经学过御剑术,可御剑轻松飞过悬崖. 现在的问题是:悬崖中间飞着很多红,黄,蓝三种 ...