前置知识 - 队列
什么是队?
STL —— queue
特点:先进先出(First In First Out)
方法:
- push(x):将x入队
- pop():队首元素出队
- front( ):获取队首元素
- back( ):获取队尾元素
- empty( ):判断queue是否为空
- size( ):队列长度
#include<queue>
#include<iostream>
using namespace std;
int main()
{
queue<int> que;//定义队列
que.push(1);
que.push(2);
que.push(3);
cout << que.front() << endl;//1
que.pop();
cout << que.front() << endl;//2
que.pop();
cout << que.front() << endl;//3
que.pop();
cout << que.empty() << endl;//1
return 0;
}
迷宫问题
问题描述:给定一个地图,从起点开始搜索达到终点的最短步数
假设迷宫的空地用’.'表示
障碍物用‘#’表示
迷宫可表示为:
..#.
....
..#.
.#..
...#
迷宫问题DFS写法:
#include <iostream>
using namespace std;
const int N = 10;
int ans = 0x3f3f3f3f;//表示从起点到终点的最小步数
int n,m;//n表示地图的行数,m表示地图的列数
char a[N][N];//存储迷宫
int vis[N][N];//标记数组,vis[i][j]=0表示未被访问,vis[i][j]=1表示已被访问
int startx,starty,endx,endy;//起点和终点
int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};//方向数组
void dfs(int x,int y,int step)//x表示当前在x行,y表示当前在y列,当前的坐标为(x,y),目前的步数是step
{
if (x == endx && y == endy)//走到了终点
{
if (step < ans)
ans = step;//更新最小的步数
return;//回溯
}
for (int i = 0;i < 4;i++)//未到终点,沿右下左上进行搜索
{
int new_x = x + dir[i][0];//new_x = x + dir[0][0] = x + 0 = x;new_y = y + dir[0][1] = y + 1 = y + 1
int new_y = y + dir[i][1];//坐标由(x,y) ——> (x,y + 1),实现坐标右移一列
if (new_x < 1 || new_x > n || new_y < 1 || new_y > m) continue;//剪枝:新的坐标超过数组边界
if (a[new_x][new_y] == '.' && vis[new_x][new_y] == 0)//当前的地方是空地且未被访问
{
vis[new_x][new_y] = 1;//修改标记数组表示已经被访问
dfs(new_x,new_y,step + 1);//深搜
vis[new_x][new_y] = 0;//恢复现场
}
}
}
int main()
{
cin >> n >> m;//行和列
cin >> startx >> starty >> endx >> endy;//输入起点(1,1)和终点(4,3)
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
cin >> a[i][j];
vis[startx][starty] = 1;//从起点开始搜索达到终点的最短步数
dfs(startx,starty,0);//位于起点是第0步
cout << ans;
return 0;
}
输入数据
5 4
1 1 4 3
..#.
....
..#.
.#..
...#
迷宫问题BFS写法:
思路:
1.首先初始化一个队列,并将起点(1,1)入队,此时的步数为0
2.初始坐标(1,1),经过一步之后可以到达坐标(1,2)或者(2,1),将这两点入队,然后队首元素出队。此时队列如图
3.(1,2)再走一步之后可以到达坐标(2,2),将这点入队,然后队首元素出队。此时队列如图
以此类推,可得到最小步数… …
#include <queue>
#include <iostream>
using namespace std;
int n,m;
const int N = 10;
int startx,starty,endx,endy;
int a[N][N];//用来存储地图,a[i][j]=0表示空地,否则为障碍物
int vis[N][N];//标记数组,vis[i][j]=0表示该点未被访问,vis[i][j]=1表示已被访问
int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};//方向数组
struct node
{
int x;//表示当前在坐标(x,y)处,步数为step
int y;
int step;
};
void bfs(int x,int y,int step)
{
queue<node> q;//定义一个可以存储以上结构体的队列
struct node t;//结构体
t.x = x;
t.y = y;
t.step = step;
q.push(t);//将起点入队(1,1,0)
vis[x][y] = 1;//修改标记数组
while (!q.empty())//队列非空
{
t = q.front();//将队首的信息取出
if (t.x == endx && t.y == endy)//到达终点
{
cout << t.step;
break;
}
for (int k = 0;k < 4;k++)//未到达终点,将(x,y)四个方向的点加入队列
{
int newx = t.x + dir[k][0];
int newy = t.y + dir[k][1];
if (vis[newx][newy] == 0 && a[newx][newy] == 0)//该点未被访问过且为空地
{
vis[newx][newy] = 1;//修改标记数组
struct node new_node;
new_node.x = newx;//设置新的
new_node.y = newy;
new_node.step = t.step + 1;
q.push(new_node);//新的结构体入队
}
}
q.pop();//四个方向扩展完毕之后将队首元素出队
}
}
int main()
{
cin >> n >> m;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
cin >> a[i][j];
cin >> startx >> starty >> endx >> endy;
bfs(startx,starty,0);
return 0;
}
BFS测试样例
5 4
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
1 1 4 3
比较BFS与DFS
- 标记数组、方向数组大部分情况下是都需要的
- 如果题目要求找到最短路径,DFS即使找到了出口也不能提前提交答案,它必须找到所有能走到终点的路径,最后选择最短的,所以DFS适合小数据。在最短路径的问题上BFS更快一点,但是代码长度也会更大一点
- 如果题目要求是否可以从A点到达B点(连通性问题),DFS也可以解决这类问题,但并不能保证是通过最短的路径到达的B点。反之,BFS则可以保证是通过最短的路径由A到达B
例题
(提示:例1可以使用DFS搜索答案。例2使用DFS会超时,最短路一般用BFS,每次向四周扩散,记录步数,DFS统计最短,要么数据很小要么特殊情况)