LeetCode 934. 最短的桥

主页有其他数据结构内容(持续更新中)

难度:Medium

代码:

class Solution {
private:
    queue<pair<int, int>> points;
    int direction[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
    void dfs(vector<vector<int>>& grid, int x, int y) {
        grid[x][y] = 2;
        for(int k = 0; k < 4; k++) {
            int nextX = x + direction[k][0];
            int nextY = y + direction[k][1];
            if(nextX >= 0 && nextX < grid.size() && nextY >= 0 && nextY < grid[0].size()) {
                if(grid[nextX][nextY] == 2){
                    //  属于同一块岛屿并且已经被标记为2了
                    continue;
                }
                if(grid[nextX][nextY] == 1) {
                    //  属于同一块岛屿但尚未标记
                    dfs(grid, nextX, nextY);
                }
                if(grid[nextX][nextY] == 0) {
                    //  是距离最近的海洋
                    points.push({nextX, nextY});    //  入队,方便后续bfs
                }
            }
        }
    }
public:
    int shortestBridge(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size(); 
        bool isFind = false;
        for (int i = 0; i < m; i++) {
            if (isFind) {
                break;
            }
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    //  调用dfs把这个岛屿都标记为2
                    dfs(grid, i, j);
                    isFind = true;
                    break;
                }
            }
        }
        //  使用bfs寻找第二个岛屿,把过程中的0变成2
        int res = 0;
        while (!points.empty()) {
            res++;
            int size = points.size();
            //  此处的while循环是为了控制元素出队的次数:只能让当前队列里的所有元素出队,不能让新加入的元素出队
            while (size--) {
                auto [x, y] = points.front();
                grid[x][y] = 2; //  填海造陆(队列里都是离陆地最近的海)
                points.pop();
                for (int k = 0; k < 4; k++) {
                    int nextX = x + direction[k][0];
                    int nextY = y + direction[k][1];
                    if (nextX >= 0 && nextX < m && nextY >= 0 && nextY < n) {
                        if (grid[nextX][nextY] == 2) {
                            continue;
                        }
                        else if (grid[nextX][nextY] == 1) {
                            //  说明找到了另一个岛屿
                            return res;
                        }
                        else {
                            //  说明遇到了离当前陆地最近的海洋,需要将其入队
                            points.push({nextX, nextY});
                            grid[nextX][nextY] = 2;
                        }
                    }
                }
            }
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值