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

机器人的运动范围

https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8

class Solution:
    def movingCount(self, threshold, rows, cols):
        # write code here
        if rows==0 or cols==0:
            return 0
        res =0
        flag = [[0 for _ in range(cols)] for _ in range(rows)]
        print(flag)
        self.dfs(threshold,rows,cols,flag,0,0)
        for i in range(rows):
            for j in range(cols):
                res += flag[i][j]
        return res

    def dfs(self,threshold,rows,cols,flag,i,j):
        if i<0 or i>=rows or j<0 or j>=cols or flag[i][j]==1:
            return
        num = i//10 + i%10 +j//10 +j%10
        if num <= threshold:
            flag[i][j]=1
        else:
            return
        self.dfs(threshold,rows,cols,flag,i-1,j)
        self.dfs(threshold,rows,cols,flag,i+1,j)
        self.dfs(threshold,rows,cols,flag,i,j-1)
        self.dfs(threshold,rows,cols,flag,i,j+1)

全部评论

相关推荐

爱吃的猪猪又被画饼了:问问他消息队列怎么保证消息不丢失的,消息堆积你是怎么解决的
点赞 评论 收藏
分享
宝宝巴逝:给他一巴掌看他还发不发癫
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务