迷宫问题(bfs的应用)
问题描述:
定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示:
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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。入口点为[0,0],既第一空格是可以走的路。
Input
一个N × M的二维数组,表示一个迷宫。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。
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)
使用广度搜素,第一个找到出口的路径一定是最短路径。
搜索过程中使用 point pre[][]数据记录上一坐标的位置,用来保存路径,这样就可以从pre[m][n]往回找寻路径,一直找到pre[0][0]。
搜索过程中可以需要改变迷宫数组mn为第三种状态,以防止重复搜索。相当于一般用法中自己定义visited数组了。
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
//定义坐标
struct point
{
int x;
int y;
}; int mn[][];//记录迷宫状态:0可以访问,1不能访问,-1已经访问过
point pre[][];//记录上一个访问的坐标
point mov[]={{-,},{,-},{,},{,}}; //表示坐标的移动方向 bool bfs(int m,int n,int x,int y)
{
queue<point> zb;
point temp;
temp.x=x;
temp.y=y;
zb.push(temp);
mn[x][y]=-;
while(!zb.empty())
{
point top;
top=zb.front();
zb.pop();
for(int i=;i<;i++)
{
if(top.x+ mov[i].x==m- && top.y+mov[i].y==n-)
{
mn[top.x+ mov[i].x][top.y+mov[i].y]=-;
pre[top.x+ mov[i].x][top.y+mov[i].y]=top;
return true;
}
if(mn[top.x+ mov[i].x][top.y+mov[i].y]==)
{
mn[top.x+ mov[i].x][top.y+mov[i].y]=-;
pre[top.x+ mov[i].x][top.y+mov[i].y]=top;
point t;
t.x=top.x+ mov[i].x;
t.y=top.y+mov[i].y;
zb.push(t);
}
}
}
return false;
} int main()
{
int m,n;
cin>>m>>n;
for(int i=;i<m;i++)
{
for(int j=;j<n;j++)
{
cin>>mn[i][j];
}
}
stack<point>st;
if(bfs(m,n,,))
{
point temp;
temp.x=m-;
temp.y=n-;
while(!(temp.x== && temp.y==) )
{
st.push(temp);
temp=pre[temp.x][temp.y];
}
temp.x=;
temp.y=;
st.push(temp);
while(!st.empty())
{
cout<<'('<<st.top().x<<','<<st.top().y<<')'<<endl;
st.pop();
}
}
return ;
}
迷宫问题(bfs的应用)的更多相关文章
- 迷宫问题(bfs)
import java.util.LinkedList; import java.util.Queue; import java.util.Stack; public class BFS { priv ...
- ZZULIOJ 1726 迷宫(BFS+小坑)
1726: 迷宫 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 394 Solved: 64 SubmitStatusWeb Board Descr ...
- HDU 1728 逃离迷宫(BFS)
Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有 ...
- HDU 1728:逃离迷宫(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有 ...
- [Swust OJ 409]--小鼠迷宫问题(BFS+记忆化搜索)
题目链接:http://acm.swust.edu.cn/problem/409/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- 问题 1923: [蓝桥杯][算法提高VIP]学霸的迷宫 (BFS)
题目链接:https://www.dotcpp.com/oj/problem1923.html 题目描述 学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗.但学霸为了不要别人打扰,住在 ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- 迷宫问题bfs, A Knight's Journey(dfs)
迷宫问题(bfs) POJ - 3984 #include <iostream> #include <queue> #include <stack> #incl ...
- POJ-3984.迷宫问题(BFS + 路径输出)
昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了.流泪.jpg 本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径. 本题思路: ...
随机推荐
- Oracle 存储过程实例
create or replace procedure PCREPORT is startDate DATE; --起始如期 nowTime DATE; --当前日期 nowTime2 DATE; - ...
- [转载+原创]Emgu CV on C# (六) —— Emgu CV on Canny边缘检测
Canny边缘检测也是一种边缘检测方法,本文介绍了Canny边缘检测的函数及其使用方法,并利用emgucv方法将轮廓检测解算的结果与原文进行比较. 图像的边缘检测的原理是检测出图像中所有灰度值变化较大 ...
- Python Socket File Transfer
I have a RPi which I intented to use it to crawl data. The development environment in RPi is very ba ...
- 领接表的建立和它的DFS, BFS;;;
//图的建立的实现->邻结矩阵和邻结表两种表示方法 #include <cstdio> #include <cstdlib> //#define _OJ_ int vis ...
- shell script 的追踪与 debug
shell script 的追踪与 debug scripts 在运行之前,最怕的就是出现语法错误的问题了!那么我们如何 debug 呢?有没有办法不需要透过直接运行该 scripts 就可以来判断是 ...
- POJ 3553 Task schedule
原题链接:http://poj.org/problem?id=3553 这道题主要就是贪心思想吧,对于每个job,根据其截止时间 dj 从小到大排序,我们必须要尽快把dj最小的job完成掉,这样才能使 ...
- Embedding Lua in C: Using Lua from inside C.
Requirments: 1: The Lua Sources. 2: A C compiler - cc/gcc/g++ for Unix, and Visual C++ for Wi ...
- What is the difference between supervised learning and unsupervised learning?
Machine Learning is a class of algorithms which is data-driven, i.e. unlike "normal" algor ...
- hdu 1233 还是畅通工程(最小生成树,基础)
题目 //也是标准的最小生成树啊,我就改一点点,不重新再打一遍增加熟练度了 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #inclu ...
- javascript加速运动
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...