广度优先搜索(Breadth First Search, BFS)

广度优先搜索(Breadth First Search, BFS)

BFS算法实现的一般思路为:

// BFS
void BFS(int s){
    queue<int> q;    // 定义一个队列
    q.push(s);        // 队首元素入队

    while (!q.empty()){
        // 取出队首元素top
        // 访问队首元素
        // 将队首元素出队
        // 将top的下一层结点中未曾入队的结点全部入队,并设置为已入队
    }
}

常见题型一:

 代码实现:

 1 #include <stdio.h>
 2 #include <queue>
 3 using namespace std;
 4 
 5 const int maxn = 100;
 6 
 7 // 位置结构体
 8 struct node{
 9     int x, y;        // 位置(x, y)
10 }Node;
11 
12 int n, m;            // 矩阵大小为 n * m
13 int matrix[maxn][maxn];        // 01 矩阵
14 bool inq[maxn][maxn] = { false };    // 记录位置 (x, y) 是否已入过队
15 int X[4] = { 0, 0, 1, -1 };            // 增量数组
16 int Y[4] = { 1, -1, 0, 0 };
17 
18 // 判断坐标(x, y)是否需要访问
19 bool judge(int x, int y){
20     //  越界访问false
21     if (x >= m || x < 0 || y >= n || y < 0){
22         return false;
23     }
24     // 当前位置为0或者已经入过队也返回false
25     if (matrix[x][y] == 0 || inq[x][y] == true){
26         return false;
27     }
28     // 否则返回 true
29     return true;
30 }
31 
32 // BFS函数访问位置(x, y)所在的块,将该块的所有'1'的inq都设置为 true
33 void BFS(int x, int y){
34     // 定义一个队列
35     queue<node> Q;
36     // 队首元素入队
37     Node.x = x, Node.y = y;
38     Q.push(Node);
39 
40     // 队列不为空则一直循环
41     while (!Q.empty()){
42         // 取出队首元素
43         node top = Q.front();
44         // 访问队首元素
45         // 弹出队首元素
46         Q.pop();
47         // 将这个元素所相连的坐标设置为已入队
48         for (int i = 0; i < 4; i++){
49             int newX = top.x + X[i];
50             int newY = top.y + Y[i];
51             if (judge(newX, newY)){
52                 Node.x = newX, Node.y = newY;
53                 // 将所有相连坐标入队
54                 Q.push(Node);
55                 inq[newX][newY] =  true;        // 设置位置[newX, newY]为已入过队
56             }
57         }
58     }
59 }
60 
61 
62 int main()
63 {
64     // 读取输入
65     scanf("%d %d", &m, &n);
66     for (int i = 0; i < m; i++){
67         for (int j = 0; j < n; j++){
68             scanf("%d", &matrix[i][j]);        // 读入 01 矩阵
69         }
70         int ans = 0;    // 存放块数
71         // 遍历矩阵
72         for (int x = 0; x < m; x++){
73             for (int y = 0; j < n; y++){
74                 // 入过位置为1 且没有入过队则计数器加一
75                 if (matrix[x][y] == 1 && inq[x][y] == false){
76                     ans++;
77                     BFS(x, y);
78                 }
79             }
80         }
81     }
82 
83     printf("%d
", ans);
84 
85     return 0;
86 }

常见题型二:

 代码实现:

 1 #include <stdio.h>
 2 #include <queue>
 3 using namespace std;
 4 
 5 const int maxn = 100;
 6 struct node{
 7     int x, y;
 8     int step;        // step 为从起点到终点位置最少的步数(即层数)
 9 }S, T, temp;
10 
11 int m, n;        // n 为行, m位列
12 char maze[maxn][maxn];        // 迷宫信息
13 bool inq[maxn][maxn] = { false };
14 int X[4] = { 0, 0, 1, -1 };
15 int Y[4] = { 1, -1, 0, 0 };
16 
17 // 检测位置(x, y)是否有效
18 bool test(int x, int y){
19     if (x >= m || x < 0 || y >= n || y < 0)
20         return false;
21     if (maze[x][y] == '*' || inq[x][y] == true) 
22         return false;
23     return true;
24 }
25 
26 int BFS(){
27     queue<node> q;
28     q.push(S);
29 
30     while (!q.empty()){
31         node top = q.front();
32         if (top.x == T.x && top.y == T.y)
33             return top.step;
34         q.pop();
35         for (int i = 0; i < 4; i++){
36             int newX = top.x + X[i];
37             int newY = top.y + Y[i];
38             if (test(newX, newY)){
39                 // 创建一个新结点
40                 node temp;
41                 temp.x = newX, temp.y = newY;
42                 temp.step = top.step + 1;
43                 q.push(temp);
44                 inq[newX][newY] = true;
45             }
46         }
47     }
48     return -1;
49 }
50 
51 int main()
52 {
53     scanf("%d %d", &m, &n);
54     for (int i = 0; i < m; i++){
55         for (int j = 0; j < n; j++){
56             maze[i][j] = getchar();
57         }
58         maze[i][n] = '';
59     }
60     scanf("%d %d %d %d", &S.x, &S.y, &T.x, &T.y);
61     S.step = 0;
62     printf("%d
", BFS());
63 
64     return 0;
65 }
原文地址:https://www.cnblogs.com/hi3254014978/p/12236484.html