Problem :

https://leetcode.com/problems/number-of-islands/


My Solution :

class Solution:
def numIslands(self, grid: 'List[List[str]]') -> 'int':
count = 0

def dfs(row, col):
if grid[row][col] == '0':
return
grid[row][col] = '0'
for y, x in ((row-1, col), (row+1, col),
(row, col-1), (row, col+1)):
if 0 <= y < len(grid) and 0 <= x < len(grid[0]):
dfs(y, x)

for row in range(len(grid)):
for col in range(len(grid[0])):
if grid[row][col] == '1':
count += 1
dfs(row, col)

return count


Comment :

가장 전형적인 DFS 문제 중 하나이다.