# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.count = 0
def movingCount(self, threshold, rows, cols):
# write code here
def helper(i, j):
if i<0 or i>=rows or j<0 or j>=cols:
return
tmpi = [int(item) for item in str(i)]
tmpj = [int(item) for item in str(j)]
if sum(tmpi) + sum(tmpj) > threshold or visited[i][j] != 0:
return
# 如果能执行到这一步说明和<=k,且之前未访问过
visited[i][j] = 1
self.count += 1
helper(i+1, j)
helper(i-1, j)
helper(i, j+1)
helper(i, j-1)
visited = [[0 for i in range(cols)] for j in range(rows)]
# 未访问过的为0,访问过的为1
helper(0, 0)
return self.count
剑指Offer66-机器人的运动范围(记忆)
最新推荐文章于 2022-03-08 19:48:08 发布