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 ...
随机推荐
- 集合比较器报错java.lang.IllegalArgumentException: Comparison method violates its general contract!
Collections.sort(listMonthlyUsage, new Comparator<MonthlyUsageDto>() { //按照元素从小到大排序 @Override ...
- 容器内安装nvidia,cuda,cudnn
/var/lib/docker/overlay2 占用很大,清理Docker占用的磁盘空间,迁移 /var/lib/docker 目录 du -hs /var/lib/docker/ 命令查看磁盘使用 ...
- 创建ThreadFactory实例的多种方式
spring的CustomizableThreadFactory guava的MoreExecutors.platformThreadFactory()静态方法 guava的ThreadFactory ...
- SQL create file遇到操作系统错误5拒绝访问
当在sql server 2014创建一个数据库时出现错误:尝试打开或创建物理文件 'G:\Test.mdf' 时,CREATE FILE 遇到操作系统错误 5(拒绝访问). 原因及解决方法如下: 这 ...
- linux下vsftpd的安装及配置使用详细步骤(推荐)
vsftpd 是“very secure FTP daemon”的缩写,安全性是它的一个最大的特点. vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行在诸如 Linux.BS ...
- Java 生成二进制加减法题目
日常算数,有益身心健康. int a; int b; int result; int symbol; int count = 50; Random random = new Random(); for ...
- insert和insertSelective区别
两者的区别在于如果选择insert 那么所有的字段都会添加一遍即使没有值 <insert id="insert" parameterType="com.ego.po ...
- QPS、TPS、PV、UV、GMV、IP、RPS
摘自:https://www.citrons.cn/jishu/226.html 关于 QPS.TPS.PV.UV.GMV.IP.RPS 这些词语,看起来好像挺专业.但实际上,我认为是这是每个程序员必 ...
- 身份证最后一位按照ISO7064:1983.MOD11-2校验码
居民身份证号码,根据[中华人民共和国国家标准 GB 11643-1999]中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成.排列顺序从左至右依次为:六位数字地 ...
- java应用cpu使用率过高问题排查
---------------------------------------linux下如何定位代码问题------------------------------- 1.先通过top命令找到消耗c ...