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

题解:两个反向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. c的scanf为什么比c++的cin快

    很早就知道,c的scanf(printf)比c++的快.刷题时尤其明显,在这上面超时是常有的事儿. 但,这是别人告诉我的,c快. 为什么快? 从网上借鉴一个例子做个简单测试: 1.cpp     // ...

  2. React Native环境搭建(iOS、Mac)

    http://reactnative.cn/docs/0.42/getting-started.html#content 1.安装Homebrew Homebrew, Mac系统的包管理器,用于安装N ...

  3. 洛谷P3572 [POI2014]PTA-Little Bird

    P3572 [POI2014]PTA-Little Bird 题目描述 In the Byteotian Line Forest there are nn trees in a row. On top ...

  4. java利用URL发送get和post请求

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  5. Nacos深入浅出(三)

    EventDispatcher.fireEvent(new ConfigDataChangeEvent(true, dataId, group, tenant, time.getTime())); 跟 ...

  6. 微信小程序之登录

    微信小程序登录基本流程就是 1. wx.login获取js_code 根据app_id, secret, js_code 数据 wx.request 获取用户的openid和session_key   ...

  7. 基于角色权限管理:rbac设计分析以及具体细节

    权限管理---设计分析以及具体细节 说起权限我们大家都知道,不一样的角色会有不一样的权限. 比如就像学生管理系统一样,管理员,老师,学生之间的权限都是不一样的,那么展示的页面也是不一样的. 所以,我们 ...

  8. Ubuntu上源码安装golang并设置开发环境

    安装go #wget https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz #tar -xzf go1.10.3.linux-amd64.tar.g ...

  9. 六,IO系统

    六,IO系统 一,数据源 1,数据源--管道确认使用那根管道--节点流 2,先确定管道在tey中new出管道,new出后就写关闭代码,写完关闭代码在写中间代码 3,取数据和放数据结束语句必须有两个,不 ...

  10. Spring的配置及jar包下载

    一.相关说明 IOC: Inversion of Control,控制反转,是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.其中最常见的方式叫做依赖注入(Dependency In ...