Q - 迷宫问题 POJ - 3984(BFS / DFS + 记录路径)
Q - 迷宫问题 POJ - 3984
定义一个二维数组:
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)
题解一
//深搜版
#include<iostream>
#include<stack>
using namespace std;
const int Len = 5;
int map[Len][Len];
int mark[Len][Len];
int mov[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
int s_x = 0,s_y = 0,e_x = 4,e_y = 4;
struct Node
{
int x,y;
}tem;
stack<Node> st;
bool is_good(int x,int y)
{
if(x >= 0 && y >= 0 && x < 5 && y < 5 && ! mark[x][y] && map[x][y] != 1)
return true;
return false;
}
int flag = 0;
void dfs(int x,int y)
{
if(x == e_x && y == e_y)
{
flag = 1;
//printf("(%d,%d)\n",x,y);
tem.x = x,tem.y = y;
st.push(tem);
return;
}
for(int i = 0; i < 4; i ++)
{
int xx = x + mov[i][0];
int yy = y + mov[i][1];
if(is_good(xx , yy))
{
mark[xx][yy] = 1;
dfs(xx , yy);
if(flag == 1)
{
//printf("(%d,%d)\n",x,y);
tem.x = x,tem.y = y;
st.push(tem);
return;
}
mark[xx][yy] = 0;
}
}
}
int main()
{
//freopen("T.txt","r",stdin);
for(int i = 0; i < Len; i ++)
for(int j = 0; j < Len; j ++)
scanf("%d", &map[i][j]);
dfs(s_x,s_y);
while(! st.empty())
{
tem = st.top();
st.pop();
if(!st.empty())
printf("(%d, %d)\n",tem.x,tem.y);
else
printf("(%d, %d)",tem.x,tem.y);
}
return 0;
}
题解二
//深搜版
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
const int Len = 5;
int map[Len][Len];
int mark[Len][Len];
int mov[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
int s_x = 0,s_y = 0,e_x = 4,e_y = 4;
struct Node
{
int x,y;
}tem,s,e,node[Len][Len];
stack<Node> st;
queue<Node> q;
bool is_good(int x,int y)
{
if(x >= 0 && y >= 0 && x < 5 && y < 5 && ! mark[x][y] && map[x][y] != 1)
return true;
return false;
}
int flag = 0;
void print(int x,int y)
{
if(node[x][y].x == x && node[x][y].y == y)
{
printf("(%d, %d)\n",x,y);
return;
}
print(node[x][y].x , node[x][y].y);
printf("(%d, %d)\n",x,y);
}
void bfs()
{
//初始化、赋值、做标记、压队列
s.x = s_x;
s.y = s_y;
node[s.x][s.y].x = s.x;
node[s.x][s.y].y = s.y;
mark[s.x][s.y] = 1;
q.push(s);
while(! q.empty())
{
s = q.front();
q.pop();
if(s.x == e_x && s.y == e_y)
{
print(s.x , s.y);
return;
}
for(int i = 0; i < 4; i ++)
{
e.x = s.x + mov[i][0];
e.y = s.y + mov[i][1];
if(is_good(e.x , e.y))
{
node[e.x][e.y].x = s.x;
node[e.x][e.y].y = s.y;
mark[e.x][e.y] = 1;
q.push(e);
}
}
}
}
int main()
{
//freopen("T.txt","r",stdin);
for(int i = 0; i < Len; i ++)
for(int j = 0; j < Len; j ++)
scanf("%d", &map[i][j]);
bfs();
return 0;
}
Q - 迷宫问题 POJ - 3984(BFS / DFS + 记录路径)的更多相关文章
- - 迷宫问题 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, 0, 1, ...
- 迷宫问题 POJ - 3984 (搜索输出路径)
题目大意 题目不需要大意,poj居然还有中文题 鸣谢 特别鸣谢ljc大佬提供的方法!!! 解法 我们可能输出个最短路径的长度比较简单,但是输出最短路径真的是没有做过,这里有一种简单的方法 因为我们的d ...
- BFS和DFS记录路径
DFS记录路径的意义好像不大,因为不一定是最短的,但是实现起来却很简单. #include<math.h> #include<stdio.h> #include<queu ...
- 哈密顿绕行世界问题(dfs+记录路径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181 哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) ...
- POJ 3984(DFS入门题 +stack储存路径)
POJ 3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...
- K - 迷宫问题 POJ - 3984
定义一个二维数组: 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, ...
- 迷宫问题 POJ - 3984 [kuangbin带你飞]专题一 简单搜索
定义一个二维数组: 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, ...
- PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs,dfs记录路径,做了两天)
1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides ...
- HDOJ-1043 Eight(八数码问题+双向bfs+高效记录路径+康拓展开)
bfs搜索加记录路径 HDOJ-1043 主要思路就是使用双向广度优先搜索,找最短路径.然后记录路径,找到结果是打印出来. 使用康拓序列来来实现状态的映射. 打印路径推荐使用vector最后需要使用a ...
随机推荐
- (26)ASP.NET Core EF保存(基本保存、保存相关数据、级联删除、使用事务)
1.简介 每个上下文实例都有一个ChangeTracker,它负责跟踪需要写入数据库的更改.更改实体类的实例时,这些更改会记录在ChangeTracker中,然后在调用SaveChanges时会被写入 ...
- PHP5.6.23+Apache2.4.20+Eclipse for PHP 4.5开发环境配置
一.Apache配置(以httpd-2.4.20-x64-vc14.zip为例)(http://www.apachelounge.com/download/) 1.安装运行库vc11和vc14 2.解 ...
- Prometheus 监控平台的搭建
1. 环境准备 两台ubuntu 16.04 服务器内网IP 作用 安装软件 172.16.4.11 监控的服务端 Prometheus( ...
- openwrt sdk 编译工具 及 hello world
需要先在 make menuconfig 中打开 sdk make -j4在 bin\targets\ramips\mt7620生成一个openwrt-sdk-ramips-mt7620_gcc-7. ...
- R自带数据集
向量 euro #欧元汇率,长度为11,每个元素都有命名landmasses #48个陆地的面积,每个都有命名precip #长度为70的命名向量rivers #北美141条河流长 ...
- 单元测试 - Tests和UITests (一) 业务测试
单元测试 假如我们今天去面试了,面试官问了一句“什么是单元测试?有没有使用?大概是针对那些情况进行单测的?单测意义从你实际使用中总结一下.” 这要在我没进行现在的单测之前这个问题我回答的可能就是“不好 ...
- linux 执行计划任务crontab
crontab 一些常用的命令 service crond start //启动服务 service crond stop //关闭服务 service crond restart //重启服务 se ...
- Python - 面向对象(二)类方法、静态方法
面向对象的各种方法 静态方法 - @staticmethod class Person(): name = "cool guy" @staticmethod def static( ...
- [剑指offer]6.从尾到头打印链表+18.删除链表节点
链表 6.从尾到头打印链表 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回). 方法一 迭代 创建空列表res,将链表值head.val依次存进res,返回翻转后的res 代码 cl ...
- CBV和APIView源码分析
CBV源码分析 查看源码的方式,先查看自身,没有去找父类,父类没有就去找父父类... 自己定义的类 class Author(View): def get(self,request): back_di ...