深度搜索(DFS):沿分支一条路走到底,无路时回退探索其他路径,用栈实现(递归本质也是栈),适合寻找所有解或路径问题。
广度搜索(BFS):逐层扩散遍历,先访问离起点最近的节点,用队列实现,适合找最短路径或层级关系问题。
核心区别:DFS优先深度(纵向深入),BFS优先广度(横向扩展)。
别急,先把这两种方法的定义先了解一下,不能理解也没关系,我们通过一道迷宫问题来讲解一下!
题目描述:
我们把迷宫用二维数组来存储:
0 1 0 0 0 1
0 1 1 0 1 1
0 0 0 1 0 1
1 1 0 0 0 1
1 1 0 0 0 1
1 1 0 1 0 0
0代表有路,1代表障碍物。
现在要求出我们从(0,0)开始走到(5,5)出口的路径。
首先用BFS来解决问题,这个方法求到的是到出口的最短路径,代码如下。
#include<iostream>
#include<queue>
#include<utility>
#include<stack>
using namespace std;
int map[6][6];//存储图
int dir[4][2] = {
{1,0},{-1,0},{0,1},{0 ,- 1}
};//定义四个方向
int visited[6][6] = {0};
int main() {
queue<pair<int, int>> queue;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
cin >> map[i][j];
}
}
visited[0][0] = -1;
pair<int, int> start(0, 0);
queue.push(start);//先将起始点入队列
while (!queue.empty()) {
int x = queue.front().first;
int y = queue.front().second;
if (x == 5&& y == 5) {
break;
}
queue.pop();
for (int i = 0; i < 4; i++) {
//计算下一个点的坐标,要是符合条件就入队列
int newx = x + dir[i][0];
int newy = y + dir[i][1];
if (newx >= 0 && newx <= 5 && newy >= 0 && newy <= 5 && visited[newx][newy] == 0&&map[newx][newy]==0) {
pair<int, int> temp(newx, newy);
queue.push(temp);
visited[newx][ newy] = i + 1;//记录前一个点到达这个点的方向,1↑,2↓,3←,4→;
}
}
}
//如果我们直接输出路径只能从终点开始,最后的路径是相反的,
// 因此我们要用栈来存储,最后输出。
stack<pair<int, int>> stack;
pair<int, int> end(5, 5);
stack.push(end);
int a = 5;
int b = 5;
while (a || b) {
int x = a - dir[visited[a][b] - 1][0];
int y = b - dir[visited[a][b] - 1][1];
pair<int, int> temp(x, y);
stack.push(temp);
a = x;
b = y;
}
//从起点开始输出路径
while (!stack.empty()) {
int x = stack.top().first;
int y = stack.top().second;
cout << "(" << x << "," << y << ")" << endl;
stack.pop();
}
return 0;
}
运行结果
0 1 0 0 0 1
0 1 1 0 1 1
0 0 0 1 0 1
1 1 0 0 0 1
1 1 0 0 0 1
(0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(3,2)
(4,2)
(5,2)
(5,3)
(5,4)
(5,5)
DFS方法:代码如下
其实就是调用递归,一条路走到底,直到走投无路再回头!
#include<iostream>
#include<stack>
#include<utility>
using namespace std;
int dir[4][2] = {
{0,1},{1,0},{0,-1},{1,0}
};
int map[6][6];//存储地图,1代表障碍物,0代表有路
int visited[6][6] = { 0 };//访问标记
int p[6][6] = {0};//用来记录前驱节点
int x = 0, y = 0;//开始位置坐标
void dfs(int x,int y) {
visited[x][y] = -1;
int newx, newy;
for (int i = 0; i < 4; i++) {
newx = x + dir[i][0];
newy = y + dir[i][1];
if (newx >= 0 && newx <=5 && newy >= 0 && newy <=5 && visited[newx][newy] == 0 && map[newx][newy] == 0) {
p[newx][newy] = i+1;
if (newx == 5 && newy == 5)cout << "找到出口了!" << endl;
dfs(newx,newy);
}
}
}
int main() {
for (int i = 0; i <=5; i++) {
for (int j = 0; j <=5; j++) {
cin >> map[i][j];
}
}
dfs(0, 0);
stack<pair<int, int>> stack;
pair<int, int> start(5, 5);
stack.push(start);
int a = 5, b = 5;
while (a||b) {
int newx = a - dir[p[a][b] - 1][0];
int newy = b - dir[p[a][b] - 1][1];
pair<int, int> temp(newx, newy);
stack.push(temp);
a = newx;
b = newy;
}
while (!stack.empty()) {
int x = stack.top().first;
int y = stack.top().second;
cout << "(" << x << "," << y << ")" << endl;
stack.pop();
}
return 0;
}
运行结果一样只是巧合,换一个地图就不一定一样了
0 1 0 0 0 1
0 1 1 0 1 1
0 0 0 1 0 1
1 1 0 0 0 1
1 1 0 0 0 1
找到出口了!
(0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(3,2)
(3,3)
(3,4)
(4,4)
(4,5)
(5,5)