200. 岛屿数量

class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        def dfs(nr, nc, grid):
            grid[nr][nc] = '0'
            for x,y in [(nr - 1, nc),(nr + 1, nc),(nr, nc + 1),(nr, nc - 1)]:
                if 0 <= x < len(grid) and 0 <= y < len(grid[0]) and grid[x][y] == '1':
                    print(x,y)
                    dfs(x, y, grid)
        if len(grid) == 0:
            return 0
        row = len(grid)
        col = len(grid[0])
        res = 0
        for nr in range(row):
            for nc in range(col):
                if grid[nr][nc] == '1':
                    res += 1
                    dfs(nr, nc, grid)
        return res
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页