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

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找到的是最短路,且每一个点只能被访问一次,因此回溯的时候一定是一条最短的路。本来想用另一种方法用下标回溯状态,但是发现每一种状态展开后会叠在前一种后面,需要好几个辅助容器,比较麻烦。还是用一般方法

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
typedef long long LL;
const double PI=acos(-1.0);
struct info
{
int x;
int y;
int prex;
int prey;
};
info direct[4]={{0,1},{0,-1},{1,0},{-1,0}};
info operator+(const info &a,const info &b)
{
info c;
c.x=a.x+b.x;
c.y=a.y+b.y;
return c;
}
int pos[5][5],vis[5][5];
info history[5][5]={{0,0,-1,-1}};
inline bool check(const info &a)
{
if((!vis[a.x][a.y])&&(!pos[a.x][a.y])&&(a.x>=0&&a.y>=0&&a.x<5&&a.y<5))
return true;
return false;
}
int main(void)
{
int i,j;
for (i=0; i<5; i++)
{
for (j=0; j<5; j++)
{
scanf("%d",&pos[i][j]);
}
}
queue<info>Q;
Q.push(history[0][0]);
vis[history[0][0].x][history[0][0].y]=1;
while (!Q.empty())
{
info now=Q.front();
Q.pop();
for (int i=0; i<4; i++)
{
info v=now+direct[i];
v.prex=now.x;
v.prey=now.y;
if(check(v))
{
Q.push(v);
vis[v.x][v.y]=1;
history[v.x][v.y]=v;
}
}
if(now.x==4&&now.y==4)
break;
}
stack<info>ans;
info k=history[4][4];
puts("(0, 0)");
while (k.prex!=-1)
{
ans.push(k);
k=history[k.prex][k.prey];
}
while (!ans.empty())
{
info t=ans.top();
printf("(%d, %d)\n",t.x,t.y);
ans.pop();
}
return 0;
}

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. MySQL常用命令和语句

    1.常用SQL语句 1)常用函数/*type可取值为:MICROSECONDSECONDMINUTEHOURDAYWEEKMONTHQUARTERYEARSECOND_MICROSECONDMINUT ...

  2. SAP产品的Field Extensibility

    SAP开发人员的工作职责,除了实现软件的功能性需求外,还会花费相当的精力实现一些非功能性需求,来满足所谓的SAP Product Standard(产品标准).这些产品标准,包含在SAP项目实施中大显 ...

  3. ftp - Internet 文件传输程序 (file transfer program)

    概述 (SYNOPSIS) ftp [-pinegvd ] [host ] pftp [-inegvd ] [host ] 说明 (DESCRIPTION) 用户通过 Ftp 这个程序来使用 Inte ...

  4. 线程锁(互斥锁Mutex)

    线程锁(互斥锁Mutex) 一个进程下可以启动多个线程,多个线程共享父进程的内存空间,也就意味着每个线程可以访问同一份数据,此时,如果2个线程同时要修改同一份数据,会出现什么状况? # -*- cod ...

  5. pytorch中的view

    https://ptorch.com/news/59.html view()相当于reshape(),其中参数若为-1表示当前的size根据其余size推断

  6. null 理解

    值 null 特指对象的值未设置.它是 JavaScript 基本类型 之一. 语法节 null 描述节 值 null 是一个字面量,它不像undefined 是全局对象的一个属性.null 是表示缺 ...

  7. jquery动态实现填充下拉框

    当点下拉框时动态加载后台数据. 后台代码 protected void doPost(HttpServletRequest request, HttpServletResponse response) ...

  8. thinkphp 结合phpexcel实现excel导入

    控制器文件: class ExcelAction extends Action { public function __construct() { import('ORG.Util.ExcelToAr ...

  9. SVN的使用二

    一,打开SCM 在xcode中,点击菜单: File -> Source Control –> Repositories 二,连接SVN服务器 1, 2,配置SVN服务器地址(http:/ ...

  10. CentOS7支持中文显示

    1.查看系统是否安装有中文语言包 locale -a | grep "zh_CN"     命令含义:列出所有可用的公共语言环境的名称,包含有"zh_CN" 若 ...