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. 第一篇!in和exists性能比较和使用

    首先,先看下in和exists的区别: in 是把外表和内表作hash 连接: exists是对外表作loop循环,每次loop循环再对内表进行查询. 普遍的观点是exists比in效率高的.但是这不 ...

  2. RSA 非对称加密 数字签名 数字证书

    什么是RSA加密算法 RSA加密算法是一种非对称加密算法,算法的数学基础是极大数分解难题. RSA加密算法的强度也就是极大数分解的难度,目前700多位(二进制)的数字已经可以破解,1024位认为是比较 ...

  3. ShareSDK 社会化分享 集成步骤

    第一步 :获取ShareSDK 官网:http://www.mob.com 完整的集成文档:http://wiki.mob.com/android-sharesdk%E5%AE%8C%E6%95%B4 ...

  4. noip 2012 开车旅行

    /*考场上写的暴力 40分钟70分*/ #include<iostream> #include<cstdio> #include<cstring> #define ...

  5. codevs 3693 数三角形

    /* n*m个点中选3个 再排除三点共线 共线分两类 1 在横线或者竖线上 m*C(n,3) n*C(m,3) 2 在对角线上 这个比较麻烦 以为对角线和矩阵是一一对应的 我们转化成求矩阵 并且保证有 ...

  6. CriticalFinalizerObject的作用

    CriticalFinalizerObject 在从 CriticalFinalizerObject 类派生的类中,公共语言运行库 (CLR) 保证所有关键终止代码都有机会执行, 即使是在 CLR 强 ...

  7. Redis,MemCached,MongoDB 概述

    调研项目主要有Redis. MemCached. MongoDB,以及Amazon的DynamoDB Redis 是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key- ...

  8. jQuery旋转插件

    jQuery旋转插件,支持Internet Explorer 6.0 + .Firefox 2.0.Safari 3.Opera 9.Google Chrome,高级浏览器下使用Transform,低 ...

  9. 重复造轮子感悟 – XLinq性能提升心得

    曾经的两座大山 1.EF 刚接触linq那段时间,感觉这家伙好神奇,语法好优美,好厉害.后来经历了EF一些不如意的地方,就想去弥补,既然想弥补,就必须去了解原理.最开始甚至很长一段时间都搞不懂IQue ...

  10. C#基础学习第一天(.net菜鸟的成长之路-零基础到精通)

    1.Net平台和C#编程语言的概念 2.桌面应用程序: 我们要使用桌面应用程序,必须要安装该应用程序的客户端. winform应用程序. Application:应用程序 Internet:互联网应用 ...