bfs 记录和打印最短路径
Poj3984 迷宫问题
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
int maze[][];
int dir[][] = {{-,},{,},{,},{,-}}; //用结构体来记录更方便
struct Node
{
int x ,y ;
int prex ,prey; //前一节点坐标
int dis ; //记录是第几层访问的节点
} s[][]; void bfs(int x, int y)
{
//队列来实现bfs
queue <Node> q;
q.push(s[][]); //加入头节点
s[][].dis = ;
s[][].x=;
s[][].y=;
while(!q.empty())
{
Node temp = q.front();
int tx = temp.x;
int ty = temp.y;
int tdis = temp.dis; if(tx == && ty == ) //终止条件
return; for(int i = ; i < ; i++)
{
int row = tx + dir[i][];
int col = ty + dir[i][];
if(row >= && row < && col >= && col < && maze[row][col] != )
{
maze[row][col] = ;
s[row][col].x = row;
s[row][col].y = col;
s[row][col].prex = tx;
s[row][col].prey = ty;
s[row][col].dis = tdis + ; //有了这一步,便可知道最短路径的长度!
q.push(s[row][col]);
}
}
q.pop(); } } //递归打印路径!从后往前,打印从前往后
void print_path(int x,int y)
{ if(x == && y == ) //终止条件,打印第一个
{
cout<<"("<<s[][].x<<", "<<s[][].y<<")"<<endl;
return;
} int prex = s[x][y].prex;
int prey = s[x][y].prey;
print_path(prex,prey);
cout<<"("<<s[x][y].x<<", "<<s[x][y].y<<")"<<endl;
} int main()
{
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
cin>>maze[i][j];
bfs(,);
//cout<<s[4][4].dis<<endl;
print_path(,);
return ;
}
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10536 | Accepted: 6263 |
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 记录和打印最短路径的更多相关文章
- [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- Codeforces-A. Shortest path of the king(简单bfs记录路径)
A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...
- 迪杰斯特拉算法dijkstra(可打印最短路径)
#include <iostream> #include <iomanip> #include <string> using namespace std; #def ...
- 弗洛伊德算法Floyed(求各顶点间最短路径):可打印最短路径
#include <iostream> #include <string> #include <iomanip> using namespace std; #def ...
- uniGUI for C++ builder下如何利用FastReport实现数据记录本地打印
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/dlboy2018/article/details/81040260 (中行雷威2018.7.14于杭 ...
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- sdut oj 3058 路线冲突问题(BFS+记录路径算法,回溯路径 )
路线冲突问题 题目描述 给出一张地图,地图上有n个点,任意两点之间有且仅有一条路.点的编号从1到n. 现在兵团A要从s1到e1,兵团B要从s2到e2,问两条路线是否会有交点,若有则输出交点个数,否出输 ...
- - 迷宫问题 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, 0, 1, ...
随机推荐
- Android开源项目分类汇总[转]
Android开源项目分类汇总 如果你也对开源实现库的实现原理感兴趣,欢迎 Star 和 Fork Android优秀开源项目实现原理解析欢迎加入 QQ 交流群:383537512(入群理由需要填写群 ...
- 静态代码检查工具 cppcheck 的使用
CppCheck是一个C/C++代码缺陷静态检查工具.不同于C/C++编译器及其它分析工具,CppCheck只检查编译器检查不出来的bug,不检查语法错误.所谓静态代码检查就是使用一个工具检查我们 ...
- hdu1074 Doing Homework(状态压缩DP Y=Y)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- linux开关机命令
1.reboot重启 2.shutdown -r now 立即重启 root用户使用,与reboot命令相同 3.shutdown -r 10 过10分钟后重启root用户使用 4.shutdown ...
- HTML中<b>标签和<strong>便签的区别
最近碰到的问题,自己写的时候因为<b>标签比较简短偶尔使用,看到别人有使用<strong>标签的,本人不懂区别,在网上找的别人的东西,觉得很有道理,跟大家分享看看~~ 链接:h ...
- (转)精通 JS正则表达式
精通 JS正则表达式 (精通?标题党 ) 正则表达式可以: •测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用卡号码模式.这称为数据有效性验证 ...
- UGUI之UI的深度问题
学过NGUI的都知道,NGUI的深度是通过值来控制的.Panel也是UI也是,如果空间太多,布局复杂UI深度的值会变得很混乱.所以在NGUI中设置UI深度时一定要多加思考.然而在UGUI控制显示顺序的 ...
- iOS 网络与多线程--4.同步Post方式的网络请求
通过Post请求方式,同步获取网络数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据 在ViewController.m文件内的viewDidLoad函数添加一下测试代码 - (void) ...
- QT5控件-QPushButton和QFocusFrame(按钮和焦点框)
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPushButton> ...
- php 数组操作类(整合 给意见)
数组操作函数整理: /* 将一个二维数组按照指定字段的值分组 * * @param array $arr * @param string $keyField * * @return array */ ...