POJ 3984:迷宫问题 bfs+递归输出路径
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 11844 | Accepted: 7094 |
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
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 <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#pragma warning(disable:4996)
using namespace std; int value[7][7];
int pre[7][7];
int flag[7][7]; void bfs()
{
memset(flag,0,sizeof(flag));
memset(pre,-1,sizeof(pre)); queue<int>x;
queue<int>y; while(x.size())x.pop();
while(y.size())y.pop(); x.push(0);
y.push(0); int a;
int b;
while(x.size())
{
a=x.front();
b=y.front(); x.pop();
y.pop(); flag[a][b]=1; if(a>0&&flag[a-1][b]==0&&value[a-1][b]==0)
{
pre[a-1][b]=a*10+b;
x.push(a-1);
y.push(b);
}
if(b<4&&flag[a][b+1]==0&&value[a][b+1]==0)
{
pre[a][b+1]=a*10+b;
x.push(a);
y.push(b+1);
}
if(b>0&&flag[a][b-1]==0&&value[a][b-1]==0)
{
pre[a][b-1]=a*10+b;
x.push(a);
y.push(b-1);
}
if(a<4&&flag[a+1][b]==0&&value[a+1][b]==0)
{
pre[a+1][b]=a*10+b;
x.push(a+1);
y.push(b);
}
} } void prin(int x,int y)
{
if(pre[x][y]!=-1)
{
int y_pr=pre[x][y]%10;
int x_pr=pre[x][y]/10;
prin(x_pr,y_pr);
}
cout<<"("<<x<<", "<<y<<")"<<endl;
} int main()
{
int i,j;
for(i=0;i<=4;i++)
{
for(j=0;j<=4;j++)
{
cin>>value[i][j];
}
} bfs();
prin(4,4); return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3984:迷宫问题 bfs+递归输出路径的更多相关文章
- 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, ...
- poj 3984 迷宫问题 bfs
学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...
- POJ 3984 迷宫问题 (BFS + Stack)
链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ...
- POJ - 3984迷宫问题(最短路径输出)
题目链接:http://poj.org/problem?id=3984 题目: 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- POJ 3984 迷宫问题 bfs 难度:0
http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...
- [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
- POJ - 3984 迷宫问题 bfs解法
#include<stdio.h> #include<string.h> #include<algorithm> #include<stack> usi ...
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
随机推荐
- redis api-set
- java 加法变乘法
加法变乘法 我们都知道:1+2+3+ - + 49 = 1225 (1) 现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015 比如: 1+2+3+...+10*11+12+...+27*28 ...
- 非极大抑制睔PYTHON实现
非极大抑制(Non-maximum suppression)python代码实现原创Butertfly 发布于2018-11-20 18:48:57 阅读数 293 收藏展开定位一个物体,最后算法就找 ...
- 关于ESP8266和ESP8285的对比
ESP8285=ESP8266+1M Flash. 与ESP8266相比,其能耐高温达125摄氏度!且原有ESP8266源码程序可以原封不动移植使用.ESP-M1/M2 模块核心处理器采用高性价比芯片 ...
- 031、Java中偶数偶数的判断方法
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- CSU 1216 异或最大值
求n个数里面,求两两异或的最大值 直接来肯定会超时 既然要异或最大值,那么两个数的二进制肯定是正好错开为好...为了能快速找到错开的数,确实有点难想到,用字典树,按二进制数插入,再一个一个在字典树里面 ...
- maven详解 之 pom.xml
Maven 一个项目管理工具 其作用就是用来管理jar 包的 maven的核心 pom.xml配置文件 <project xmlns="http://maven.apache ...
- SpringBoot-配置Java方式
SpringBoot中使用Java方式配置步骤如下: 在类上加入@Configuration注解,代表作为配置类 在该类方法上加入@Bean注解,代表将方法返回的Bean加入Spring容器 在该类中 ...
- 《Airbnb架构要点分享》阅读笔记
Airbnb成立于2008年8月,总部位于加利福尼亚州旧金山市.Airbnb是一个值得信赖的社区型市场,在这里人们可以通过网站.手机或平板电脑发布.发掘和预订世界各地的独特房源,其业务已经覆盖190个 ...
- Easy Climb UVA - 12170 滚动dp +离散化+ 单调队列优化
E.Easy Climb Somewhere in the neighborhood we have a very nice mountain that gives a splendid view o ...