笔试题大疆08.07

题目如下:
在这里插入图片描述
输入输出的测试用例如下所示:
测试用例如下:

6 6 2 3 3
37 37 39 41 13 205
37 41 41 203 39 243
37 41 40 131 40 41
91 41 39 198 41 9
189 41 39 40 40 38
37 124 38 167 41 41

在这里插入图片描述
这道题采用BFS解决问题,这道题和力扣上岛屿问题很类似。
代码如下:

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

void bfs(vector<vector<int>>& grid, vector<vector<bool>>& box, int& nowNum, int nowx, int nowy, int T)
{
    vector<int> x = { 0,0,1,-1 };
    vector<int> y = { -1,1,0,0 };
    int target = grid[nowx][nowy];
    queue<pair<int, int>> pos;
    pos.push(make_pair(nowx, nowy));
    while (!pos.empty()) {
        for (int i = 0; i < 4; i++) {
            int tempx = pos.front().first + x[i];
            int tempy = pos.front().second + y[i];
            if (tempx >= 0 && tempy >= 0 && tempx < grid.size() && tempy < grid[0].size() && !box[tempx][tempy]
                && grid[tempx][tempy] >= target - T + 1 && grid[tempx][tempy] <= target + T - 1) {
                pos.push(make_pair(tempx, tempy));
                box[tempx][tempy] = true;
                nowNum++;
            }
        }
        pos.pop();
    }
}

int main()
{
    int n, m, x, y, t;
    int retSum = 0;
    cin >> n >> m >> x >> y >> t;
    vector<vector<int>> vec(n, vector<int>(m, 0));
    vector<vector<bool>> box(n, vector<bool>(m, false));
    for (size_t i = 0; i < n; ++i) {
        for (size_t j = 0; j < m; ++j) {
            cin >> vec[i][j];
        }
    }
    bfs(vec, box, retSum, y, x, t);
    cout << retSum << endl;

    system("Pause");
    return 0;
}

运行结果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值