# 695:最大岛屿面积
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
def dfs(x,y):
if x < 0 or y < 0 or x >= m or y >= n or grid[x][y] != 1:
return 0
grid[x][y] = 0
ans = 1
for idx, idy in [(0,1),(0,-1),(-1,0),(1,0)]:
new_x, new_y = x + idx, y + idy
if 0 <= new_x < m and 0 <= new_y < n:
ans += dfs(new_x, new_y)
return ans
res = []
for i in range(m):
for j in range(n):
res.append(dfs(i,j))
return max(res)
# 200:岛屿数量
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
m, n = len(grid), len(grid[0])
def dfs(x,y):
if x < 0 or y < 0 or x >= m or y >= n or grid[x][y] != '1':
return
grid[x][y] = 0
for idx, idy in [(0,-1),(0,1),(-1,0),(1,0)]:
new_x, new_y = x + idx ,y + idy
if 0<=new_x<m and 0<=new_y<n:
dfs(new_x,new_y)
return
ans = 0
for i in range(m):
for j in range(n):
if grid[i][j] == '1':
ans += 1
dfs(i,j)
return ans
# 547:省份数量
class Solution:
def findCircleNum(self, isConnected: List[List[int]]) -> int:
def dfs(i):
for j in range(cities):
if isConnected[i][j] == 1 and j not in visited:
visited.append(j)
dfs(j)
return
cities = len(isConnected)
visited = []
ans = 0
for i in range(cities):
if i not in visited:
visited.append(i)
ans += 1
dfs(i)
return ans
# 130:被围绕的区域
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m, n = len(board), len(board[0])
def dfs(i,j):
if i < 0 or j < 0 or i >= m or j >= n or board[i][j] != 'O':
return
board[i][j] = 'A'
for idx,idj in [(0,-1),(0,1),(-1,0),(1,0)]:
new_i, new_j = i+idx, j+idj
if 0 <= new_i < m and 0 <= new_j < n:
dfs(new_i ,new_j)
return
for i in range(m):
if board[i][0] == 'O':
dfs(i,0)
if board[i][n-1] == 'O':
dfs(i,n-1)
for j in range(n):
if board[0][j] == 'O':
dfs(0,j)
if board[m-1][j] == 'O':
dfs(m-1,j)
for i in range(m):
for j in range(n):
if board[i][j] == 'A':
board[i][j] = 'O'
else:
board[i][j] = 'X'
return board
# 417: 太平洋大西洋水流问题
class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
m, n = len(heights), len(heights[0])
def dfs1(i, j, visited1):
if i == 0 or j == 0:
return True
visited1.add((i, j))
for idx, idj in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
new_i, new_j = i + idx, j + idj
if 0 <= new_i < m and 0 <= new_j < n and heights[new_i][new_j] <= heights[i][j] and (
new_i, new_j) not in visited1:
if dfs1(new_i, new_j,visited1):
return True
return False
def dfs2(i, j,visited2):
if i == m - 1 or j == n - 1:
return True
visited2.add((i, j))
for idx, idj in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
new_i, new_j = i + idx, j + idj
if 0 <= new_i < m and 0 <= new_j < n and heights[new_i][new_j] <= heights[i][j] and (
new_i, new_j) not in visited2:
if dfs2(new_i, new_j,visited2):
return True
return False
res1 = set()
res2 = set()
for cur_i in range(m):
for cur_j in range(n):
if dfs1(cur_i, cur_j, set()):
res1.add((cur_i, cur_j))
if dfs2(cur_i, cur_j,set()):
res2.add((cur_i, cur_j))
return list(res1 & res2)
#1631: 最小体力消耗路径
def minimumEffortPath(heights) -> int:
a, b = len(heights),len(heights[0])
res = [[999999] * b for _ in range(a)]
visited = []
def dfs(x,y):
if x == a or y == b or x == -1 or y == -1:
return
if (x,y) in visited:
return
for idx, idy in [(-1,0),(1,0),(0,1),(0,-1)]:
if (x,y) not in visited:
visited.append((x, y))
if (x + idx <= a-1) and (x + idx >= 0) and (y + idy <= b-1) and (y + idy >= 0):
res[x + idx][y + idy] = min(res[x + idx][y + idy], abs(heights[x + idx][y + idy]-heights[x][y]))
dfs(x + idx,y + idy)
dfs(0,0)
return res[-1][-1]