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数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
随机推荐
- Day9 - K - Yue Fei's Battle HDU - 5136
Yue Fei is one of the most famous military general in Chinese history.He led Southern Song army in t ...
- MySQL多实例安装(相同版本)
本文以MySQL5.7为例,介绍在同一台机器下如何安装多个MySQL实例. 环境:RHEL 6.5 + MySQL 5.7 1.单实例MySQL安装 2.多实例MySQL配置 3.多实例MySQL初始 ...
- Zero 初识Sciter
在浏览有关Sciter技术前,您需要花点时间浏览以下内容. 您是否需要花时间学习Sciter? 如果您的工作或您想从事的工作与桌面应用开发无关,那么您不需要学习Sciter. 如果您不认同HTML\C ...
- 实战mysql存储程序与定时器
home198979 实战mysql存储程序与定时器 博客分类: mysql 存储过程定时器eventprocedure实战 需求:一个庞大的日志表,现每天做定时统计一天的总数,放另一个表中,方便查 ...
- 如何使用Nexus搭建Maven私服
如何使用Nexus搭建Maven私服 听语音 | 浏览:47 | 更新:2016-09-29 10:22 1 2 3 4 5 6 7 分步阅读 一键约师傅 百度师傅最快的到家服务,最优质的电脑清灰! ...
- IEEE Spectrum 2014年十大编程语言盘点
近日,IEEE Spectrum推出 了一个最流行的编程语言排行榜.排行榜筛选了 12 项指标,综合了 10 个来源(含 IEEE Xplore.Google.GitHub)的数据,最终评选出了下面这 ...
- 给服务器做pve系统(可以通过web管理物理机集群资源与虚拟机)
做此系统前,可以先进入bios,设置一下ipmi的网络地址.可以远程管理服务器 输入服务器的ipmi里面配置的ip 默认账号与密码admin 点击launch 会自动下载认证文件 下载好java软件环 ...
- HTML5学习第四天
HTML5学习第四天 一.HTML列表 HTML列表,有无序表,有序表以及自定义表,列表于列表之间可以实现嵌套 列表相关操作 <ul> <li>(多选)谁世界第二可爱?< ...
- 小程序真机上报错 for developer: some selectors are not allowed in component wxss , including tag name selectors, id selectors, and attribute selectors
for developer: some selectors are not allowed in component wxss , including tag name selectors, id s ...
- 在 Scale Up 中使用 Health Check【转】
对于多副本应用,当执行 Scale Up 操作时,新副本会作为 backend 被添加到 Service 的负载均衡中,与已有副本一起处理客户的请求.考虑到应用启动通常都需要一个准备阶段,比如加载缓存 ...