【C++刷题】优选算法——BFS第一辑

FloodFill问题

  1. 图像渲染
    在这里插入图片描述
class Solution {
    queue<pair<int, int>> q;
    int original_color;
    int new_color;

    unordered_multimap<int, int> direction = {
        {0, 1},
        {0, -1},
        {1, 0},
        {-1, 0}
    };
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        original_color = image[sr][sc], new_color = color;
        if (original_color == new_color) return image;
        image[sr][sc] = new_color;
        q.push({ sr, sc });
        
        while (!q.empty()) {
            auto base = q.front();
            q.pop();
            for (auto& e : direction) {
                int x = base.first + e.first, y = base.second + e.second;
                if ((x >= 0 && x < image.size())
                    && (y >= 0 && y < image[0].size())
                    && (image[x][y] == original_color)) {
                    image[x][y] = new_color;
                    q.push({ x, y });
                }
            }             
        }
        
        return image;
    }
};
  1. 岛屿数量
    在这里插入图片描述
class Solution {
    vector<vector<bool>> mark;
    queue<pair<int, int>> q;
    int ret = 0;
    unordered_multimap<int, int> direction = {
        {0, 1}, // 右
        {0, -1}, // 左
        {1, 0}, // 上
        {-1, 0} // 下
    };
public:
    void bfs(vector<vector<char>>& grid) {
        while (!q.empty()) {
            auto base = q.front();
            q.pop();
            for (auto& e : direction) {
                int x = base.first + e.first, y = base.second + e.second;
                if ((x >= 0 && x < grid.size())
                && (y >= 0 && y < grid[0].size())
                && (mark[x][y] == false)
                && (grid[x][y] == '1')) {
                    mark[x][y] = true;
                    q.push({x, y});
                }
            }
        }
    }
    int numIslands(vector<vector<char>>& grid) {
        int m = grid.size(), n = grid[0].size();
        mark = vector<vector<bool>>(m, vector<bool>(n, false));

        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (mark[i][j] == false && grid[i][j] == '1') {
                    mark[i][j] = true;
                    q.push({i, j});
                    bfs(grid);
                    ++ret;
                }
            }
        }
        return ret;
    }
};
  1. 岛屿的最大面积
    在这里插入图片描述
class Solution {
vector<vector<bool>> mark;
queue<pair<int, int>> q;
int max_area = 0;
unordered_multimap<int, int> direction = {
    {0, 1},
    {0, -1},
    {1, 0},
    {-1, 0},
};
public:
    int bfs(vector<vector<int>>& grid) {
        int area = 1;
        while (!q.empty()) {
            auto base = q.front();
            q.pop();
            for (auto& e : direction) {
                int x = base.first + e.first, y = base.second + e.second;
                if ((x >= 0 && x < grid.size())
                && (y >= 0 && y < grid[0].size())
                && (mark[x][y] == false)
                && (grid[x][y] == 1)) {
                    mark[x][y] = true;
                    q.push({x, y});
                    ++area;
                }
            }
        }
        return area;
    }
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        mark = vector<vector<bool>>(m, vector<bool>(n, false));

        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (mark[i][j] == false && grid[i][j] == 1) {
                    mark[i][j] = true;
                    q.push({i, j});
                    max_area = max(max_area, bfs(grid));
                }
            }
        }
        return max_area;
    }
};
  1. 被围绕的区域
    在这里插入图片描述
class Solution {
vector<vector<bool>> mark;
queue<pair<int, int>> q;
unordered_multimap<int, int> direction = {
    {0, 1},
    {0, -1},
    {1, 0},
    {-1, 0},
};
public:
    void dfs(vector<vector<char>>& board) {
        while (!q.empty()) {
            auto base = q.front();
            q.pop();
            for (auto& e : direction) {
                int x = base.first + e.first, y = base.second + e.second;
                if ((x >= 0 && x < board.size())
                && (y >= 0 && y < board[0].size())
                && (mark[x][y] == false)
                && (board[x][y] == 'O')) {
                    mark[x][y] = true;
                    q.push({x, y});
                }
            }
        }
    }
    void solve(vector<vector<char>>& board) {
        int m = board.size(), n = board[0].size();
        mark = vector<vector<bool>>(m, vector<bool>(n, false));

        // 第一行 board[0][j]
        // 最后一行 board[m-1][j]
        for (int j = 0; j < n; ++j) {
            if (mark[0][j] == false && board[0][j] == 'O') {
                mark[0][j] = true;
                q.push({0, j});
                dfs(board);
            }
            if (mark[m-1][j] == false && board[m-1][j] == 'O') {
                mark[m-1][j] = true;
                q.push({m-1, j});
                dfs(board);
            }
        }

        // 第一列 board[i][0]
        // 最后一列 board[i][n-1]
        for (int i = 0; i < m; ++i) {
            if (mark[i][0] == false && board[i][0] == 'O') {
                mark[i][0] = true;
                q.push({i, 0});
                dfs(board);
            }
            if (mark[i][n-1] == false && board[i][n-1] == 'O') {
                mark[i][n-1] = true;
                q.push({i, n-1});
                dfs(board);
            }
        }

        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (mark[i][j] == false && board[i][j] == 'O') {
                    board[i][j] = 'X';
                }
            }
        }
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿阿阿顺Yaya

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值