![](https://i-blog.csdnimg.cn/blog_migrate/5090cd4d8120b47682056290a782468f.png)
目录
介绍
在深搜和广搜中,有一类问题非常常见,就是走迷宫问题。
概念
迷宫问题是dfs和bfs的一种应用,一般形式为:一个主体(人,动物或者机器等)放在一个迷宫的入口处,迷宫里有许多墙,主体需要在不越过墙的前提下,走出迷宫
类型
迷宫问题一般有一下几种类型:
- 判断是否能到达终点
- 求最小步数
- 求一条可行路径
这类问题可以用dfs和bfs实现
判断是否可达
这类问题比较简单,可以枚举每一条路径,如果发现一条路径能到达终点,就可达。
如果没有路径能到终点,终点就是不可达的
dfs实现
伪代码:
read n,m
read maze[0...n-1][0...m-1]
flag <- false
read sx, sy, ex, ey
check(x, y){
return x>=0 and x<n and y>=0 and y<m and maze[x][y]=0
}
dfs(x, y){
if(flag=1) return
maze[x][y] <- 1
if(x=ex and y=ey){
flag <- true
return
}
nx[0...3]<-{x-1,x+1,x,x}
ny[0...3]<-{y,y,y-1,y+1}
for i <- 0 to 3 do{
if call check(nx[i], ny[i]) = true then{
call dfs(nx[i], ny[i])
}
}
call dfs(x,y)
if flag=true then write "Yes"
else then write "No"
C++实现:
#include<iostream>
using namespace std;
int n, m;
const int N = 100;
int maze[N][N]; //迷宫,1代表墙,0代表空地
bool flag = false;
int ex, ey; // 终点坐标
//检查(x,y)是否能走
bool check(int x, int y) {
return x >= 0 && x < n&& y >= 0 && y < m&& maze[x][y] == 0;
}
// 当前已经走到(x,y)
void dfs(int x, int y) {
if (flag) return;
maze[x][y] = 1; //已经走过,后面不能再走
// 走到终点
if (x == ex && y == ey) {
flag = true;
return;
}
// 所有可达点的坐标
int nx[4] = { x - 1,x + 1,x,x };
int ny[4] = { y,y,y - 1,y + 1 };
for (int i = 0; i < 4; i++)
if (check(nx[i], ny[i])) dfs(nx[i], ny[i]);
}
// 读入迷宫
void read() {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> maze[i][j];
}
int main() {
int sx, sy;
read();
cin >> sx >> sy >> ex >> ey;
dfs(sx, sy);
cout << (flag ? "Yes" : "No") << endl;
return 0;
}
bfs实现
用dfs实现的程序,数据一大,就可能栈溢出。
下面分享bfs的实现
伪代码:
read n,m
read maze[0..n-1][0...m-1]
q = new queue
push(x,y){
q.push(x,y)
maze[x][y] <- 1
}
check(x, y){
return x>=0 and x<n and y>=0 and y<m and maze[x][y]=0
}
can_reach(sx,sy,ex,ey){
push(x,y)
repeat{
t <- q.front
q.pop
next[0...3]={[t.x-1,t.y],[t.x+1,t.y],[t.x,t.y-1],[t.x,t.y+1]}
for i = 0 to 3 do{
if call check(next[i].x,next[i].y) call push(next[i].x,next[i].y)
if(next[i].x=ex and next[i].y=ey) return true
}
}until q.empty
return false
}
read sx,sy,ex,ey
if call can_reach(sx,sy,ex,ey) = true then write "Yes"
else then write "No"
C++实现:
#include<iostream>
#include<queue>
using namespace std;
int n, m;
const int N = 100;
int maze[N][N]; //迷宫,1代表墙,0代表空地
struct node {
int x, y;
node(int x, int y) :x(x), y(y) {}
};
queue<node> q;
// 点(x,y)入队
void push(int x, int y) {
q.emplace(x, y);
maze[x][y] = 1;
}
bool check(int x, int y) {
return x >= 0 && x < n&& y >= 0 && y < m&& maze[x][y] == 0;
}
bool can_reach(int sx, int sy, int ex, int ey) {
push(sx, sy);
while (q.size()) {
node t = q.front();
q.pop();
// 往四个方向扩展
node next[4] = { node(t.x - 1,t.y),node(t.x + 1,t.y),node(t.x,t.y - 1),node(t.x,t.y + 1) };
for (int i = 0; i < 4; i++)
// 检查是否能到达
if (check(next[i].x, next[i].y)) {
push(next[i].x, next[i].y); //入队
//到达终点
if (next[i].x == ex && next[i].y == ey) return true;
}
}
return false;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> maze[i][j];
int ha, la, hb, lb; //起点为(ha,la),终点为(hb,lb)
cin >> ha >> la >> hb >> lb;
if (can_reach(ha, la, hb, lb)) cout << "yes" << endl;
else cout << "no" << endl;
return 0;
}
求最小步数
这类问题相当常见,输入n*m的迷宫,计算出给定两点的最小步数
dfs实现
伪代码:
read n,m
read maze[0...n-1][0...m-1]
st[0...n-1][0...m-1] <- false
read sx, sy, ex, ey
ans <- inf
check(x, y){
return x>=0 and x<n and y>=0 and y<m and maze[x][y]=0 and st[x][y]=false
}
dfs(x, y, step){
if(x=ex and y=ey){
ans = ans < step? ans: step
return
}
nx[0...3]<-{x-1,x+1,x,x}
ny[0...3]<-{y,y,y-1,y+1}
for i <- 0 to 3 do{
if call check(nx[i], ny[i]) = true then{
st[nx[i]][ny[i]] <- true
call dfs(nx[i], ny[i])
st[nx[i]][ny[i]] <- false
}
}
if maze[sx][sy]=1 then write "no path"
else then{
call dfs(x,y)
if ans=inf then write "no path"
else write ans
}
c++:
#include<iostream>
#include<climits>
using namespace std;
int n, m;
const int N = 100;
int maze[N][N]; //迷宫,1代表墙,0代表空地
bool st[N][N]; // 标记数组
int sx, sy, ex, ey;
int ans = INT_MAX;
// 检查(x,y)是否能走
bool check(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m&& maze[x][y] == 0 && !st[x][y];
}
// 当前在(x,y)上,已经走了step步
void dfs(int x, int y, int step) {
if (x == ex && y == ey) ans = min(ans, step);
else {
int nx[4] = { x - 1,x + 1,x,x };
int ny[4] = { y,y,y - 1,y + 1 };
for (int i = 0; i < 4; i++)
if (check(nx[i], ny[i])) {
st[nx[i]][ny[i]] = true;
dfs(nx[i], ny[i], step + 1);
st[nx[i]][ny[i]] = false;
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> maze[i][j];
cin >> sx >> sy >> ex >> ey;
if(maze[sx][sy]==1) cout<<-1<<endl;
else{
st[sx][sy] = true; // 标记起点
dfs(sx, sy, 0);
if (ans != INT_MAX) cout << ans << endl;
else cout << -1 << endl;
}
return 0;
}
bfs实现
伪代码:
read n,m
read maze[0...n-1][0...m-1]
st[0...n-1][0...m-1] <- false
q = new queue
push(x,y,step){
q.push(x,y,step)
st[x][y] <- true
}
check(x, y){
return x>=0 and x<n and y>=0 and y<m and maze[x][y]=0 and st[x][y]=false
}
min_step(sx,sy,ex,ey){
if(maze[sx][sy]=1) return -1
push(sx,sy,0)
repeat{
t <- q.front
q.pop
if(t.x=ex and t.y=ey) return t.step
nx[0...3]<-{x-1,x+1,x,x}
ny[0...3]<-{y,y,y-1,y+1}
for i=0 to 3 do{
if call check(nx[i],ny[i]) = true then push(nx[i],ny[i],t.step+1)
}
}until q.empty
}
read sx,sy,ex,ey;
write min_step(sx,sy,ex,ey)
c++:
#include<iostream>
#include<queue>
using namespace std;
int n, m;
const int N = 100;
int maze[N][N]; //迷宫,1代表墙,0代表空地
bool st[N][N]; // 标记数组
struct node {
int x, y, step;
node(int x, int y, int step) :x(x), y(y), step(step) {}
};
queue<node> q;
// 点(x,y)入队
void push(int x, int y, int step) {
q.emplace(x, y, step);
st[x][y] = true;
}
// 检查(x,y)是否能走
bool check(int x, int y) {
return x >= 0 && x < n&& y >= 0 && y < m&& maze[x][y] == 0 && !st[x][y];
}
// 最小步数,无法到达返回-1
int min_step(int sx, int sy, int ex, int ey) {
// 起点是障碍
if (maze[sx][sy] == 1) return -1;
push(sx, sy, 0); //起点入队
while (q.size()) {
node t = q.front();
q.pop();
// 到达终点,返回最小步数
if (t.x == ex && t.y == ey) return t.step;
// 向四个方向扩展
int nx[4] = { t.x - 1,t.x + 1,t.x,t.x };
int ny[4] = { t.y,t.y,t.y - 1,t.y + 1 };
for (int i = 0; i < 4; i++)
if (check(nx[i], ny[i])) push(nx[i], ny[i], t.step + 1);
}
return -1;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> maze[i][j];
int ha, la, hb, lb; //起点为(ha,la),终点为(hb,lb)
cin >> ha >> la >> hb >> lb;
cout << min_step(ha, la, hb, lb) << endl;
return 0;
}
求一条可达路径
这类问题的形式是:输入n*m的迷宫,给定一个起点和终点,求一条可行路径(一般为最短路径)
这类问题用dfs实现不容易,只分享bfs做法
伪代码:
read n,m
read maze[0...n-1][0...m-1]
st[0...n-1][0...m-1] <- false
pre[0...n-1][0...m-1] <- (0,0)
q = new queue
push(x,y,tx,ty){
q.push(x,y)
st[x][y] <- true
pre[x][y]=(tx,ty)
}
check(x, y){
return x>=0 and x<n and y>=0 and y<m and maze[x][y]=0 and st[x][y]=false
}
shortest_path(sx,sy,ex,ey){
if(maze[sx][sy]=1) return false
push(sx,sy,-1,-1)
repeat{
t <- q.front
q.pop
if(t.x=ex and t.y=ey) return true
nx[0...3]<-{x-1,x+1,x,x}
ny[0...3]<-{y,y,y-1,y+1}
for i=0 to 3 do{
if call check(nx[i],ny[i]) = true then push(nx[i],ny[i],t.x,t.y)
}
}until q.empty
return false
}
print(x,y){
if(x=-1 and y=-1) return
print(pre[x][y].x,pre[x][y].y)
write (x,y)
}
read sx,sy,ex,ey;
if call shortest_path(sx,sy,ex,ey) = true then print(ex,ey)
else write "no path"
C++:
#include<iostream>
#include<queue>
using namespace std;
int n, m;
const int N = 100;
int maze[N][N]; //迷宫,1代表墙,0代表空地
bool st[N][N]; // 标记数组
struct node {
int x, y, step;
node(int x, int y) :x(x), y(y){}
node() { node(0, 0); }
};
queue<node> q;
node pre[N][N]; //记录(x,y)的前驱
// 点(x,y)入队,记录前驱
void push(int x, int y, int px, int py) {
q.emplace(x, y);
st[x][y] = true;
pre[x][y] = node(px, py);
}
// 检查(x,y)是否能走
bool check(int x, int y) {
return x >= 0 && x < n&& y >= 0 && y < m&& maze[x][y] == 0 && !st[x][y];
}
// 输出路径
void print(int x, int y) {
if (x == -1 && y == -1) return;
print(pre[x][y].x, pre[x][y].y);
cout << "(" << x << "," << y << ")" << endl;
}
// 计算(sx,sy)到(ex,ey)的最短路径,返回值代表是否可达
bool shortest_path(int sx, int sy, int ex, int ey) {
if (maze[sx][sy] == 1) return false;
push(sx, sy, -1, -1);
while (q.size()) {
node t = q.front();
q.pop();
// 可以到达
if (t.x == ex && t.y == ey) return true;
// 扩展
int nx[4] = { t.x - 1,t.x + 1,t.x,t.x };
int ny[4] = { t.y,t.y,t.y - 1,t.y + 1 };
for (int i = 0; i < 4; i++)
if (check(nx[i], ny[i])) push(nx[i], ny[i], t.x, t.y);
}
return false;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> maze[i][j];
int ha, la, hb, lb; //起点为(ha,la),终点为(hb,lb)
cin >> ha >> la >> hb >> lb;
if (shortest_path(ha, la, hb, lb)) print(hb, lb);
else cout << "no path" << endl; //走不通
return 0;
}
输入:
3 3
0 0 0
1 1 0
1 1 0
输出:
(0,0)
(0,1)
(0,2)
(1,2)
(2,2)
pre数组情况:
0 | 1 | 2 | |
0 | (-1,-1) | (0,0) | (0,1) |
1 | (0,0) | (0,0) | (0,2) |
2 | (0,0) | (0,0) | (1,2) |