(BFS)广度优先搜索例子:迷宫,寻找连块数

//广度优先搜索

主要分成以下几块

0、节点node结构体;

1、matrix[][],原始矩阵

2、增量数组X[],Y[] 实现该坐标的左右上下的更新坐标

3、判断时候下一个元素需要入队列的函数

4、记录元素坐标时候已经访问的inq[][];

//广度优先  BFS

//寻找代码块的个数

struct Node_B{
    int x, y;
};
Node_B node;
int n, m;
const int maxn = 100;

int matrix[maxn][maxn];
bool inq[maxn][maxn] = { false };
int X[4] = { 0, 0, 1, -1 };
int Y[4] = { 1, -1, 0, 0 };

//判断改点是在矩阵的内部
bool judge(int x, int y){
    if (x >= n || y>= m ||x<0 || y<0){
        return false;
    }
    if (matrix[x][y] == 0 || inq[x][y] == true){
        return false;
    }
    return true;
}
//广度优先分成的几个步骤需要你充分熟悉  4步骤
void BFS(int x, int y){
    queue<Node_B> Q;
    node.x = x; node.y = y;
    Q.push(node);
    inq[x][y] = true;
    while (!Q.empty())
    {
        Node_B 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)){
                node.x = newX;
                node.y = newY;
                Q.push(node);
                inq[newX][newY] = true;
            }
        }

    }

}

void testBFS(){
    cin >> n >> m;
    for (int x = 0; x < n; x++)
    {
        for (int y = 0; y < m; y++)
        {
            cin >> matrix[x][y];
        }
    }
    int ans = 0;//记录存放的快速
    for (int x = 0; x < n; x++)
    {
        for (int y = 0; y < m; y++)
        {
            if (matrix[x][y] ==1 && inq[x][y] ==false)
            {
                ans++;
                BFS(x, y);
            }
        }
    }

    cout << ans;

}

//广度优先搜索的示例二:走迷宫

1.top实际上是每个分支的起点;

2.检索遇到满足maze和inq的田间的就加入队列中去;

// 广度优先2  BFS

//迷宫

struct Node_C{
    int x, y;
    int step=0;
};
Node_C node0,S,T;

char maze[maxn][maxn];


//判断改点是在矩阵的内部
bool judge1(int x, int y){
    if (x >= n || y >= m || x<0 || y<0){
        return false;
    }
    if (maze[x][y] == '*' || inq[x][y] == true){
        return false;
    }
    return true;
}
//广度优先分成的几个步骤需要你充分熟悉  4步骤
int BFS2(){
    queue<Node_C> Q;
    Q.push(S);
    while (!Q.empty())
    {
        Node_C top = Q.front();//取出队列头,可以后的当前的坐标值便于更新下一次的坐标
        Q.pop();

        //若全部弹出之后则就返回步数;
        if (top.x==T.x && top.y==T.y)
        {
            return top.step;
        }

        for (int i = 0; i < 4; i++)
        {
            int newX = top.x + X[i];
            int newY = top.y + Y[i];
            if (judge1(newX, newY)){
                node0.x = newX;
                node0.y = newY;
                node0.step = top.step + 1;//新的节点,即将要访问的节点的更新
                Q.push(node0);
                inq[newX][newY] = true;
            }
        }

    }
    return -1;

}

void testBFS2(){
    cin >> n >> m;
    for (int x = 0; x < n; x++)
    {
        cin.get();
        for (int y = 0; y < m; y++)
        {
            cin >> maze[x][y];
        }
        maze[x][m + 1] = '\0';
    }
    cin >> S.x >> S.y >> T.x >> T.y;
    S.step = 0;
    cout << BFS2() << endl;

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值