Title Description
A ground grid and m rows n columns. A robot moves from the grid coordinates 0,0, every time only left, right, upper, lower four directions a cell, but can not enter the row and column coordinates of the grid is greater than the sum of the number of bits k. For example, when k is 18, the robot can enter the box (35, 37), because 3 + 3 + 5 + 7 = 18. However, it can not enter the box (35, 38), because 3 + 3 + 5 + 8 = 19. Will the robot be able to reach the number of lattice?
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.count = 0 def movingCount(self, threshold, rows, cols): # write code here arr = [[1 for i in range(rows)]for j in range(cols)] self.count_path(arr,0,0,threshold) return self.count def count_path(self,arr,i,j,k): if i<0 or j<0 or i>=len(arr) or j>=len(arr[0]): return tmpi = list(map(int, list(str(i)))) tmpj = list(map(int, list(str(j)))) if sum(tmpi)+sum(tmpj)>k or arr[i][j]!=1: return arr[i][j] = 0 self.count+=1 self.count_path(arr,i+1,j,k) self.count_path(arr,i-1,j,k) self.count_path(arr,i,j+1,k) self.count_path(arr,i,j-1,k)