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

机器人的运动范围

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)

全部评论

相关推荐

废物一个0offer:认真的吗二本本科找人工智能岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务