【leetcode】200. 岛屿数量

 

void dfs(char** grid, int gridSize, int* gridColSize, int r, int c){
    if (r < 0 || r >= gridSize || c < 0 || c >= *gridColSize || grid[r][c] != '1')
        return;
    grid[r][c] = 'A';
    dfs(grid, gridSize, gridColSize, r + 1, c);
    dfs(grid, gridSize, gridColSize, r - 1, c);
    dfs(grid, gridSize, gridColSize, r, c+1);
    dfs(grid, gridSize, gridColSize, r, c-1);
}
int numIslands(char** grid, int gridSize, int* gridColSize){
    int i, j, cnt=0;
    for (i = 0; i < gridSize; i++){
        for (j = 0; j < *gridColSize; j++){
            if (grid[i][j] == '1'){
                cnt++;
                dfs(grid, gridSize, gridColSize, i, j);
            }
        }
    }
    return cnt;
}

 

posted @ 2020-12-25 22:23  温暖了寂寞  阅读(110)  评论(0编辑  收藏  举报