[牛客网]

13. 机器人的运动范围

## 解法 1：xxxxxxxx

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 # -*- coding:utf-8 -*- class Solution: def movingCount(self, threshold, rows, cols): visited = [[False for c in range(cols)] for r in range(rows)] global ret ret = 0 def sum_all_digits(n): s = 0 while n > 0: s += n % 10 n = n // 10 return s def movingCount_helper(i, j): if (i >= 0 and i < rows and j >= 0 and j < cols and not visited[i][j] and sum_all_digits(i) + sum_all_digits(j) <= threshold): global ret ret += 1 visited[i][j] = 1 movingCount_helper(i + 1, j) movingCount_helper(i - 1, j) movingCount_helper(i, j + 1) movingCount_helper(i, j - 1) movingCount_helper(0, 0) return ret 
• 运行时间：28ms
• 占用内存：5756k