ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Algorithm】广度优先搜索(BFS)

2022-03-19 21:02:35  阅读:180  来源: 互联网

标签:Node return Algorithm int BFS newX newY maxn 广度


前面介绍了深度优先搜索,可知 DFS 是以深度作为第一关键词的,即当碰到岔道口时总是先选择其中的一条岔路前进,而不管其他岔路,直到碰到死胡同时才返回岔道口并选择其他岔道口。

接下来介绍的广度优先搜索则是以广度为第一关键词,当碰到岔路口时,总是先依次访问从该岔路口能直接到达的所有结点,然后再按这些结点被访问的顺序去依次访问它们能直接到达的所有结点,以此类推,直到所有的结点都被访问为止。这就跟平静的水面中投入一颗小石子一样,水花总是以石子落水处为中心,并以同心圆的方式向外扩散至整个水面,从这点看和 DFS 那种沿着一条线前进的思路是完全不同的。

 

#include <cstdio>
#include <queue>
using namespace std;

const int maxn = 100;
struct node {
    int x, y;
} Node;

int n, m;   // 矩阵大小 n * m
int matrix[maxn][maxn]; // 01 矩阵
bool inq[maxn][maxn] = { false };   // 记录位置 (x, y) 是否已入过队
int X[4] = {0, 0, 1, -1};   // 增量数组
int Y[4] = {1, -1, 0, 0};

bool judge(int x, int y)    // 判断坐标 (x, y) 是否需要访问
{
    // 越界返回 false
    if(x >= n || x < 0 || y >= m || y < 0) return false;
    // 当前位置为 0, 或 (x, y) 已入过队, 返回 false
    if(matrix[x][y] == 0 || inq[x][y] == true) return false;
    // 以上都不满足, 返回 true
    return true;
}

// BFS 函数访问位置 (x, y) 所在的块, 将该块中所有的 "1" 的 inq 都设置为 true
void BFS(int x, int y)
{
    queue<node> Q;
    Node.x = x, Node.y = y;
    Q.push(Node);
    inq[x][y] = true;
    while(!Q.empty()) {
        node top = Q.front();
        Q.pop();
        for(int i = 0; i < 4; i++) {
            int newX = top.x + X[i];
            int newY = top.y + Y[i];
            if(judge(newX, newY)) { // 如果新位置 (newX, newY) 需要访问
                Node.x = newX, Node.y = newY;
                Q.push(Node);
                inq[newX][newY] = true;
            }
        }
    }
}

int main()
{
    scanf("%d %d", &n, &m);
    for(int x = 0; x < n; x++) {
        for(int y = 0; y < m; y++) {
            scanf("%d", &matrix[x][y]);
        }
    }

    int ans = 0;    // 存入块数
    for(int x = 0; x < n; x++) {    // 枚举每一个位置
        for(int y = 0; y < m; y++) {
            // 如果元素为 1, 且未入过队
            if(matrix[x][y] == 1 && inq[x][y] == false) {
                ans++;  // 块数加 1
                BFS(x, y);  // 访问整个块, 将该块所有 "1" 的 inq 都标记为 true
            }
        }
    }

    printf("%d\n", ans);

    return 0;
}
/*
7 6
0 1 1 1 0 0 1
0 0 1 0 0 0 0
0 0 0 0 1 0 0
0 0 0 1 1 1 0
1 1 1 0 1 0 0
1 1 1 1 0 0 0 
 */

 

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int maxn = 100;
struct node {
    int x, y;   // 位置 (x, y)
    int step;   // step 为从起点 S 到达该位置的最少步数 (层数)
} S, T, Node;   // S 为起点, T 为终点, Node 为临时结点

int n, m;   // n 行 m 列
char maze[maxn][maxn];  // 迷宫信息
bool inq[maxn][maxn] = { false };   // 记录位置 (x, y) 是否已入过队
int X[4] = {0, 0, 1, -1};
int Y[4] = {1, -1, 0, 0};

// 检查位置 (x, y) 是否有效
bool test(int x, int y)
{
    if(x >= n || x < 0 || y >= m || y < 0) return false;    // 超过边界
    if(maze[x][y] == '*') return false; //墙壁 *
    if(inq[x][y] == true) return false; // 已入过对
    return true;
}

int BFS()
{
    queue<node> q;
    q.push(S);
    while(!q.empty()) {
        node top = q.front();
        q.pop();
        if(top.x == T.x && top.y == T.y) {
            return top.step;    // 终点, 直接返回最少步数
        }
        for(int i = 0; i < 4; i++) {    // 循坏 4 次, 得到 4 个相邻位置
            int newX = top.x + X[i];
            int newY = top.y + Y[i];
            if(test(newX, newY)) {  // 位置 (newX, newY) 有效
                Node.x = newX, Node.y = newY;
                Node.step = top.step + 1;
                q.push(Node);
                inq[newX][newY] = true;
            }
        }
    }
    return -1;  // 无法到达最终 T 时返回 -1
}

int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; i++) {
        getchar();  // 过滤掉每行后面的换行符
        for(int j = 0; j < m; j++) {
            maze[i][j] = getchar();
        }
        // maze[i][m + 1] = '\0';
    }
    scanf("%d%d%d%d", &S.x, &S.y, &T.x, &T.y);  // 起点和终点的坐标
    S.step = 0; // 起始化起点的层数为 0, 即 S 到 S 的最少步数为 0
    printf("%d\n", BFS());
    return 0;
}
/*
5 5
.....
.*.*.
.*S*.
...T*
2 2 4 3
*/

 

标签:Node,return,Algorithm,int,BFS,newX,newY,maxn,广度
来源: https://blog.csdn.net/y__a__o/article/details/123583512

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有