POJ - 3984迷宫问题(最短路径输出)
题目链接:http://poj.org/problem?id=3984
题目:
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 35034 | Accepted: 19912 |
Description
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, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
Output
Sample Input
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4) 解题思路:我们都知道BFS可以很简单求出最短路径的值,但是又怎样输出它的最短路径呢?我们可以开个node类型的二维数组pre[x][y],记录每个节点的前驱顶点是哪个顶点。因为每个节点的前驱节点是唯一的,只会是最快到达它的那个顶点,而每个节点的后继节点却不唯一,它可能可以朝各个方向走,所以记录它的前驱节点是可行的。然后就是输出问题,我们需要递归逆序输出就可以了。
附上代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int mp[][],vis[][];
int dir[][]={{,},{-,},{,},{,-}};
struct node{
int x,y;
};
node pre[][];
void BFS()
{
queue<node> que;
node str;
str.x=str.y=;
que.push(str);
vis[][]=;
while(!que.empty())
{
node now=que.front();
que.pop();
if(now.x==&&now.y==)
return;
for(int i=;i<;i++)
{
node next;
next.x=now.x+dir[i][];
next.y=now.y+dir[i][];
if(next.x>=&&next.x<&&next.y>=&&next.y<&&!mp[next.x][next.y]&&!vis[next.x][next.y])
{
vis[next.x][next.y]=;
que.push(next);
pre[next.x][next.y]=now;
}
}
}
}
void print(node cur)
{
if(cur.x==&&cur.y==)
{
printf("(0, 0)\n");
return;
}
print(pre[cur.x][cur.y]); //逆序输出
printf("(%d, %d)\n",cur.x,cur.y);
}
int main()
{
for(int i=;i<;i++)
for(int j=;j<;j++)
scanf("%d",&mp[i][j]);
BFS();
node ed;
ed.x=ed.y=;
print(ed);
return ;
}
POJ - 3984迷宫问题(最短路径输出)的更多相关文章
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
- POJ 3984 迷宫问题
K - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- POJ 3984 迷宫问题(简单bfs+路径打印)
传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- POJ 3984 迷宫问题(BFS)
迷宫问题 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...
- poj 3984 迷宫问题【bfs+路径记录】
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10103 Accepted: 6005 Description ...
- poj 3984 -- 迷宫问题 深搜
迷宫问题 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...
随机推荐
- tailf、tail -f、tail -F三者区别(转)
tail -f 等同于--follow=descriptor,根据文件描述符进行追踪,当文件改名或被删除,追踪停止 tail -F 等同于--follow=name --retry,根 ...
- Azure系列2.1.8 —— BlockEntry
(小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...
- laravel 项目表单中有csrf_token,但一直报错419错误 解决redis连接错误:MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persi
laravel 项目表单中有csrf_token,但一直报错419错误,因为项目中使用到Redis缓存,在强制关闭Redis后出现的问题,查询laravel.log文件查找相关问题 安装redis后在 ...
- MyBatis映射文件3(参数处理Map)
参数命名 POJO 如果多个参数,正好是业务逻辑的数据模型,那么我们就可以直接传入POJO,这样#{}中就可以直接使用属性名 Map 如果多个参数不是业务逻辑的数据模型,没有对应的POJO,为了方便, ...
- 剑指offer(16)栈的压入、弹出序列
题目: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈 ...
- Linux基础学习(16)--备份与恢复
第十六章——备份与恢复 一.备份概述 1.Linux系统需要备份的数据: 2.备份策略: 二.dump和restore命令 1.dump命令: 2.restore命令:
- Front-end Job Interview Questions
Front-end Job Interview Questions 前端面试 https://github.com/h5bp/Front-end-Developer-Interview-Questio ...
- c++ string替换指定字符串
string fnd = "dataset"; string rep = "labels"; string buf = "d:/data/datase ...
- GLSL 变量属性
1. attribute变量为这个attribute变量指定一个位置(用无符号值表示):glBindAttribLocation利用这个“位置”来指定需要传给shader里的attribue变量的数据 ...
- CodeForces 589F-Gourmet and Banquet-二分答案
有m盘菜,每盘有一个开始时间和结束时间,必须每盘都吃同样的时间.问最多能吃多久. 二分答案,然后用一个优先队列维护当前时间内的菜,然后每次都吃结束时间最小的那盘. #include <cstdi ...