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<rows and col>=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
面试题67: 机器人的运动范围
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 20170727搜狐面试算法实习生 岗位搜狐后台开发(机器学习NLP)10:00--11:101、首先自我介绍,b...