bfs(同一最短路径)
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(同一最短路径)的更多相关文章
- hdu1428 记忆化搜索(BFS预处理最短路径和+DP+DFS)
题意:有一块 n * n 大小的方形区域,要从左上角 (1,1)走到右下角(n,n),每个格子都有通过所需的时间,并且每次所走的下一格到终点的最短时间必须比当前格子走到重点的最短时间短,问一共有多少种 ...
- xdoj新生现场赛1269——带有限制条件的bfs 寻找最短路径
bfss是解决最短路径的强大武器 (尝试dfs寻找最短路径 -(7*7)就会爆炸) 例题1 ccf 201604-4 游戏 问题描述 小明在玩一个电脑游戏,游戏在一个n×m的方格图上进行,小明控制 ...
- UVa 1599 理想路径(反向BFS 求最短路径 )
题意: 给定一个有重边有自环的无向图,n个点(2 <= n <= 100000), m条边(1 <= m <= 200000), 每条边有一个权值, 求从第一个点到n的最少步数 ...
- 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, ...
- bfs(最短路径)
链接:https://ac.nowcoder.com/acm/contest/993/F来源:牛客网 Farmer John is leaving his house promptly at 6 AM ...
- BFS - 求最短路径
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- 图论相关知识(DFS、BFS、拓扑排序、最小代价生成树、最短路径)
图的存储 假设是n点m边的图: 邻接矩阵:很简单,但是遍历图的时间复杂度和空间复杂度都为n^2,不适合数据量大的情况 邻接表:略微复杂一丢丢,空间复杂度n+m,遍历图的时间复杂度为m,适用情况更广 前 ...
- POJ3083——Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...
- POJ-1915 Knight Moves (BFS)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 26952 Accepted: 12721 De ...
- 『Pushing Boxes 双重bfs』
Pushing Boxes Description Imagine you are standing inside a two-dimensional maze composed of square ...
随机推荐
- vue和electron做的聊天应用表情包处理
表情包库: https://apps.timwhitlock.info/emoji/tables/unicode <template> <div @click.stop> &l ...
- shiro框架学习-3- Shiro内置realm
1. shiro默认自带的realm和常见使用方法 realm作用:Shiro 从 Realm 获取安全数据 默认自带的realm:idae查看realm继承关系,有默认实现和自定义继承的realm ...
- maven项目使用自己创建的jar包--maven without test code
eclipse版本为2018-12(4.10.0) 1.创建一个jar包 首先自己建立了一个maven project,名为jweb.GAV坐标: <groupId>amberai< ...
- SpringBoot搭建基于Apache Shiro的权限管理功能
Shiro 是什么 Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户“登录”: 授权 - 访问控制: 密码加密 ...
- XML 文档包含 XML 元素。
XML 文档包含 XML 元素. 什么是 XML 元素? XML 元素指的是从(且包括)开始标签直到(且包括)结束标签的部分. 元素可包含其他元素.文本或者两者的混合物.元素也可以拥有属性. < ...
- 为什么MongoDB适合大数据的存储?
NoSQL数据库都被贴上不同用途的标签,如MongoDB和CouchDB都是面向文档的数据库,但这并不意味着它们可以象JSON(JavaScript Object Notation,JavaScrip ...
- spring cloud:gateway-eureka
gateway-server-eureka 1. File-->new spring starter project 2.add dependency <dependency> &l ...
- Vue左滑组件slider的实现
本文链接:https://blog.csdn.net/latency_cheng/article/details/82983000 slider组件与swiper组件不同,slider滑动时并不翻页, ...
- POJ 1625 Censored ( Trie图 && DP && 高精度 )
题意 : 给出 n 个单词组成的字符集 以及 p 个非法串,问你用字符集里面的单词构造长度为 m 的单词的方案数有多少种? 分析 :先构造出 Trie 图方便进行状态转移,这与在 POJ 2278 中 ...
- LeetCode_70.爬楼梯
LeetCode-70 LeetCode_70.爬楼梯 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正 ...