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();
    ;ch=getchar();}
    +ch-';ch=getchar();}
    return x*f;
}
//**************************************************************************************

][];
][];
string s;
struct node
{
    int x,y,z;
};
]={,-,,};
]={,,,-};
][];
int main()
{
    int n=read(),m=read();
    ;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. 看懂mysql执行计划--官方文档

    原文地址:https://dev.mysql.com/doc/refman/5.7/en/explain-output.html 9.8.2 EXPLAIN Output Format The EXP ...

  2. [蓝牙] 5、Battery Service module

    Detailed Description This module implements the Battery Service with the Battery Level characteristi ...

  3. winrt控件

    http://www.mindscapehq.com/products/metroelements 常见的翻书,相册,图表,时间组件 demo下载地址http://assets.mindscape.c ...

  4. Mac OS X中MacPorts安装和使用

      安装 官网pkg安装   搜索索引中的软件port search name 安装新软件sudo port install name 卸载软件sudo port uninstall name 查看有 ...

  5. Perl 随笔

    1.    .pl  文件带入参数: ./auto_cfg.pl ./mconfig.config ./boardconfig.config ./menuconfig.config .ver  ./a ...

  6. jsp 变量和方法的声明 Java程序片 HTML注释 JSP注释

    <%!...%> 声明变量和方法 <%!...%>之中的变量为JSP页面的成员变量,当多个线程访问本页面时,多个线程共享此变量. <%@ page contentType ...

  7. Classes and Objects :类和对象(2)

    类内部可以有另一个类,也就是内部类,如果带static则为静态内部类静态内部类当然不能直接访问实例变量内部类修饰符可以有四种,而外部类只有两种 内部类的意义:这个内部类只适用于这个外部类因为外部类的某 ...

  8. json转换(c#后台生成json的方法)

    此文转自:http://bbs.csdn.net/topics/380200497,为了方便自己记忆才以文章形式保存. using System; using System.Collections.G ...

  9. ORMBase对象/关系型数据库映射在MVC中的应用(二)

    3.DataBase基类,查询方法返回值是List<T>,并且是分页的,ThePart.dll版本2.0中封装了一个PageInfo类,作为分页的类型.这种方法很机械,也很狗血..建议大家 ...

  10. Codevs_1230_元素查找_(set/Hash)

    描述 http://codevs.cn/problem/1230/ ... 1230 元素查找 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond       题目 ...