http://oj.jxust.edu.cn/contest/Problem?id=1702&pid=7

题意:现在有两个相同大小的地图,左上角为起点,右下角问终点。问是否存在同一条最短路径。最短距离一样,他们走的路径也一样。

n 行 m 列(1 <= n , m <= 500)

存在就输出YES , 否则NO;

解法:三个bfs (其中一个是合并的图),判断三个最短路径是否相等且不为-1(不存在)。

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 20191117
#define PI acos(-1)
using namespace std;
typedef long long ll ;
char s[][] , str[][];
int vis[][];
int dir[][] = {{ , } , {- , } , { , } , { , -}};
int n , m ; struct node
{
int x , y , w ;
}; int bfs1(int x , int y , int w)
{
node a , b ;
a.x = x , a.y = y , a.w = w ;
queue<node>q;
q.push(a);
vis[x][y] = ;
while(!q.empty())
{
a = q.front();
q.pop();
if(a.x == n - && a.y == m -)
{
return a.w ;
}
for(int i = ; i < ; i++)
{
b.x = a.x + dir[i][];
b.y = a.y + dir[i][];
b.w = a.w + ;
if(b.x < || b.x >= n || b.y < || b.y >= m || vis[b.x][b.y] || s[b.x][b.y] == '#')
{
continue ;
}
vis[b.x][b.y] = ;
q.push(b);
}
}
return -;
} int bfs2(int x , int y , int w)
{
node a , b ;
a.x = x , a.y = y , a.w = w ;
queue<node>q;
q.push(a);
vis[x][y] = ;
while(!q.empty())
{
a = q.front();
q.pop();
if(a.x == n - && a.y == m -)
{
return a.w ;
}
for(int i = ; i < ; i++)
{
b.x = a.x + dir[i][];
b.y = a.y + dir[i][];
b.w = a.w + ;
if(b.x < || b.x >= n || b.y < || b.y >= m || vis[b.x][b.y] || str[b.x][b.y] == '#')
{
continue ;
}
vis[b.x][b.y] = ;
q.push(b);
}
}
return -;
} int main()
{ scanf("%d%d" , &n , &m);
for(int i = ; i < n ; i++)
{
for(int j = ; j < m ; j++)
{
cin >> s[i][j] ;
}
}
int ans1 = bfs1( , , );
for(int i = ; i < n ; i++)
{
for(int j = ; j < m ; j++)
{
cin >> str[i][j];
if(str[i][j] == '#' && s[i][j] == '*')
{
s[i][j] = '#';
}
}
}
memset(vis , , sizeof(vis));
int ans2 = bfs2( , , ) ;
memset(vis , , sizeof(vis));
int ans3 = bfs1( , , ) ;
if(ans1 == ans2 && ans2 == ans3 && ans1 != -)
{
printf("YES\n");
}
else{
printf("NO\n");
} return ;
}

bfs(同一最短路径)的更多相关文章

  1. hdu1428 记忆化搜索(BFS预处理最短路径和+DP+DFS)

    题意:有一块 n * n 大小的方形区域,要从左上角 (1,1)走到右下角(n,n),每个格子都有通过所需的时间,并且每次所走的下一格到终点的最短时间必须比当前格子走到重点的最短时间短,问一共有多少种 ...

  2. xdoj新生现场赛1269——带有限制条件的bfs 寻找最短路径

    bfss是解决最短路径的强大武器 (尝试dfs寻找最短路径 -(7*7)就会爆炸) 例题1  ccf 201604-4  游戏 问题描述 小明在玩一个电脑游戏,游戏在一个n×m的方格图上进行,小明控制 ...

  3. UVa 1599 理想路径(反向BFS 求最短路径 )

    题意: 给定一个有重边有自环的无向图,n个点(2 <= n <= 100000), m条边(1 <= m <= 200000), 每条边有一个权值, 求从第一个点到n的最少步数 ...

  4. POJ-3984 迷宫问题(BFS找最短路径并保存)

    问题: 定义一个二维数组:  int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, ...

  5. bfs(最短路径)

    链接:https://ac.nowcoder.com/acm/contest/993/F来源:牛客网 Farmer John is leaving his house promptly at 6 AM ...

  6. BFS - 求最短路径

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

  7. 图论相关知识(DFS、BFS、拓扑排序、最小代价生成树、最短路径)

    图的存储 假设是n点m边的图: 邻接矩阵:很简单,但是遍历图的时间复杂度和空间复杂度都为n^2,不适合数据量大的情况 邻接表:略微复杂一丢丢,空间复杂度n+m,遍历图的时间复杂度为m,适用情况更广 前 ...

  8. POJ3083——Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...

  9. POJ-1915 Knight Moves (BFS)

    Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26952   Accepted: 12721 De ...

  10. 『Pushing Boxes 双重bfs』

    Pushing Boxes Description Imagine you are standing inside a two-dimensional maze composed of square ...

随机推荐

  1. IP地址(参考百度百科)

    题目1:用子网掩码划分网络时,如果划分8个子网,则需要4位.因为24-2>=8.(全0代表网络自身,全1代表广播地址,所以减2) 以C类地址为例,(1)8个子网,则4位子网,主机号为后4位,主机 ...

  2. 对flex深入研究一点

    flex顶层设计 1.在任何流动的方向上(包括上下左右)都能进行良好的布局 2.可以以逆序 或者 以任意顺序排列布局 3.可以线性的沿着主轴一字排开 或者 沿着侧轴换行排列 4.可以弹性的在任意的容器 ...

  3. 安装caffe碰到的坑(各种.so未找到)

    ./include/caffe/common.hpp:4:32: fatal error: boost/shared_ptr.hpp: 没有那个文件或目录 所有类似于上面的错误,都可以用如下格式来解决 ...

  4. C++ 没有合适的默认构造函数(无参数构造函数)

    本来今天吧,想写一个proxy class的范例,写着写着出了个问题,见如下代码 ; Array1D* _elemArray = new Array1D[_cap]; 同时我为Array1D这个类写了 ...

  5. layui.form is not a function

    改变: var form = layui.form; 如果你是从1.x升级2.x造成的,那只需要在定义的时候吧后面的()去掉就可以了.请关注更新日志:1.x 升 2.0 特别注意事项layDate日期 ...

  6. postman导入接口

    给大家说一个poatman导入接口的好办法,平常要是想在postman上模拟接口,如果复杂的很难配,其实有一个很简单的方法: 现在我模拟一下百度搜索时历史记录的接口: 点击Copy as cUrl 然 ...

  7. sh_07_函数的嵌套调用

    sh_07_函数的嵌套调用 def test1(): print("*" * 50) def test2(): print("-" * 50) # 函数的嵌套调 ...

  8. python build-in function

    目录(?)[-] absx alliterable anyiterable basestring binx boolx callableobject chri classmethodfunction ...

  9. windows下开启远程连接Mysql

    使用“Ctrl + R”组合键快速打开cmd窗口,并输入“cmd”命令,打开cmd窗口.  使用“mysql -uroot -proot”命令可以连接到本地的mysql服务.  使用“use mysq ...

  10. nmon性能监控

    1.nmon下载地址 2../nmon_x86_rhel52 3.根据上面提示的快捷键进行输入即可显示相应的资源耗用情况,如输入:c.m.d(显示cpu.内存.磁盘使用情况) 4.输入数据到文件 ./ ...