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, ...
随机推荐
- 通过 Linux 容器进行虚拟化
简单介绍 Linux 容器是一种轻量级"虚拟化"方法,用于在单个控制主机上同一时候执行多个虚拟装置(容器).还有一个可用来描写叙述 Linux 容器所执行的操作的术语是" ...
- qDebug 学习小结
在qtcentre中看到有网友问这样一个问题: Why this doesn't work? qDebug() << "Test" << std::endl ...
- Embedded tomcat 7 servlet 3.0 annotations not working--转
Question: I have a stripped down test project which contains a Servlet version 3.0, declared with an ...
- [转] 关于SIGPIPE导致的程序退出
PS: 如果服务器程序不忽略SIGPIPE,在某些时候TCP writer收到这个信号,会导致进程退出 The rule that applies is: When a process writes ...
- [转] IPC之管道、FIFO、socketpair
管道和FIFO作为最初的UNIX IPC形式,现在已用得较少.SocketPair可作为全双工版本的管道,较为常用,这里简单做个笔记 管道 * 只用于有亲缘关系的进程间通信 * 单向,即半双工 (双向 ...
- Webpack 基本环境搭建
1. 第一步安装之前 先npm init 创建 package.json cnpm init; 然后全局安装 cnpm install webpack -g 确保哪里都可以使用 cnpm instal ...
- css中的垂直居中方法
单行文字 (外行高度固定) line-height 行高, 将line-height值与外部标签盒子的高度值设置成一致就可以了. height:3em; line-height:3em; 多行文字 图 ...
- 黑信 socket即时通讯 示例
整个过程 首先开启服务器 打开一个SDK大于4.4的手机---B 打开一个SDK小于4.4的手机---A 相互发送一条消息,对方就可以收到,当然这些消息都是通过服务器[转发]过来的 MainActiv ...
- java图片缩放
package com.rubekid.springmvc.utils; import java.awt.AlphaComposite; import java.awt.Graphics2D; imp ...
- 基于jQuery仿uploadify的HTML5图片上传控件jquery.html5uploader
(function($){ var methods = { init:function(options){ return this.each(function(){ var $this = $(thi ...