문제 보러 가기 👈 클릭!
class Solution:
def dfs_recursion(self, grid, i, j):
if (i < 0 or i >= len(grid)) or (j < 0 or j >= len(grid[0])) or grid[i][j] != '1':
return
grid[i][j] = '0'
self.dfs_recursion(grid, i - 1, j)
self.dfs_recursion(grid, i + 1, j)
self.dfs_recursion(grid, i, j - 1)
self.dfs_recursion(grid, i, j + 1)
def numIslands(self, grid: List[List[str]]) -> int:
cnt = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '1':
self.dfs_recursion(grid, i, j)
cnt += 1
return cnt