在写这种 bfs 之类的题目时,我经常会遇到这个问题。
其实我大概知道原因,就是变量自动变了,但还是很困惑,不知道到底是哪里变的。
class Solution:
def pondSizes(self, land: List[List[int]]) -> List[int]:
m = len(land)
n = len(land[0])
res = []
for i,row in enumerate(land):
for j, v in enumerate(row):
if v == 0:
q = [(i,j)]
land[i][j] = 1
cnt = 1
while q:
# print(q)
tmp = q
q = []
for (i,j) in tmp:
for dx in range(-1,2):
for dy in range(-1,2):
if dx == dy == 0:
continue
x = i + dx
y = j + dy
if 0
class Solution:
def pondSizes(self, land: List[List[int]]) -> List[int]:
m = len(land)
n = len(land[0])
def bfs(i,j):
q = [(i,j)]
land[i][j] = 1
cnt = 1
while q:
# print(q)
tmp = q
q = []
for (i,j) in tmp:
for dx in range(-1,2):
for dy in range(-1,2):
if dx == dy == 0:
continue
x = i + dx
y = j + dy
if 0