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

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

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 记录和打印最短路径的更多相关文章

  1. [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)

    题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...

  2. HDU1026--Ignatius and the Princess I(BFS记录路径)

    Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...

  3. 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 ...

  4. 迪杰斯特拉算法dijkstra(可打印最短路径)

    #include <iostream> #include <iomanip> #include <string> using namespace std; #def ...

  5. 弗洛伊德算法Floyed(求各顶点间最短路径):可打印最短路径

    #include <iostream> #include <string> #include <iomanip> using namespace std; #def ...

  6. uniGUI for C++ builder下如何利用FastReport实现数据记录本地打印

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/dlboy2018/article/details/81040260 (中行雷威2018.7.14于杭 ...

  7. POJ.3894 迷宫问题 (BFS+记录路径)

    POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...

  8. sdut oj 3058 路线冲突问题(BFS+记录路径算法,回溯路径 )

    路线冲突问题 题目描述 给出一张地图,地图上有n个点,任意两点之间有且仅有一条路.点的编号从1到n. 现在兵团A要从s1到e1,兵团B要从s2到e2,问两条路线是否会有交点,若有则输出交点个数,否出输 ...

  9. - 迷宫问题 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, ...

随机推荐

  1. Ubuntu Server安全Webserver搭建流程

    之前整过CentOS.整了Ubuntu才发现,Ubuntu简单多了--不知道性能相比又怎样. 以Ubtuntu 14.04为例.记录一下搭建流程. 一.SSHserver 第一件事当然是ssh,默认安 ...

  2. swift开发笔记24 解决键盘遮挡输入框 的方法

    很简单,就是开始输入时把整个view的frame上移,也就是把y值减小就行了,至于减少多少自己调 ,也可以动态获取参见(http://blog.csdn.net/lengshengren/articl ...

  3. python使用正則表達式

    python中使用正則表達式 1. 匹配字符 正則表達式中的元字符有 .  ^  $ *   +  ?  { }  [ ]  \  | ( ) 匹配字符用的模式有 \d 匹配随意数字 \D 匹配随意非 ...

  4. Example of how to use both JDK 7 and JDK 8 in one build.--reference

    JDK 8 Released Most of us won’t be able to use/deploy JDK 8 in production for a looong time. But tha ...

  5. svg text文字居中

    <text x="100" y="100" text-anchor="middle" dominant-baseline=" ...

  6. 你真的了解 console 吗

    对于前端开发者来说,在开发过程中需要监控某些表达式或变量的值的时候,用 debugger 会显得过于笨重,取而代之则是会将值输出到控制台上方便调试.最常用的语句就是console.log(expres ...

  7. (转)SVN源码管理(上&下)

    原文地址:http://www.cnblogs.com/IPrograming/archive/2012/12/15/SVN_1.html 使用SVN进行源码管理(上) 在原来的项目中使用的源码管理工 ...

  8. 开启mysql慢查询日志并使用mysqldumpslow命令查看

    mysql服务器有一项功能,可以检测到哪条sql语句查询得比较慢,就是慢查询slowlog,现在介绍如何开启. 在[mysqld]下面增加如下代码: long_query_time = 1 log-s ...

  9. 获取subview

    通常我们在view层级里面对subView的操作可以通过两种方式:1.保留一个subview的引用,然后在类中通过该引用对该subview进行操作,但是要注意在适当的位置添加内存维护的代码,退出前手动 ...

  10. JDK环境变量配置及Tomcat安装服务

    1.测试jdk安装是否成功: 在cmd中输入java -version 2.环境变量: 1)新建系统变量JAVA_HOME,如:D:\Program Files\Java\jdk1.8.0_60 2) ...