题解 | #岛屿数量#python3 普通的递归dfs
岛屿数量
http://www.nowcoder.com/practice/0c9664d1554e466aa107d899418e814e
#坑就是grid里的元素时字符串形式.. class Solution: def solve(self , grid ): # write code here record = [[0 for e in sublist] for sublist in grid] c = 0 for i in range(len(grid)): for j in range(len(grid[0])): print(record[i][j], grid[i][j]) if record[i][j] == 0 and grid[i][j] == '1': self.dfs(grid, record, i, j) c += 1 return c def dfs(self, grid, record, i, j): record[i][j] = 1 for x, y in [[i-1, j], [i+1, j], [i, j-1], [i, j+1]]: if x < len(grid) and y < len(grid[0]) and x >=0 and y >=0 and record[x][y] == 0 and grid[x][y] == '1': self.dfs(grid, record, x, y)