题解 | #机器人的运动范围#
机器人的运动范围
https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param threshold int整型
# @param rows int整型
# @param cols int整型
# @return int整型
#
class Solution:
def movingCount(self , threshold: int, rows: int, cols: int) -> int:
# write code here
self.record=[[0 for i in range(cols)] for i in range(rows)]
self.run(threshold, rows, cols,0,0)
result=0
for i in range(rows):
for j in range(cols):
result+=self.record[i][j]
return result
def checksum(self,threshold, i, j):
if i//10+i%10+j//10+j%10 >threshold:
return False
return True
def checkpos(self, rows, cols, i, j):
if i<0 or j<0 or i>=rows or j>=cols:
return False
return True
def run(self,threshold, rows, cols,i,j):
if self.checksum(threshold,i,j) and self.checkpos( rows, cols, i, j) and self.record[i][j]==0: # 当前位置可以
print(i,j)
self.record[i][j]=1
self.run(threshold, rows, cols,i+1,j)
self.run(threshold, rows, cols,i-1,j)
self.run(threshold, rows, cols,i,j+1)
self.run(threshold, rows, cols,i,j-1)
return 0
else:
return 0

