题解 | #机器人的运动范围#

机器人的运动范围

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


全部评论

相关推荐

04-25 18:13
五邑大学 Java
后来123321:大二两段实习太厉害了,我现在大二连面试都没有
点赞 评论 收藏
分享
醉蟀:你是我今年见过的最美牛客女孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务