华为笔试复盘 bfs-1 最大山体面积

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <queue>
using namespace std;
struct node {
    int x, y;
};
int n, m, ans = 0;
int mmap[105][105];//存储迷宫的数组
int dir[4][2] = {1, 0, 0, 1, 0, -1, -1, 0};//下,右,左,上
void bfs(int x, int y) {
    queue<node> que;//定义一个保存坐标的队列
    que.push((node){x, y});//存入开始坐标
    int t = mmap[x][y];//标记此点的值
    mmap[x][y] = 0;
    while (!que.empty()) {//循环条件是队列不为空
        node temp = que.front();//取出队列中的坐标
        que.pop();//清空队列
        for (int i = 0; i < 4; ++i) {//循环遍历四个方向
            int xx = temp.x + dir[i][0];
            int yy = temp.y + dir[i][1];//分别加方向数组
            if (xx < 0 || yy < 0 || xx == n || yy == m || mmap[xx][yy] == 0)continue;
            t += mmap[xx][yy];
            mmap[xx][yy] = 0;//标记此坐标已经走过
            que.push((node){xx, yy});//加入队列
        }
    }
    ans = max(t, ans);
}
int main() {
    cin >> m >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> mmap[i][j];
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (mmap[i][j] == 0)  continue;
            bfs(i, j);
        }
    }
    cout << ans << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值