POJ-3984.迷宫问题(BFS + 路径输出)
昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了。流泪.jpg
本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径。
本题思路:主要就是BFS寻路,为了方便打印,从右下角开始进行BFS。
注意输出时候的大坑,会有标记。
本题代码:
#include <cstdio>
#include <queue>
#include <map>
using namespace std; typedef pair<int ,int > P;
const int n = , INF = 1e7;
int maze[n][n], d[n][n], dx[] = {, , -, }, dy[] = {, , , -};
P path[n][n];//用来保存每个结点的父节点,方便输出 void bfs() {
queue <P> s;
s.push(P(, ));//从终点到起点方便路径打印
for(int i = ; i < n; i ++)
for(int j = ; j < n; j ++) {
d[i][j] = INF;
path[i][j] = P(-, -);
}
d[][] = ;
while(s.size()) {
P p = s.front();
if(p.first == && p.second == ) break;
s.pop();
for(int i = ; i < ; i ++) {
int nx = p.first + dx[i], ny = p.second + dy[i];
if(nx >= && nx < n && ny >= && ny < n && maze[nx][ny] == && d[nx][ny] == INF) {
s.push(P(nx, ny));
d[nx][ny] = d[p.first][p.second] + ;
path[nx][ny] = P(p.first, p.second);
}
}
}
} int main () {
for(int i = ; i < n; i ++)
for(int j = ; j < n; j ++)
scanf("%d", &maze[i][j]);
bfs();
P p = make_pair(, );
while(p.first != -) {
printf("(%d, %d)\n", p.first, p.second);
int tmp = p.first;//这个很坑,坑了我4个小时,修改值之前进行记录
p.first = path[p.first][p.second].first;
p.second = path[tmp][p.second].second;
}
return ;
}
POJ-3984.迷宫问题(BFS + 路径输出)的更多相关文章
- 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, ...
- POJ - 3984迷宫问题(最短路径输出)
题目链接:http://poj.org/problem?id=3984 题目: 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- POJ 3984 迷宫问题 记录路径的广搜
主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~ #include<cstdio> # ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- POJ 3984 迷宫问题 bfs 难度:0
http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...
- poj 3984 迷宫问题 bfs
学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...
- [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
- POJ 3984 迷宫问题 (BFS + Stack)
链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ...
- POJ - 3984 迷宫问题 bfs解法
#include<stdio.h> #include<string.h> #include<algorithm> #include<stack> usi ...
随机推荐
- Callable和Future 多线程
参考:https://www.cnblogs.com/fengsehng/p/6048609.html
- tensorflow实战系列(一)
最近开始整理一下tensorflow,准备出一个tensorflow实战系列,以飨读者. 学习一个深度学习框架,一般遵循这样的思路:数据如何读取,如如何从图片和标签数据中读出成tensorflow可以 ...
- requirements.txt 的使用与创建
1. requirements.txt 主要是记录你的python 解释器安装了那些第三方模块,这样好方便项目迁移,自动解决掉项目的依赖关系 2. 网上找的那些关于 requirements 的文档 ...
- DataSnap Server 客户端调用 异常
No peer with the interface with guid {9BB0BE5C-9D9E-485E-803D-999645CE1B8F} has been registered.
- OWASP安全编码规范快速参考指南
0x00 原则 概览 开发安全的软件需要对安全原则有基本的了解.虽然对于安全原则的全面评估超出了本指南的范围,但是我们还是提供了一个快速的概览.软件安全的目标是要维护信息资源的 保密性 , 完整性 ...
- Spring注解标签详解@Autowired @Qualifier等
http://blog.csdn.net/wangsr4java/article/details/42777855 @Component.@Repository.@Service.@Controlle ...
- Leetcode 题解 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- jinjia
https://www.cnblogs.com/dachenzi/p/8242713.html
- 分布式存储Seaweedfs源码分析
基于源码版本号 0.67 , [Seaweedfs以前旧版叫Weedfs]. Seaweedfs 是一个非常优秀的由 golang 开发的分布式存储开源项目, 虽然在我刚开始关注的时候它在 githu ...
- 使用ab对网站进行压力测试
1.安装yum install httpd-tools 2.ab -kc 1000 -n 1000 http://localhost/ab.html 这个指令会使用1000个并发,进行连接1000次