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=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;
            else
                a1[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;
            else
                a2[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");
    else
        puts("NO");

}
时间: 2024-08-08 09:42:17

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

CodeForces Gym 101047E Escape from Ayutthaya BFS

Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 101047E Description standard input/output Ayutthaya was one of the first kingdoms in Thailand, spanning since its foundation in 1350 to its collaps

Codeforces gym Hello 2015 Div1 B and Div2 D

Codeforces gym 100571 problem D Problem 给一个有向图G<V,E>和源点S,边的属性有长度L和颜色C,即E=<L,C>.进行Q次询问,每次给定一个点X,输出S到X的最短路的长度(不存在则输出 -1).但要求S到X的路径中相邻两条边颜色不一样. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 |V|, |E|: [1, 10^5] X, S: [1, |V| ] L: [1, 10^9] |C|

Codeforces gym Hello 2015 Div1 E

Codeforces gym 100570 problem E (一种处理动态最长回文子串问题的方法) Problem 给一个长度为N的字符串S,字符集是'a'-'z'.进行Q次操作,操作分三种.一,修改位置X的字符为C:二,查询以P位置为中心的最长回文子串的长度,并输出:三,查询以P与P+1的中间位置为中心的最长回文子串的长度,并输出. More 第二种操作子串长度为奇数,一定存在:第三种操作子串长度为偶数,若不存在,输出 -1. Limits Time Limit(ms): 4000(1s足

Codeforces gym Hello 2015 Div1 C and Div2 E

Codeforces gym 100570 problem C Codeforces gym 100571 problem E Problem 给一个N行M列的矩阵Ma,进行Q次(Q<=10)查询,每次给定一个K,问有多少子矩阵,满足最大值max - 最小值min <=K. Limits Time Limit(ms): 8000 Memory Limit(MB): 512 N, M: [1, 400] Q: [1, 10] Ma(i, j), K: [1, 10^9] Solution (Th

【模拟】ECNA 2015 I What&#39;s on the Grille? (Codeforces GYM 100825)

题目链接: http://codeforces.com/gym/100825 题目大意: 栅栏密码.给定N(N<=10),密钥为一个N*N的矩阵,'.'代表空格可以看到,'X'代表被遮挡,还有密文字符串S,长度为N*N 每次将这个矩阵顺时针旋转90°,把矩阵中空格对应的位置按照从上到下从左到右的顺序依次填充上密文字符,求最终这个密文字符能否填满N*N的矩阵,能按顺序输出得到的答案,不能输出"invalid grille" 题目思路: [模拟] 直接模拟即可.旋转的坐标公式很好推.

Codeforces gym Hello 2015 Div2 B

Codeforces gym 100571 problem B Problem 设函数F(x),F(1)与F(2)已知,且当 i>=3,F(i)=a*F(i-2)+b*F(i-1).再给一个长度为N的数列A,进行Q次如下操作:每次给一个区间[L, R],对于每个k(L=<k<=R),将A[k]=A[k]+F[k-L+1].最后输出数列A(mod 10^9+7). Limits Time Limit(ms): 1000 Memory Limit(MB): 256 N, Q: [1, 10^

Codeforces Gym - 101147J Whistle&#39;s New Car

Discription Statements Whistle has bought a new car, which has an infinite fuel tank capacity. He discovered an irregular country since it has n cities and there are exactly n?-?1roads between them, of course, all cities are connected. He is so much

Codeforces Gym 101174 A Within Arm&#39;s Reach 贪心 手臂

#include<iostream> #include<stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <math.h> using namespace std; #define LL long long const int maxn=25; double a[maxn],l[maxn],r[maxn]; double ex,ey

Codeforces gym 100685 F. Flood bfs

F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Description We all know that King Triton doesn't like us and therefore shipwrecks, hurricanes and tsunami do happen. But being bored with the same routine