题目链接: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的更多相关文章

  1. Codeforces Gym 100187E E. Two Labyrinths bfs

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

  2. CF Gym 100187E Two Labyrinths (迷宫问题)

    题意:问两个迷宫是否存在公共最短路. 题解:两个反向bfs建立层次图,一遍正向bfs寻找公共最短路 #include<cstdio> #include<cstring> #in ...

  3. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  4. Gym 101617J Treasure Map(bfs暴力)

    http://codeforces.com/gym/101617/attachments 题意:给出一个图,每个顶点代表一个金矿,每个金矿有g和d两个值,g代表金矿初始的金子量,d是该金矿每天的金子量 ...

  5. Gym - 100971J (思维+简单bfs)

    题目链接:http://codeforces.com/gym/100971/problem/J J. Robots at Warehouse time limit per test 2.0 s mem ...

  6. Gym - 101572E Emptying the Baltic bfs加剪枝

    题目传送门 题目大意:给出一幅海洋的描述,0为海平面,负数即有水,在给出的xy坐标的底部安放抽水机,问最多能有多少水.水往低处流,且八个方向都可以. 思路:bfs,记录到每一个节点有效的最低海平面,然 ...

  7. Codeforces gym 100685 F. Flood bfs

    F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...

  8. 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 ...

  9. Gym - 101147E E. Jumping —— bfs

    题目链接:http://codeforces.com/gym/101147/problem/E 题意:当人在第i个商店时,他可以向左或向右跳di段距离到达另一个商店(在范围之内),一个商店为一段距离. ...

随机推荐

  1. IDEA连接linux服务器

    idea连接linux(完成了xshell和xftp连接linux的功能,可以直接卸载这俩了..) File->settings->Deployment左侧加号添加 选择传输类型ftp或者 ...

  2. ML | SVM

    What's xxx An SVM model is a representation of the examples as points in space, mapped so that the e ...

  3. android应用无法接收到广播?

    本篇文章记录Android应用无法接收到广播的几种case 1. 没有register 广播其实是一种订阅者模式,所以当然需要先register,register的方式有两种 1.1  through ...

  4. 用Java做一个跳一跳辅助

    Java跳一跳辅助 首先..让大家看下效果: 由于是闲着没事写的.也懒得看OpenCV的API了. 要用到的工具如下: adb+jdk 把adb添加到环境变量里就行.很简单. 全部代码如下.就这么多. ...

  5. HDU3625 Examining the Rooms

    @(HDU)[Stirling數] Problem Description A murder happened in the hotel. As the best detective in the t ...

  6. debian6之eclipse和jdk安装

    安装JDK 目前最新的JDK版本是:Java SE Development Kit 7u5 下载地址:http://www.oracle.com/technetwork/java/javase/dow ...

  7. 数组全排列 knuth 分解质因数

    template<typename T> void swap(T* a, T* b) { T temp = *a; *a = *b; *b = temp; } //数组的全排列 void ...

  8. maven编译maven-surefire-plugin插件报错

    1.编译错误信息: [INFO] ------------------------------------------------------------------------ [ERROR] Fa ...

  9. MRP routing设置释疑

    Jeffer9@gmail.com         工艺是指在不同工作中心执行的作业序列         作业的详细信息 Number of cycles 在该工作中心操作几个循环 Number of ...

  10. Irrlicht 3D Engine 笔记系列之 教程4 - Movement

    作者: i_dovelemon 日期: 2014 / 12 / 16 来源: CSDN 主题: Event Receiver, Animator, Framerate independent move ...