迷宫问题
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14568   Accepted: 8711

Description

定义一个二维数组:

  1. int maze[5][5] = {
  2.  
  3. 0, 1, 0, 0, 0,
  4.  
  5. 0, 1, 0, 1, 0,
  6.  
  7. 0, 0, 0, 0, 0,
  8.  
  9. 0, 1, 1, 1, 0,
  10.  
  11. 0, 0, 0, 1, 0,
  12.  
  13. };

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

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

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

  1. 0 1 0 0 0
  2. 0 1 0 1 0
  3. 0 0 0 0 0
  4. 0 1 1 1 0
  5. 0 0 0 1 0

Sample Output

  1. (0, 0)
  2. (1, 0)
  3. (2, 0)
  4. (2, 1)
  5. (2, 2)
  6. (2, 3)
  7. (2, 4)
  8. (3, 4)
  9. (4, 4)

这题的状态记录和上一题的不太一样,上一题是不需要回溯的,这题需要回溯,因此不再是简单地记录上一个节点的状态,而是每一个点都记录前驱点,这样根据终点可以回溯到起点。显然每一个点可以向四个方向拓展,因此一个点最多可以成为四个拓展点的前驱点,但这不意味着一个点同时存在四种状态,而是四种状态被分开来压入队列。以前在这点上一直搞不清楚,这题就不会做。由于BFS找到的是最短路,且每一个点只能被访问一次,因此回溯的时候一定是一条最短的路。本来想用另一种方法用下标回溯状态,但是发现每一种状态展开后会叠在前一种后面,需要好几个辅助容器,比较麻烦。还是用一般方法

代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdlib>
  4. #include<sstream>
  5. #include<cstring>
  6. #include<cstdio>
  7. #include<string>
  8. #include<deque>
  9. #include<stack>
  10. #include<cmath>
  11. #include<queue>
  12. #include<set>
  13. #include<map>
  14. using namespace std;
  15. #define INF 0x3f3f3f3f
  16. #define MM(x) memset(x,0,sizeof(x))
  17. #define MMINF(x) memset(x,INF,sizeof(x))
  18. typedef long long LL;
  19. const double PI=acos(-1.0);
  20. struct info
  21. {
  22. int x;
  23. int y;
  24. int prex;
  25. int prey;
  26. };
  27. info direct[4]={{0,1},{0,-1},{1,0},{-1,0}};
  28. info operator+(const info &a,const info &b)
  29. {
  30. info c;
  31. c.x=a.x+b.x;
  32. c.y=a.y+b.y;
  33. return c;
  34. }
  35. int pos[5][5],vis[5][5];
  36. info history[5][5]={{0,0,-1,-1}};
  37. inline bool check(const info &a)
  38. {
  39. if((!vis[a.x][a.y])&&(!pos[a.x][a.y])&&(a.x>=0&&a.y>=0&&a.x<5&&a.y<5))
  40. return true;
  41. return false;
  42. }
  43. int main(void)
  44. {
  45. int i,j;
  46. for (i=0; i<5; i++)
  47. {
  48. for (j=0; j<5; j++)
  49. {
  50. scanf("%d",&pos[i][j]);
  51. }
  52. }
  53. queue<info>Q;
  54. Q.push(history[0][0]);
  55. vis[history[0][0].x][history[0][0].y]=1;
  56. while (!Q.empty())
  57. {
  58. info now=Q.front();
  59. Q.pop();
  60. for (int i=0; i<4; i++)
  61. {
  62. info v=now+direct[i];
  63. v.prex=now.x;
  64. v.prey=now.y;
  65. if(check(v))
  66. {
  67. Q.push(v);
  68. vis[v.x][v.y]=1;
  69. history[v.x][v.y]=v;
  70. }
  71. }
  72. if(now.x==4&&now.y==4)
  73. break;
  74. }
  75. stack<info>ans;
  76. info k=history[4][4];
  77. puts("(0, 0)");
  78. while (k.prex!=-1)
  79. {
  80. ans.push(k);
  81. k=history[k.prex][k.prey];
  82. }
  83. while (!ans.empty())
  84. {
  85. info t=ans.top();
  86. printf("(%d, %d)\n",t.x,t.y);
  87. ans.pop();
  88. }
  89. return 0;
  90. }

POJ——3984迷宫问题(BFS+回溯)的更多相关文章

  1. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  2. POJ 3984 迷宫问题 bfs 难度:0

    http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...

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

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

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

  5. poj 3984 迷宫问题 bfs

    学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...

  6. POJ - 3984 迷宫问题 bfs解法

    #include<stdio.h> #include<string.h> #include<algorithm> #include<stack> usi ...

  7. POJ 3984 迷宫问题 (BFS + Stack)

    链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ...

  8. BFS(最短路+路径打印) POJ 3984 迷宫问题

    题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...

  9. POJ 3984 迷宫问题(简单bfs+路径打印)

    传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  10. POJ 3984 迷宫问题

    K - 迷宫问题 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

随机推荐

  1. Android 8.0 NotificationChannel 采坑实例

    Android O 上Notification的新特性: 通知通道功能 1. 简介: 通知通道功能使开发者管理自己应用的通知成为一个组或者一个通道,用户可以通过通知通道完成设置通知,如:阻止所有通知, ...

  2. uvm_pkg——老板,打包带走

    Thus spake the master programmer: “After three day without programming, life becomes meaningless.” 编 ...

  3. jmeter中登录和提交收银出现的错误

    登录出现的错误 登录界面如图所示: 为了防止登录跳转的问题response code 302的问题,要设置 2.提交收银界面 当系统设置必须传送jison格式时,要在HTTP Header Manag ...

  4. nginx 编译某个模板的问题./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library stati

    root@hett-PowerEdge-T30:/usr/local/src/nginx-1.9.8# ./configure --prefix=/usr/local/nginx  --add-mod ...

  5. shell 简单脚本 2

    #!/bin/bash source /etc/profile APPLICATIONS_HOME="/cpic/cpicapp/cpic_analy/jars" APPLICAT ...

  6. java日期操作的工具类时间格式的转换

    package cn.itcast.oa.util; import java.text.ParseException; import java.text.SimpleDateFormat;import ...

  7. 一个batch如何通过一个网络

    一个batch下所有的图片一起经过整个网络,不是说一张图片经过网络后再让下一张进入网络,这样一个batch一起通过网络计算速度比一张一张这样快

  8. Jordan 标准型的实例

    将学习到什么 练习一下如何把一个矩阵化为 Jordan 标准型.   将矩阵化为 Jordan 标准型需要三步: 第一步 求出矩阵 \(A \in M_n\) 全部的特征值 \(\lambda_1,\ ...

  9. apache shiro的工作流程分析

    本文基于shiro的web环境,用宏观(也就是不精确)的角度去理解shiro的工作流程,先看shiro官方的一张图. 和应用程序直接交互的对象是Subject,securitymanager为Subj ...

  10. jQuery-AJAX简介

    AJAX是浏览器后台与服务器交换数据的技术,无须加载整个页面的情况下,对页面中的局部进行更新. AJAX=异步的JavaScript与XML(Asynchronous JavaScript and X ...