codeforces-gym-100187-E【bfs】

题目链接:点击打开链接

E. Two Labyrinths
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Examples
input
3 5
.....
.#.#.
.....
  

.....
#.#.#
.....
output
NO
input
3 5
.....
.#.##
.....
  

.....
##.#.
.....
output
YES

大意:给你两个图,问两个图从 (0,0) 到 (n-1,m-1) 的最短路径是否相同

思路:跑三发 bfs,第一次找一图的最短路,第二次找二图的最短路,第三次核查两图的最短路是否相同。刚开始以为 dfs 能闯过去,交了一发果断 tle

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
int n,m;
char mp[2][510][510];
bool vis[2][510][510];
bool mark[510][510];
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
int ans[2];
struct node
{
	int x,y,step;
};
bool judge(int x,int y)
{
	if(x<0||y<0||x>=n||y>=m)
		return 0;
	return 1;
}
queue<node> Q;
void bfs(int id)
{
	while(!Q.empty())	Q.pop();
	node now,next;
	now.x=0,now.y=0,now.step=0;
	vis[id][0][0]=1;
	Q.push(now);
	while(!Q.empty())
	{
		now=Q.front();
		Q.pop();
		if(now.x==n-1&&now.y==m-1)
		{
			ans[id]=now.step;
			return ;
		}
		for(int i=0;i<4;i++)
		{
			next.x=now.x+dx[i];
			next.y=now.y+dy[i];
			if(judge(next.x,next.y)&&mp[id][next.x][next.y]=='.'&&vis[id][next.x][next.y]==0)
			{
				next.step=now.step+1;
				vis[id][next.x][next.y]=1;
				Q.push(next);
			}
		}
	}
}
bool flag;
void find()
{
	while(!Q.empty())	Q.pop();
	node now,next;
	now.x=0,now.y=0,now.step=0;
	mark[0][0]=1;
	Q.push(now);
	while(!Q.empty())
	{
		now=Q.front();
		Q.pop();
		if(now.x==n-1&&now.y==m-1&&now.step==ans[0]) // 这里要加判断步数才能 ac 
		{
			flag=1;
			return ;
		}
		for(int i=0;i<4;i++)
		{
			next.x=now.x+dx[i];
			next.y=now.y+dy[i];
			if(judge(next.x,next.y)&&mark[next.x][next.y]==0&&mp[0][next.x][next.y]=='.'&&mp[1][next.x][next.y]=='.')
			{
				next.step=now.step+1;
				mark[next.x][next.y]=1;
				Q.push(next);
			}
		}
	}
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=0;i<n;i++)
			scanf("%s",mp[0][i]);
		for(int i=0;i<n;i++)
			scanf("%s",mp[1][i]);
		ans[0]=INF,ans[1]=INF,ans[2]=INF;
		memset(vis,0,sizeof(vis));
		bfs(0);	bfs(1);
		if(ans[0]!=ans[1])
		{
			puts("NO");
			continue;
		}
		memset(mark,0,sizeof(mark));
		flag=0;
		find();
//		printf("%d %d %d\n",ans[0],ans[1],ans[2]);
		if(flag)
			puts("YES");
		else
			puts("NO");
	}
	return 0;
 } 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值