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. How to Read, Write XLSX File in Java - Apach POI Example---reference

    No matter how Microsoft is doing in comparison with Google, Microsoft Office is still the most used ...

  2. JS和利用openssl的object C加密得到相同的aes加密密文

    这是之前接到的一个工作内容,项目原本的登录操作是获得账号和密码以后,对密码进行一遍MD5加密,然后传递账号和密文到cgi文件.在c中获取到账户以后,从数据库中获取到密码,对密码进行一次MD5的加密,然 ...

  3. HDU -2524 矩形A + B

    找规律题,这种题目比较巧妙,要仔细观察找出规律 1. 假设只有一行,一共有n列,那么由一个小矩形构成的矩形个数为n, 由两个小矩形构成的矩形个数为 n - 1个 .... 由 n 个小矩形构成的矩形个 ...

  4. jQuery事件与动画

    一 事件 1 加载DOM事件 $(document).ready():执行时机:DOM元素准备就绪  执行次数:多次  简单写法:原:$(document).ready(function(){})  ...

  5. Arcgis android - Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE

    报错: Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE Please check logcat output for more deta ...

  6. oralce中exists not exists in not in对于NULL的处理

    1.   先讨论 in 与 not in中存在NULL的情况, sql语句如下: 1 select 1 result1 from dual where 1 not in (2, 3); 2 3 4 s ...

  7. 单点登录CAS使用记(五):cas-client不拦截静态资源以及无需登录的请求。

    一.问题在哪? 在配置cas-client中,有这么一段配置: <filter> <filter-name>CAS Filter</filter-name> < ...

  8. phpcms V9 联动菜单的调用

    /*********************************** 通过id获取显示联动菜单的 顶级父类的名称* @param  $linkageid 联动菜单id* @param  $keyi ...

  9. 关于Zend Framework 2中 Zend\Session的使用

    一直迷惑于zend\Session的使用,这个是Zend\Session的官方教程的中文版,http://zend-framework-2.yangfan.co/blog/556. 其中最重要的是关于 ...

  10. 后台返回data直接在页面转换

    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <fmt: ...