定义一个二维数组:


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)

记录路径并输出

代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<algorithm>
4 #include<iostream>
5 #include<queue>
6 using namespace std;
7 struct shu
8 {
9 int y,x,step;
10 }str1,str2;
11 int a,s,d,f,g,q[10][10],v[10][10],w[10][10],z[25],c[25];
12 int p[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
13 void bfs()
14 {
15 int xx,yy,x,y;
16 queue<shu>r;
17 r.push(str1);
18 while(!r.empty())
19 {
20 str1=r.front();
21 r.pop();
22 if(str1.y==4 && str1.x==4)
23 {
24 xx=q[str1.y][str1.x]%10;
25 yy=(q[str1.y][str1.x]-xx)/10;
26 while(1) //while循环找寻路径
27 {
28 z[g]=yy;c[g]=xx;
29 g++;
30 if(q[yy][xx]==0)
31 {
32 break;
33 }
34 x=q[yy][xx]%10; //找到这一个点的前一个点的坐标(因为我们之前维护过,所以用同样方式找寻点坐标)
35 y=(q[yy][xx]%100-x)/10;
36 yy=y;
37 xx=x;
38 }
39 while(!r.empty())
40 r.pop();
41 return;
42 }
43 str2.step=str1.step+1;
44 for(int i=0;i<4;++i)
45 {
46
47 str2.y=str1.y+p[i][1];
48 str2.x=str1.x+p[i][0];
49 if(str2.y<0 || str2.x<0 || str2.y>=5 || str2.x>=5) continue;
50 if(v[str2.y][str2.x]==0 && w[str2.y][str2.x]==0)
51 {
52 q[str2.y][str2.x]=str1.y*10+str1.x; //把它的前一个点的坐标维护成一个唯一值
53 w[str2.y][str2.x]=1;
54 r.push(str2);
55 /*
56 除了这一种方式之外,我们还可以定义一个结构体专门保存上一个点的坐标
57
58 */
59 }
60 }
61 }
62 }
63 int main()
64 {
65 g=0;
66 for(int i=0; i<5;++i)
67 {
68 for(int j=0; j<5 ;++j)
69 {
70 scanf("%d",&v[i][j]);
71 }
72 }
73 q[0][0]=0;
74 str1.x=0;
75 str1.y=0;
76 str1.step=0;
77 w[0][0]=1;
78 bfs();
79 printf("(0, 0)\n");
80 for(int i=g-1;i>=0;--i)
81 {
82 printf("(%d, %d)\n",z[i],c[i]);
83 }
84 printf("(4, 4)\n");
85 return 0;
86 }

- 迷宫问题 POJ - 3984 bfs记录路径并输出最短路径的更多相关文章

  1. Q - 迷宫问题 POJ - 3984(BFS / DFS + 记录路径)

    Q - 迷宫问题 POJ - 3984 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...

  2. poj 2395 bfs/记录路径

    http://poj.org/problem?id=2935 Basic Wall Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

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

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

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

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

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

  6. 迷宫问题(bfs+记录路径)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...

  7. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  8. (简单) POJ 3414 Pots,BFS+记录路径。

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  9. 迷宫问题 POJ - 3984 (搜索输出路径)

    题目大意 题目不需要大意,poj居然还有中文题 鸣谢 特别鸣谢ljc大佬提供的方法!!! 解法 我们可能输出个最短路径的长度比较简单,但是输出最短路径真的是没有做过,这里有一种简单的方法 因为我们的d ...

随机推荐

  1. python学习笔记 | 顺序表的常规操作

    ''' @author: 人人都爱小雀斑 @time: 2020/3/11 8:46 @desc: 顺序表的相关操作 ''' class SequenceList: def __init__(self ...

  2. Python pip install 默认路径修改。

    pip动不动就下载数百M的文件.这些文件默认在C:盘,那么为了节省空间需要修改这些路径: 打开cmd命令窗口.输入: python -m site C:\Users\hewei>python - ...

  3. 一文带你学会AQS和并发工具类的关系2

    1.创建公平锁 1.使用方式 Lock reentrantLock = new ReentrantLock(true); reentrantLock.lock(); //加锁 try{ // todo ...

  4. 经常使用的Sublime Text 快捷键

    最常用的 Sublime快捷键:

  5. 串口使用Pipeline时诡异的ReadOnlySequence问题

    借鉴之前的Pipeline的操作方式,现在目标是给串口读取操作也使用上Pipeline.稍微改造一下,以下代码可以直接运行. 协议为使用连续的4个0XFF作为结尾,没有头标志.数据总长为68位定长. ...

  6. 【Oracle】11G 11.2.0.4 RAC环境打补丁

    一.准备工作 1,数据库环境 操作系统版本  : RedHat 7.2 x64   数据库版本    : Oracle 11.2.0.4 x64 RAC    Grid          : 11.2 ...

  7. oracle 存储过程和包的权限

    GRANT CREATE ANY PROCEDURE TO MONKEY --創建,查看,替換的權限 GRANT EXECUTE ANY PROCEDURE TO MONKEY --執行和查看的權限 ...

  8. Kubernetes 升级过程记录:从 1.17.0 升级至最新版 1.20.2

    本文记录的是将 kubernetes 集群从 1.17.0 升级至最新版 1.20.2 的实际操作步骤,由于 1.17.0 无法直接升级到 1.20.2,需要进行2次过滤升级,1.17.0 -> ...

  9. 求得二叉搜索树的第k小的元素

    求得二叉搜索树的第k小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 须知:二叉搜索树,又叫二叉排序树,二叉查找树.特点是:左子树的所有元素都小于等 ...

  10. chain issues incorrect order,EXtra certs,Contains anchor

    背景: 下载颁发下来的ssl证书安装好之后网站正常显示安全,但是通过ssl证书网站去检测报错误:chain issues incorrect order,EXtra certs,Contains an ...