题解 | #机器人的运动范围#
机器人的运动范围
http://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8
dfs
class Solution:
def movingCount(self , threshold: int, rows: int, cols: int) -> int:
# write code here、
def calc_thre(x,y):
ans = 0
for i in str(x):
ans += int(i)
for i in str(y):
ans += int(i)
return ans <= threshold
vis = [[0] * cols for _ in range(rows)]
directions = [[0,1],[0,-1],[1,0],[1,-1]]
def dfs(x,y):
if 0<=x<rows and 0<=y<cols and not vis[x][y] and calc_thre(x,y):
vis[x][y] = 1
num = 1
for dx, dy in directions:
num += dfs(x+dx,y+dy)
return num
return 0
return dfs(0,0)