题意:问两个迷宫是否存在公共最短路。

题解:两个反向bfs建立层次图,一遍正向bfs寻找公共最短路

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = +; int d1[maxn][maxn];
int d2[maxn][maxn]; char g1[maxn][maxn];
char g2[maxn][maxn]; int n,m;
struct node{
int x,y;
node(int X = , int Y = ){
x = X; y = Y;
}
}; int dx[] = {,-,,};
int dy[] = {,,,-};
//1,1
void rbfs(int id)
{
char (*G)[maxn];
int (*vis)[maxn];
if(id == ) G = g1, vis = d1;
else G = g2, vis = d2;
memset(vis,-,sizeof(d1));
queue<node>q;
node u(n-,m-);
q.push(u);
vis[u.x][u.y] = ;
while(q.size()){
u = q.front();q.pop();
if(u.x == && u.y == ) return;
for(int i = ; i < ; i++){
int nx = u.x + dx[i], ny = u.y + dy[i];
if(nx>=&&nx<n&&ny>=&&ny<m&&G[nx][ny]!='#'&&!~vis[nx][ny]){
vis[nx][ny] = vis[u.x][u.y]+;
q.push(node(nx,ny));
}
}
}
}
bool vis[maxn][maxn]; bool bfs()
{
if(d1[][] != d2[][] )return false;
memset(vis,,sizeof(vis));
queue<node>q;
q.push(node(,));
int tx = n-, ty = m-;
while(q.size()){
node &u = q.front();
if(u.x == tx && u.y == ty) return true;
for(int i = ; i < ; i++){
int nx = u.x + dx[i], ny = u.y + dy[i];
if(nx>=&&nx<n&&ny>=&&ny<m&&
d1[nx][ny] == d1[u.x][u.y] - && d2[nx][ny] == d2[u.x][u.y] - && !vis[nx][ny]){
vis[nx][ny] = ;
q.push(node(nx,ny));
}
}
q.pop();
}
return false;
} int main()
{
// freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i = ; i < n; i++)
scanf("%s",g1[i]);
for(int i = ; i < n; i++)
scanf("%s",g2[i]);
rbfs();
rbfs();
printf("%s\n",bfs()?"YES":"NO");
return ;
}

CF Gym 100187E Two Labyrinths (迷宫问题)的更多相关文章

  1. CF Gym 102028G Shortest Paths on Random Forests

    CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...

  2. Codeforces Gym 100187E E. Two Labyrinths bfs

    E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/prob ...

  3. Gym - 100187E E - Two Labyrinths —— bfs

    题目链接:http://codeforces.com/gym/100187/problem/E 题解:一开始做的时候是将两幅图合并,然后直接bfs看是否能到达终点.但这种做法的错的,因为走出来的路对于 ...

  4. CF gym 101933 K King's Colors —— 二项式反演

    题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...

  5. cf Gym 101086M ACPC Headquarters : AASTMT (Stairway to Heaven)

    题目: Description standard input/output As most of you know, the Arab Academy for Science and Technolo ...

  6. CF Gym 100685A Ariel

    传送门 A. Ariel time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. CF Gym 100685E Epic Fail of a Genie

    传送门 E. Epic Fail of a Genie time limit per test 0.5 seconds memory limit per test 64 megabytes input ...

  8. CF GYM 100703A Tea-drinking

    题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...

  9. CF GYM 100703B Energy Saving

    题意:王子每月买m个灯泡给n个房间换灯泡,如果当前有的灯泡数够列表的第一个房间换的就全换,直到灯泡不够为止,给出q个查询,查询x月已经换好几个房子,手里还剩多少灯泡. 解法:水题……小模拟. 代码: ...

随机推荐

  1. 教你如何暴力破解-telnet ftp ssh mysql mssql vnc 等

    大家应该都知道暴力破解的原理,但却不知道遇见telnet和ftp等服务和数据库如何破解,今天小R就教大家如何利用一个工具就能对其破解. 今天要给大家介绍的工具是:hydra(中文名:九头蛇) 这个名听 ...

  2. HDU - 6114 2017百度之星初赛B Chess

    Chess  Accepts: 1805  Submissions: 5738  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 32768 ...

  3. Beetl使用注意事项

    1.如果表达式跟定界符或者占位符有冲突,可以在用 “\” 符号 @for(user in users){ email is ${user.name}\@163.com @} ${[1,2,3]} // ...

  4. PHP之递归函数

    https://www.cnsecer.com/4146.html http://www.jb51.net/article/71424.htm //一列数字的规则如下:1,1,2,3,5,8,13,2 ...

  5. 洛谷P1556 幸福的路

    P1556 幸福的路 题目描述 每天,John都要为了农场里N(1≤N≤10)头牛的健康和幸福四处奔波. 每头牛的位置可以描述为一个二维坐标,John从坐标原点(0,0)出发.为了使路径更有趣,Joh ...

  6. [NOIP2008] 传球游戏

    1011. [NOIP2008] 传球游戏 http://cogs.pro/cogs/problem/problem.php?pid=1011 ★   输入文件:ballg.in   输出文件:bal ...

  7. redis单机上部署集群

    一.安装单机redis  redis的安装:版本至少是3.2.8及其以上,这里以3.2.8版本为例说明 1.安装redis wget http://download.redis.io/releases ...

  8. JS 检测字符串是否还有某个字符

    function filer(s) { var str = "字符串"; if (str.indexOf(s) == -1) { alert("没有"); } ...

  9. Linux Maven install

    1 下载 maven : http://maven.apache.org/download.cgi2 解压 tar -xvf apache-maven-3.3.9-bin.tar.gz3 移到所需目录 ...

  10. mysql issue:

    ####0 https://yq.aliyun.com/ziliao/53466 首先,很荣幸你找到了这篇文章... 如果你忘记了mysql的密码不妨试试以下这个方法. 1.打开my.cnf  代码如 ...