how to

250119

Feb 5, 2025
notesjulyfun工作william
1 Minutes
135 Words

课堂小结:dfs 可用于搜索连通块大小,暴力枚举所有情况等。dfs 在网格中的写法:

1
dir = [(0, 1), (1, 0), (-1, 0), (0, -1)]
2
def dfs(x, y):
3
global area
4
area += 1
5
vis[x][y] = True
6
for dx, dy in dir:
7
tx, ty = x+dx, y+dy
8
if 0 <= tx <= n -1 and 0 <= ty <= n-1 and a[tx][ty] == '#' and not vis[tx][ty]:
9
dfs(tx, ty)

和 bfs 类似,枚举 4 个方向。搜索连通块和枚举所有情况时都要注意,判断每个状态是否已经走过,可用二维数组或者 set 判断.

Article title:250119
Article author:Julyfun
Release time:Feb 5, 2025
Copyright 2025
Sitemap