问题:

定义一个二维数组: 

  1. int maze[5][5] = {

  2. 0, 1, 0, 0, 0,

  3. 0, 1, 0, 1, 0,

  4. 0, 0, 0, 0, 0,

  5. 0, 1, 1, 1, 0,

  6. 0, 0, 0, 1, 0,

  7. };

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

输入:

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

输出:

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

  1. //#include <bits/stdc++.h>
  2. #include <stdio.h>
  3. #include <cmath>
  4. #include <queue>
  5. #define inf 0x3f3f3f3f
  6. #define FRE() freopen("in.txt", "r", stdin)
  7. #define FRO() freopen("out.txt", "w", stdout)
  8.  
  9. using namespace std;
  10. typedef long long ll;
  11. const int maxn = ;
  12. int dx[]={,,,-};
  13. int dy[]={,-,,};
  14. int mp[maxn][maxn];
  15. struct Node
  16. {
  17. int x,y;
  18. Node(int _x,int _y):x(_x),y(_y){}
  19. Node(){}
  20. };
  21. Node path[maxn][maxn];
  22. queue<Node> que;
  23.  
  24. bool isin(int x,int y)
  25. {
  26. if(x>= && x< && y>= && y<)
  27. return true;
  28. return false;
  29. }
  30.  
  31. void BFS()
  32. {
  33. Node now = Node(,);
  34. que.push(now);
  35. mp[][]=;
  36.  
  37. while(!que.empty())
  38. {
  39. Node u = que.front();
  40. que.pop();
  41. if(u.x== && u.y==) return;
  42. for(int i=; i<; i++)
  43. {
  44. int tx = u.x+dx[i];
  45. int ty = u.y+dy[i];
  46. if(isin(tx,ty) && mp[tx][ty]==)
  47. {
  48. que.push(Node(tx,ty));
  49. mp[tx][ty] = ;
  50. path[tx][ty] = u;
  51. }
  52. }
  53.  
  54. }
  55.  
  56. }
  57.  
  58. void showPath(Node u)
  59. {
  60. if(u.x== && u.y==) printf("(%d, %d)\n",u.x,u.y);
  61. else
  62. {
  63. showPath(path[u.x][u.y]);
  64. printf("(%d, %d)\n",u.x,u.y);
  65. }
  66. }
  67.  
  68. int main()
  69. {
  70. for(int i=; i<; i++)
  71. {
  72. for(int j=; j<; j++)
  73. {
  74. scanf("%d",&mp[i][j]);
  75. }
  76. }
  77.  
  78. BFS();
  79. showPath(Node(,));
  80. return ;
  81. }
  82. /*
  83. Sample Input:
  84. 0 1 0 0 0
  85. 0 1 0 1 0
  86. 0 0 0 0 0
  87. 0 1 1 1 0
  88. 0 0 0 1 0
  89.  
  90. Sample Output:
  91. (0, 0)
  92. (1, 0)
  93. (2, 0)
  94. (2, 1)
  95. (2, 2)
  96. (2, 3)
  97. (2, 4)
  98. (3, 4)
  99. (4, 4)
  100. */

POJ-3984 迷宫问题(BFS找最短路径并保存)的更多相关文章

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

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

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

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

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

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

  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 + Stack)

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

  6. poj 3984 迷宫问题 bfs

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

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

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

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

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

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

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

随机推荐

  1. Pytorch【直播】2019 年县域农业大脑AI挑战赛---初级准备(一)切图

    比赛地址:https://tianchi.aliyun.com/competition/entrance/231717/introduction 这次比赛给的图非常大5万x5万,在训练之前必须要进行数 ...

  2. 使用IDEA,Eclispe搭建Spring Boot项目

    如何创建一个Spring Boot项目?这里使用maven来进行依赖管理,根据常用的IDE,可以使用IDEA.Eclipse.或者访问官方网站搭建. 项目搭建环境准备 JDK:1.8 MAVEN:3. ...

  3. 在React中随机生成图形验证码

    各个方法 在输入框中定义一个位置存放图形 完整代码 方便复制粘贴 import React, { Component } from 'react'; import styles from './lef ...

  4. Html5 -- 语义标签兼容性处理

    方法一:通过js处理 方法二:完美的处理方式 no.1 !--[if lte IE 8]> <script type="text/javascript" src=&qu ...

  5. 2017 青岛现场赛 I The Squared Mosquito Coil

    Lusrica designs a mosquito coil in a board with n × n grids. The mosquito coil is a series of consec ...

  6. Vue中 onmouseenter,onmouseleave,onmouseover,onmouseout的区别

    今天在学Vue视频的时候,提到了这四个触发事件,我就想做下笔记: 1.onmouseenter和onmouseleave是一组:当鼠标进入指定区域的时候触发,但是不支持冒泡,进入或者离开子组件都不触发 ...

  7. C++ 11 :override 关键字的使用

    override 关键字 作用:在成员函数声明或定义中, override 确保该函数为虚函数并覆写来自基类的虚函数. 位置:函数调用运算符之后,函数体或纯虚函数标识 "= 0" ...

  8. POJ 3349:Snowflake Snow Snowflakes 六片雪花找相同的 哈希

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 35642   Accep ...

  9. Linux-initramfs

    1. 内核启动问题2. 解决方案2.1 ramdisk(比如initrd)2.2 tmpfs(比如initramfs)2.3 ramdisk vs ramfs2.4 临时文件系统2.4.1 观察tmp ...

  10. Http与Https协议规范

    HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1.0的第 ...