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. Python 字典(Dictionary)Ⅱ

    删除字典元素 能删单一的元素也能清空字典,清空只需一项操作. 显示删除一个字典用del命令,如下实例: 但这会引发一个异常,因http://www.xuanhe.net/为用del后字典不再存在: 注 ...

  2. css-js-弹出层

    HTML: <!-- 弹出层 --> <div class="popwindow" > <div class="pop" id=& ...

  3. SpringBoot搭建基于Apache Shiro的权限管理功能

    Shiro 是什么 Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户“登录”: 授权 - 访问控制: 密码加密 ...

  4. 以Emacs Org mode为核心的任务管理方案

    前言 如今用于任务管理的方法与工具越来越多,如纸笔系统.日历与任务列表.Emacs Org mode系统,以及移动设备上的诸多应用.这些解决方案各具特色,在一定程度上能够形成互补作用.但是,它们彼此之 ...

  5. Cloud Computing——Everything as a Service

    service 分类 有Iaas, Paas, SaaS HDFS 总结☞: HDFS应付不了的场景 无法低时延 小文件存储存在空间利用率问题 文件不可修改 三副本有什么作用 防止单机故障,提高可用性 ...

  6. tf.InteractiveSession() 和 tf.Session() 的区别

    tf.InteractiveSession():它能让你在运行图的时候,插入一些计算图,这些计算图是由某些操作(operations)构成的.这对于工作在交互式环境中的人们来说非常便利,比如使用IPy ...

  7. (53)LINUX应用编程和网络编程之八Linux网络基础

    3.8.1.网络通信概述 3.8.1.1.从进程间通信说起:网络域套接字socket,网络通信其实就是位于网络中不同主机上面的2个进程之间的通信. 3.8.1.2.网络通信的层次 (1)硬件部分:网卡 ...

  8. for循环,foreach, map,reduce用法对比+for in,for of

    for不做赘述,相当简单: foreach方法: forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数. 注意: forEach() 对于空数组是不会执行回调函数的. array.f ...

  9. sqli-labs(22)

    接下里我们进入第二二关 好像和第21关一样 cookie的base64加密注入 闭合变成了双引号而已 0X01 构造语句进行尝试 " union select 1,2,3# IiB1bmlv ...

  10. JDK中String类的源码分析(一)

    1.String类是final的,不允许被继承 /** The value is used for character storage. */ private final char value[]; ...