标签:坐标 范围 val 初始 hold count 有一个 ali 入行
题目描述class Solution:
def movingCount(self, threshold, rows, cols):
# 判断(row, col)是否可以进入:位置合法且未进入过
def isValid(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols:
return False
num = 0
while row > 0:
num += row % 10
row //= 10
while col > 0:
num += col % 10
col //= 10
return num <= threshold
def helper(row, col):
cnt = 0 # 对于每个位置,在确认过可以访问之前先初始化为0
if isValid(row, col) and not visited[row][col]:
# 如果(row, col)可以进入,那么将其设为已访问,然后对四个邻居进行访问
visited[row][col] = True
cnt = (1 + helper(row + 1, col) + helper(row - 1, col)
+ helper(row, col + 1) + helper(row, col - 1))
return cnt
if threshold < 0:
return 0
visited = [[False] * cols for _ in range(rows)]
return helper(0, 0)
标签:坐标 范围 val 初始 hold count 有一个 ali 入行
原文地址:https://blog.51cto.com/jayce1111/2380661