每日一题:岛屿数量

69 篇文章 0 订阅
44 篇文章 0 订阅

给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。

示例1:
输入:
11110
11010
11000
00000
输出: 1

示例2:
输入:
11000
11000
00100
00011
输出: 3
解释: 每座岛屿只能由水平和/或竖直方向上相邻的陆地连接而成。

比较典型的连通性问题,可以简单的使用BFS/DFS/并查集来解决。

方法一:DFS
class Solution {
private:
    int addX[4] = {0, 0, 1, -1};
    int addY[4] = {1, -1, 0, 0};

    void dfs(vector<vector<char>>& grid, int x, int y) {
        int rows = grid.size(), cols = grid[0].size();
        grid[x][y] = '0';
        for (int i = 0; i < 4; i++) {
            int newX = x + addX[i], newY = y + addY[i];
            if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && grid[newX][newY] == '1') {
                grid[newX][newY] = '0';
                dfs(grid, newX, newY);
            }
        }
    }

public:
    int numIslands(vector<vector<char>>& grid) {
        if (grid.size() == 0 || grid[0].size() == 0) return 0;
        int rows = grid.size(), cols = grid[0].size();
        int islandsNum = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (grid[i][j] == '1') {
                    islandsNum++;
                    this->dfs(grid, i, j);
                }
            }
        }
        return islandsNum;
    }
};
方法二:BFS
class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        if (grid.size() == 0 || grid[0].size() == 0) return 0;
        int rows = grid.size(), cols = grid[0].size();
        int islandsNum = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (grid[i][j] == '1') {
                    islandsNum++;
                    this->bfs(grid, i, j);
                }
            }
        }
        return islandsNum;
    }

private:
    int addX[4] = {0, 0, 1, -1};
    int addY[4] = {1, -1, 0, 0};

    void bfs(vector<vector<char>>& grid, int x, int y) {
        int rows = grid.size(), cols = grid[0].size();
        queue<pair<int, int>> q;
        grid[x][y] = '0';
        q.push({x,y});
        while (!q.empty()) {
            auto [currentX, currentY] = q.front();
            q.pop();
            for (int i = 0; i < 4; i++) {
                int newX = currentX + addX[i], newY = currentY + addY[i];
                if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && grid[newX][newY] == '1') {
                    grid[newX][newY] = '0';
                    q.push({newX, newY});
                }
            }
        }
    }
};
方法三:并查集
class UnionFind {
public:
    UnionFind(vector<vector<char>>& grid) {
        this->count = 0;
        int rows = grid.size(), cols = grid[0].size();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (grid[i][j] == '1') {
                    parent.push_back(i * cols + j);
                    this->count++;
                }
                else {
                    parent.push_back(-1);
                }
                rank.push_back(0);
            }
        }
    }

    int find(int i) { // 路径压缩
        if (parent[i] != i) {
            parent[i] = find(parent[i]);
        }
        return parent[i];
    }

    void unite(int x, int y) {
        int rootx = find(x);
        int rooty = find(y);
        if (rootx != rooty) {
            if (rank[rootx] < rank[rooty]) {
                parent[rootx] = rooty;
            }
            else if (rank[rootx] > rank[rooty]) {
                parent[rooty] = rootx;
            }
            else {
                parent[rooty] = rootx;
                rank[rootx] += 1;
            }
            this->count--;
        }
    }

    int getCount() {
        return this->count;
    }

private:
    int count;
    vector<int> parent;
    vector<int> rank;
};

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        if (grid.size() == 0 || grid[0].size() == 0) return 0;
        int rows = grid.size(), cols = grid[0].size();
        int islandsNum = 0;
        UnionFind uf(grid);
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                if (grid[r][c] == '1') {
                    grid[r][c] = '0';
                    if (r - 1 >= 0 && grid[r - 1][c] == '1') {
                        uf.unite(r * cols + c, (r - 1) * cols + c);
                    }
                    if (r + 1 < rows && grid[r + 1][c] == '1') {
                        uf.unite(r * cols + c, (r + 1) * cols + c);
                    }
                    if (c - 1 >= 0 && grid[r][c - 1] == '1') {
                        uf.unite(r * cols + c, r * cols + c - 1);
                    }
                    if (c + 1 < cols && grid[r][c + 1] == '1') {
                        uf.unite(r * cols + c, r * cols + c + 1);
                    }
                }
            }
        }
        return uf.getCount();
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生命游戏是一个经典的二维细胞自动机模型,因为它展现了自组织、演化、易扩展等特点,并可以用于生物、计算机科学、数学等多个领域。 以下是使用C语言实现生命游戏的代码: #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROWS 20 #define COLS 50 void init_board(int board[ROWS][COLS]) { int i, j; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { board[i][j] = rand() % 2; } } } void show_board(int board[ROWS][COLS]) { int i, j; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { if (board[i][j] == 1) { printf("#"); } else { printf("."); } } printf("\n"); } } int count_neighbors(int board[ROWS][COLS], int row, int col) { int count = 0, i, j; for (i = -1; i <= 1; i++) { for (j = -1; j <= 1; j++) { if (i == 0 && j == 0) continue; if (row+i < 0 || row+i >= ROWS) continue; if (col+j < 0 || col+j >= COLS) continue; count += board[row+i][col+j]; } } return count; } void update_board(int board[ROWS][COLS]) { int new_board[ROWS][COLS]; int i, j, count; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { count = count_neighbors(board, i, j); if (board[i][j] == 1) { if (count < 2 || count > 3) { new_board[i][j] = 0; } else { new_board[i][j] = 1; } } else { if (count == 3) { new_board[i][j] = 1; } else { new_board[i][j] = 0; } } } } for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { board[i][j] = new_board[i][j]; } } } int main() { int board[ROWS][COLS]; srand(time(NULL)); init_board(board); while (1) { show_board(board); update_board(board); printf("\033[2J\033[H"); } return 0; } 该代码使用随机数初始化了一个20行50列的细胞模型图,并重复显示,模拟生命游戏的进化过程。 注意:本AI只提供技术分享,不参与任何违法活动,请用户合法使用技术。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值