【算法积累】LeetCode的三道DFS题目

LeetCode 200 - 岛屿数量(中等)

题目链接
在这里插入图片描述

注意DFS的条件判断,不然会因为无限递归而Stack Overflow。

语言:C++

class Solution {
public:
    const int di[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

    void dfs(vector<vector<char>>& grid, int x, int y) {
        int r = grid.size();
        int c = grid[0].size();
        if (grid[x][y] == '1') {
            grid[x][y] = '0';
            for (auto & i : di) {
                int next_x = i[0] + x;
                int next_y = i[1] + y;
                if (next_x >= 0 && next_x < r && next_y >= 0 && next_y < c) {
                    dfs(grid, next_x, next_y);
                }
            }
        }    
    }

    int numIslands(vector<vector<char>>& grid) {
        int r = grid.size();
        int c = grid[0].size();
        int ans = 0;
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                if (grid[i][j] == '1') {
                    ans++;
                    dfs(grid, i, j);
                }
            }
        }
        return ans;
    }
};

LeetCode 695 - 岛屿的最大面积(中等)

题目链接
在这里插入图片描述
语言:C++

class Solution {
public:
    const int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

    int dfs(vector<vector<int>>& grid, int x, int y) {
        if (x < 0 || y < 0 || x >= grid.size() || y >= grid[0].size() || grid[x][y] != 1) {
            return 0;
        }
        grid[x][y] = 0;
        int ans = 1;
        for (auto & i : di) {
            int x_next = x + i[0];
            int y_next = y + i[1];
            ans += dfs(grid, x_next, y_next);
        }
        return ans;
    }

    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int ans = 0;
        for (int i = 0; i < grid.size(); i++) {
            for (int j = 0; j < grid[0].size(); j++) {
                ans = max(ans, dfs(grid, i, j));
            }
        }
        return ans;
    }
};

LeetCode 733 - 图像渲染(简单)

题目链接
在这里插入图片描述

和上面的LeetCode 200一样,需要注意DFS的条件判断,不然会因为无限递归而Stack Overflow。

语言:C++

class Solution {
public:
    const int di[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};

    void dfs(vector<vector<int>>& image, int x, int y, int oldColor, int newColor) {
        if (image[x][y] == oldColor) {
            image[x][y] = newColor;
            for (auto & i : di) {
                int x_next = i[0] + x;
                int y_next = i[1] + y;
                if (x_next >= 0 && y_next >= 0 && x_next < image.size() && y_next < image[0].size()) {
                    dfs(image, x_next, y_next, oldColor, newColor);
                }
            }
        }
    }

    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
        int oldColor = image[sr][sc];
        if (oldColor != newColor) {
            dfs(image, sr, sc, oldColor, newColor);
        }
        return image;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值