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. "长辈牌"电子产品:有一种评论朋友圈叫给你打电话

    一.长辈们使用电子产品的姿势集合 先问你一个问题:「怎么下载搜狗输入法?」 (非广告) 摁?看到这篇文章的你可能都有点懵,不就下载安装就完了吗?但是,真的就只是这样吗? 前一段时间,当家里的长辈问到我 ...

  2. 数据库--Redis

    原因: 源码是官方configure过的,但官方configure时,生成的文件有时间戳信息,所以如果你的虚拟机的时间不对,比如说是2022年,就可能会出错 解决: date -s ‘yyyy-mm- ...

  3. Element-UI饿了么时间组件控件按月份周日期,开始时间结束时间范围限制参数

    在日常开发中,我们会遇到一些情况,在使用Element-UI 限制用户的日期时间范围的选择控制(例如:查询消息开始和结束时间,需要限制不能选择今天之后的时间). 看了网上的一些文档,零零散散.各式各样 ...

  4. DEV GridControl控件使用(CheckBox全选、操作按钮、事件处理,获取值)

    1.GridControl控件使用 (1)绑定数据源 //绑定DataTable gridControl1.DataSource = DbHelper.ExecuteDataTable("S ...

  5. 用libvlc 抓取解码后的帧数据

    vlc是一套优秀的开源媒体库,其特点是提供了完整的流媒体框架, 用它可以非常方便的实现抓取解码帧的功能. 与此功能有关的关键API为 libvlc_video_set_callbacks /*设置回调 ...

  6. 一键配置openstack-cata版的在线yum源

    下面脚本可以直接复制来配置openstack-ocata版的yum源: echo "nameserver 8.8.8.8 nameserver 119.29.29.29 nameserver ...

  7. js 运动的应用 新浪微博

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. (转)const的内部链接属性(C++中适用)

    转载自:http://xiangwangfeng.com/2011/05/02/const%E7%9A%84%E5%86%85%E9%83%A8%E9%93%BE%E6%8E%A5%E5%B1%9E% ...

  9. 初识Flask、快速启动

    目录 一.初识Flask 1.1 什么是flask? 1.2 为什么要有flask? 二.Flask快速启动 一.初识Flask 1.1 什么是flask? Flask 本是作者 Armin Rona ...

  10. 在d盘创建文件夹,里面有aaa.txt/bbb.txt/ccc.txt,然后遍历出aaa文件夹下的文件(新手)

    //导入的包.import java.io.File;import java.io.IOException;//创建的一个类.public class zy { //公共静态的主方法. public ...