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 + 记录路径)的更多相关文章

  1. - 迷宫问题 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, ...

  2. 迷宫问题 POJ - 3984 (搜索输出路径)

    题目大意 题目不需要大意,poj居然还有中文题 鸣谢 特别鸣谢ljc大佬提供的方法!!! 解法 我们可能输出个最短路径的长度比较简单,但是输出最短路径真的是没有做过,这里有一种简单的方法 因为我们的d ...

  3. BFS和DFS记录路径

    DFS记录路径的意义好像不大,因为不一定是最短的,但是实现起来却很简单. #include<math.h> #include<stdio.h> #include<queu ...

  4. 哈密顿绕行世界问题(dfs+记录路径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181 哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) ...

  5. 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, ...

  6. 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, ...

  7. 迷宫问题 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, ...

  8. 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 ...

  9. HDOJ-1043 Eight(八数码问题+双向bfs+高效记录路径+康拓展开)

    bfs搜索加记录路径 HDOJ-1043 主要思路就是使用双向广度优先搜索,找最短路径.然后记录路径,找到结果是打印出来. 使用康拓序列来来实现状态的映射. 打印路径推荐使用vector最后需要使用a ...

随机推荐

  1. python实验一

    安徽工程大学 Python程序设计实验报告 班级物流管理191 姓名彭艺    学号3190505139成绩          日期  2020年3月3日     指导老师    修宇 实验名称    ...

  2. Mac 下 Docker 运行较慢的原因分析及个人见解

    在mac 使用 docker 的时候,我总感觉程序在 docker 下运行速度很慢,接下来我一一分析我遇到的问题,希望大家能进行合理的讨论和建议. 问题: valet 下打开 laravel 首页耗时 ...

  3. etcdctl的使用

    etcdctl是一个提供简洁命令的etcd客户端,使用etcdctl可以直接和etcd服务打交道,对etcd中的键值对进行增删改查. 安装etcdctl 下载etcdctl工具 下载地址:etcdct ...

  4. 【原创】(五)Linux进程调度-CFS调度器

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...

  5. CSS核心概念之盒子模型

    盒子模型(Box Model) 关于更多CSS核心概念的文章请关注GitHub--CSS核心概念. 当对一个文档进行布局的时候,浏览器的渲染引擎会根据标准之一的 CSS 基础框盒模型(CSS basi ...

  6. C++ 小练习,一个整型数字的处理

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> //输入一个任意位数的int整数,并判断该整数的位数,并输出每一位数(每个数字中间用空 ...

  7. 单选框 改成 复选框 的css样式

    fillEditorFakeTable.less /* add for the global title checkbox fake */ .fake-checkbox { display: inli ...

  8. 网络安全从入门到精通(第二章-3)后端基础SQL— MySQL高级查询与子查询

    本文内容: MySQL的基础查询语句 链接查询 联合查询 子查询 渗透测试常用函数 1,MySQL基础查询语句: select * from 表 order  by ASC/DESC; ASC:从小到 ...

  9. SpringMVC框架——文件的上传与下载

    使用SpringMVC框架做个小练习,需求: 1.单个图片上传并显示到页面中: 2.多个图片上传并显示到页面中: 3.上传文件后下载文件: 1.pom.xml中添加依赖 <!-- 文件上传 -- ...

  10. IOS(苹果手机)使用video播放HLS流,实现在内部播放及全屏播放(即非全屏和全屏播放)。

    需求: 实现PC及移动端播放HLS流,并且可以自动播放,在页面内部播放及全屏播放功能. 初步:PC及安卓机使用hls.js实现hls流自动播放及全屏非全屏播放 首先使用了hls.js插件,可以实现在P ...