AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 吐槽
  • App
  • 登录/注册

岛屿数量

作者: 作者的头像   Jiarui.X ,  2023-09-19 16:21:23 ,  所有人可见 ,  阅读 31


1


class Solution {
public:
    int ans = 0, n, m;
    int dd[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    bool vis[301][301];
    vector<vector<char>> a;
    inline void dfs(int x, int y) {
        for (int t = 0; t < 4; t ++) {
            int c = dd[t][0] + x, d = dd[t][1] + y;
            if(c < 0 || c >= n || d < 0 || d >= m || vis[c][d] || a[c][d] != '1') continue;
            vis[c][d] = true;
            dfs(c, d);
        }
    }
    int numIslands(vector<vector<char>>& b) {
        a = b;
        n = a.size(), m = a[0].size();
        for (int i = 0; i < n; i ++) {
            for (int j = 0; j < m; j ++) {
                if(a[i][j] == '1' && !vis[i][j]) {
                    ++ ans;
                    vis[i][j] = true;
                    dfs(i, j);
                }
            }
        }
        return ans;
    }
};

1 评论


用户头像
成大失败哥   8天前         踩      回复

ORZ


你确定删除吗?
1024
x

© 2018-2023 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息