定义一个二维数组:


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. (二)数据源处理1-configparser读取.ini配置文件

    import osimport configparsercurrent_path =os.path.dirname(__file__)#获取config当前文件路径config_file_path = ...

  2. Java并发包源码学习系列:挂起与唤醒线程LockSupport工具类

    目录 LockSupport概述 park与unpark相关方法 中断演示 blocker的作用 测试无blocker 测试带blocker JDK提供的demo 总结 参考阅读 系列传送门: Jav ...

  3. docker 常用的容器命令

    容器命令 # --name 给容器起名 # -p 端口映射 # -d 后台启动 # -it 交互模式启动 # 交互模式启动 # docker run -it 镜像名/id /bin/bash # do ...

  4. 【Spring】Spring中的Bean - 1、Baen配置

    Bean配置 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 什么是Spring中的Bean? Spring可以被看作是一个 ...

  5. 【Oracle】查看哪些用户被授予了DBA权限

    查看哪些用户被授予了DBA权限 select * from dba_role_privs where granted_role='DBA'; 回收权限: revoke dba from xxx;

  6. kubernets之计算资源

    一  为pod分配cpu,内存以及其他的资源 1.1  创建一个pod,同时为这个pod分配内存以及cpu的资源请求量 apiVersion: v1 kind: Pod metadata: name: ...

  7. LuoguP5748 集合划分计数

    题意 一个有\(n\)个元素的集合,将其分为任意个非空子集,求方案数.集合之间是无序的,\(\{\{1,2\},\{3\}\}=\{\{3\},\{1,2\}\}\). 设\(f_n\)表示用\(n\ ...

  8. AOP面向切面编程(使用注解和使用配置文件)

    Aop(面向切面编程) 使用注解的方式: 加入相应的jar包: com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspe ...

  9. Linux下利用ifconfig命令查看和操纵网络接口

    为了说明这个问题,首先我们需要解释一下在Linux系统下"网络接口"的含义.通俗来讲,Linux中的所谓网络接口就是指本机的网卡,它相当于计算机的一台负责对网络进行收发数据的外设. ...

  10. InnoDB的主键选择与插入优化

    索引的存放方式MyISAM和InnoDB存储引擎在MySQL中,不同存储引擎对索引的实现方式是不同的,总结下MyISAM和InnoDB两个存储引擎的索引实现方式.MyISAM引擎使用B+Tree作为索 ...