题目:
https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8?tpId=13&tqId=11219&tPage=4&rp=3&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking
class Solution:
def movingCount(self, threshold, rows, cols):
# write code here
visit = []
for i in range(rows):
row = []
for j in range(cols):
row.append(False)
visit.append(row)
count = self.MoveingC(threshold, rows, cols, 0, 0,visit )
return count
def getDigit( self ,num ):
sum = 0
while(num > 0):
sum = sum + num % 10
num = num // 10
return sum
def check(self, threshold, rows , cols , row , col , visit):
if( row>= 0 and row=0 and col< cols and self.getDigit(row)+self.getDigit(col) <= threshold and not visit[row][col]):
return True
return False
def MoveingC( self, threshhold, rows ,cols, row , col , visit ):
count = 0
if self.check(threshhold,rows,cols,row,col,visit):
visit[row][col] = True
count = 1 + self.MoveingC(threshhold,rows,cols,row-1,col,visit) + \
self.MoveingC(threshhold,rows,cols,row+1,col,visit)+ \
self.MoveingC(threshhold,rows,cols,row,col-1,visit)+ \
self.MoveingC(threshhold,rows,cols,row,col+1,visit)
return count