CF Gym 100187E Two Labyrinths (迷宫问题)
题意:问两个迷宫是否存在公共最短路。
题解:两个反向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 (迷宫问题)的更多相关文章
- CF Gym 102028G Shortest Paths on Random Forests
CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...
- Codeforces Gym 100187E E. Two Labyrinths bfs
E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/prob ...
- Gym - 100187E E - Two Labyrinths —— bfs
题目链接:http://codeforces.com/gym/100187/problem/E 题解:一开始做的时候是将两幅图合并,然后直接bfs看是否能到达终点.但这种做法的错的,因为走出来的路对于 ...
- CF gym 101933 K King's Colors —— 二项式反演
题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...
- 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 ...
- CF Gym 100685A Ariel
传送门 A. Ariel time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 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 ...
- CF GYM 100703A Tea-drinking
题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...
- CF GYM 100703B Energy Saving
题意:王子每月买m个灯泡给n个房间换灯泡,如果当前有的灯泡数够列表的第一个房间换的就全换,直到灯泡不够为止,给出q个查询,查询x月已经换好几个房子,手里还剩多少灯泡. 解法:水题……小模拟. 代码: ...
随机推荐
- windows64下安装MySQLdb连接数据库
MySQLdb是Python连接MySQL的模块,下面介绍一下源码方式安装MySQLdb: 1.首先要下载:http://www.codegood.com/downloads,由于我的python是2 ...
- HDU - 4821 String(窗口移动+map去重+hash优化)
String Given a string S and two integers L and M, we consider a substring of S as “recoverable” if a ...
- HDU - 5875 Function(预处理)
Function The shorter, the simpler. With this problem, you should be convinced of this truth. Yo ...
- linux文件重命名
rename 命令用字符串替换的方式批量改变文件名. 语法 rename(参数) 参数 原字符串:将文件名需要替换的字符串: 目标字符串:将文件名中含有的原字符替换成目标字符串: 文件:指定要改变文件 ...
- Shaderlab blend
http://www.cnblogs.com/daxiaxiaohao/p/4059310.html 1.不透明度 当我们要将两个半透的纹理贴图到一个材质球上的时候就遇到混合的问题,由于前面的知识我们 ...
- 【转】insert忽略重复、mysql插入操作跳过、插入覆盖覆盖、mysql更新重复
需求背景:一般情况,插入数据的时候,有脏数据的情况,主键重复的话,直接insert into 会报错的,然后下面的sql都不再执行了,如果可以确定后面的数据可以覆盖前面的数据,直接用replace i ...
- 静态文件与APP
目录 静态文件的配置和使用 什么是静态文件? 为什么使用静态文件 如何配置,使用静态文件 静态文件相关(动态配置) app创建预注册 app指什么? 创建一个APP 注册app app文件作用 app ...
- 剑指Offer的学习笔记(C#篇)-- 二叉树的深度(详讲递归)
题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 一 . 思维发散 借助这个题目,我想用一个更好理解的方法说一说递归. ...
- vue+element-ui实现无限级动态菜单树
使用vue+element-ui实现无限级动态菜单 该案例实现主要使用递归的思想,递归对新人来容易迷惑的是自己调用自己,直到满足条件为止,接下来我们就一步一步实现一个动态多级菜单vue组件 搭建项目并 ...
- dom4j解析简单的xml文件 解析元素并封装到对象
package cn.itcast.xml; import cn.itcast.domain.Book; import org.dom4j.Document; import org.dom4j.Doc ...