UVA 672 油田(BFS)

本文介绍了一种使用广度优先搜索(BFS)算法解决二维数组中联通‘@’数量的问题。给定一个由‘@’和‘*’组成的二维数组,通过BFS遍历找出所有独立联通的‘@’群组,并统计其数量。文章提供了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Discription

给定一个包含@ *的二维数组,输出有几个@是联通的,如果一个@在另外一个@相邻的八个方向则视作相邻。

Input

包含@ *的二维数组。

Output

输出联通的个数

Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output

0
1
2
2

Solution

BFS模板题,换一个模板

Code
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;
const int maxn = 111;
char pic[maxn][maxn];
bool vst[maxn][maxn];
int dir[8][2] = {0, -1, 1, -1, 1, 0, 1, 1, 0, 1, -1, 1, -1, 0, -1, -1}; // 方向向量
struct State                                                            // BFS 队列中的状态数据结构
{
    int x, y;         // 坐标位置
    int Step_Counter; // 搜索步数统计器
};
State a[maxn];
bool CheckState(State s) // 约束条件检验
{
    if (!vst[s.x][s.y] && pic[s.x][s.y] == '@') // 满足条件
        return 1;
    else // 约束条件冲突
        return 0;
}
void bfs(State st)
{
    queue<State> q;  // BFS 队列
    State now, next; // 定义2 个状态,当前和下一个
    // st.Step_Counter = 0; // 计数器清零
    q.push(st);          // 入队
    vst[st.x][st.y] = 1; // 访问标记
    while (!q.empty())
    {
        now = q.front(); // 取队首元素进行扩展
        for (int i = 0; i < 8; i++)
        {
            next.x = now.x + dir[i][0]; // 按照规则生成下一个状态
            next.y = now.y + dir[i][1];
            next.Step_Counter = now.Step_Counter + 1; // 计数器加1
            if (CheckState(next))                     // 如果状态满足约束条件则入队
            {
                q.push(next);
                vst[next.x][next.y] = 1; //访问标记
            }
        }
        q.pop(); // 队首元素出队
    }
    return;
}
int main()
{
    // freopen("in.txt", "r", stdin);
    int m, n;
    while (~scanf("%d%d", &m, &n) && (m || n))
    {
        memset(vst, false, sizeof(vst));
        for (int i = 0; i < m; i++)
            scanf("%s", &pic[i]);
        int ans = 0;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (vst[i][j] == false && pic[i][j] == '@')
                {
                    ans++;
                    State s;
                    s.x = i, s.y = j;
                    bfs(s);
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值