Gym - 100187E E - Two Labyrinths —— bfs
题目链接:http://codeforces.com/gym/100187/problem/E
题解:一开始做的时候是将两幅图合并,然后直接bfs看是否能到达终点。但这种做法的错的,因为走出来的路对于两幅图来说不一定都是最短的。正确做法:
第一步:分别用bfs求出两图的最短路。
第二步:如果最短路长度一样。则将两幅图合并,再bfs,如果能走到终点,且最短路长度仍然等于未合并前的长度,则YES; 否则NO。
学习之处: 求两个或多个事物所共有的东西,其实就是求交集。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#include<set>
//#define LOCAL
using namespace std;
#define pb push_back
#define ms(a, b) memset(a,b,sizeof(a));
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int mod = ;
const int maxn = ; struct node
{
int x,y,k;
};
node now, e; int n,m, vis[][];
char a[][], b[][], c[][];
int d[][] = {,,-,,,,,-}; int bfs(int x, int y, char s[][])
{
queue<node>q;
memset(vis,,sizeof(vis));
now.x = x;
now.y = y;
now.k = ;
vis[x][y] = ;
q.push(now); while(!q.empty())
{
now = q.front();
q.pop(); if(now.x==n && now.y==m)
return now.k; for(int i = ; i<; i++)
{
e.x = now.x + d[i][];
e.y = now.y + d[i][];
e.k = now.k + ; if(e.x< || e.x>n || e.y< || e.y>m || vis[e.x][e.y] || s[e.x][e.y]=='#')
continue; vis[e.x][e.y] = ;
q.push(e);
}
}
return -;
} int main()
{ cin>>n>>m;
for(int i = ; i<=n; i++)
scanf("%s",a[i]+); for(int i = ; i<=n; i++)
scanf("%s",b[i]+); for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
if(a[i][j]=='#' || b[i][j]=='#')
c[i][j] = '#';
else
c[i][j] = '.';
} int B = ;
int t1 = bfs(,,a);//分别搜两幅图的最短路
int t2 = bfs(,,b);
if(t1 == t2 && t1 !=-)//在最短路的前提下,再找两图合并的最短路是否存在并是否为原来的值
{
if( bfs(,,c)== t1 )
B = ;
} if(B)
puts("YES");
else
puts("NO");
}
Gym - 100187E E - Two Labyrinths —— bfs的更多相关文章
- Codeforces Gym 100187E E. Two Labyrinths bfs
E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/prob ...
- CF Gym 100187E Two Labyrinths (迷宫问题)
题意:问两个迷宫是否存在公共最短路. 题解:两个反向bfs建立层次图,一遍正向bfs寻找公共最短路 #include<cstdio> #include<cstring> #in ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- Gym 101617J Treasure Map(bfs暴力)
http://codeforces.com/gym/101617/attachments 题意:给出一个图,每个顶点代表一个金矿,每个金矿有g和d两个值,g代表金矿初始的金子量,d是该金矿每天的金子量 ...
- Gym - 100971J (思维+简单bfs)
题目链接:http://codeforces.com/gym/100971/problem/J J. Robots at Warehouse time limit per test 2.0 s mem ...
- Gym - 101572E Emptying the Baltic bfs加剪枝
题目传送门 题目大意:给出一幅海洋的描述,0为海平面,负数即有水,在给出的xy坐标的底部安放抽水机,问最多能有多少水.水往低处流,且八个方向都可以. 思路:bfs,记录到每一个节点有效的最低海平面,然 ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】
F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...
- Gym - 101147E E. Jumping —— bfs
题目链接:http://codeforces.com/gym/101147/problem/E 题意:当人在第i个商店时,他可以向左或向右跳di段距离到达另一个商店(在范围之内),一个商店为一段距离. ...
随机推荐
- [转]IIS7.5优化--提高线程数来适应高并发
根据压测结果做出的修改历史: 第一步:只针对maxWorkerThreads.maxIoThreads和minWorkerThreads做了修改<processModel autoConfig= ...
- 洛谷—— P2251 质量检测
https://www.luogu.org/problemnew/show/P2251 题目背景 无 题目描述 为了检测生产流水线上总共N件产品的质量,我们首先给每一件产品打一个分数A表示其品质,然后 ...
- cmake ccmake
下载libqrencode源码编译过程 git clone https://github.com/fukuchi/libqrencode.git 2001 mkdir build 2002 cd ...
- eclipse 添加 maven 小结
今天想创建了一个maven项目,由于之前重装系统,maven的一些配置要重新进行配置,y有时候一点小问题那真是一路曲折,为防止之后再遇到此问题,小结一下吧! 环境: eclipse apache- ...
- Css实现一个菜单导航
提要:使用大div定位设置为relative,子div设置为absolute实现菜单下拉 实现代码: <!DOCTYPE html> <html lang="en" ...
- Android Framework 记录之一
简介 之前的研究太偏向应用层功能实现了,很多原理不了解没有深究,现在研究framework框架层了. 记录 1.下载源码,目录如下: 2.Android系统的层次如下: 3.项目目录简单分析如下: 4 ...
- CMake - boost - 可运行程序 - 静态库
CMake - boost 最后更新日期:2014-04-25by kagula 阅读前提:<CMake入门(二)>.Linux的基本操作 环境: Windows 8.1 64bit英文版 ...
- project管理之makefile与自己主动创建makefile文件过程
(风雪之隅 http://www.laruence.com/2009/11/18/1154.html) Linux Makefile自己主动编译和链接使用的环境 想知道到Linux Makefile系 ...
- cocos2d-x入口类
上一篇文章中有一个在栈中创建的实例--AppDelegate.这个类的初始化使cocos2d-x的程序能够执行起来.由于它是继承于CCApplication类.而执行的run方法就是在此类中实现的. ...
- MySQL中文显示乱码
http://blog.csdn.net/acmain_chm/article/details/4174186