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 ...
随机推荐
- tf.expand_dims和tf.squeeze函数
from http://blog.csdn.net/qq_31780525/article/details/72280284 tf.expand_dims() Function tf.expand_d ...
- postman—创建collection,执行collection和批量执行
接口测试中,可以在 Postman 逐个创建请求.但当请求逐渐增多时,如果我们不采取任何措施管理,散乱的请求维护起来就比较麻烦了.这个时候我们可以创建测试集 Collection 来对这些请求进行管理 ...
- Mysql 日期函数date_format()
用法:DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据 1.语法 date_fromat(date,format) 说明:date 参数是合法的日期.format 规定日期/时间的输 ...
- BZOJ1460: Pku2114 Boatherds
题目链接:点这里 题目描述:给你一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. 数据范围:\(n\le1e5\,,p\le100\,,长 ...
- windows10 下 gcc/g++ 的安装
一.gcc的下载 网址:www.mingw.org ,点击右上方的 download installer 二.安装 打开安装程序,默认安装,弹出下列界面 找到mingw32-gcc-g++(注意cla ...
- C#获取网页信息并存入数据库
1,获取以及商品分类信息 给一网页获取网页上商品信息的分类 using Skay.WebBot; using System; using System.Collections.Generic; usi ...
- 大v用户数据统计分析
1,统计数据的基本情况,包括微博总数,用户总数,最活跃和最不活跃的用户id #!/bin/sh source_dir=/home/minelab/data/DATA source_file_name= ...
- python数据类型之 元祖、列表字典
Python中元祖,列表,字典 Python中有3种內建的数据结构:列表.元祖和字典: 1.列表 list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目. 列表中的项目应该包 ...
- IDEA使用一套代码启动多个应用
在为公司开发一个消息中心,开发过程中需要模拟多个消费者.具体方式: 1.编辑应用配置 2.复制应用配置 3.重命名配置 4.修改端口,-Dserver.port=9991
- webpack前置知识2(JavaScript项目初始化)
所有的JavaScript项目都是在终端输入npm init -y进行项目初始化,如果要自定义项目规则,去掉 -y 参数. vscode终端快捷键ctrl+` 初始化 运行上述命令后,项目内会新建一个 ...