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

机器人的运动范围

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

dfs

class Solution:
    def movingCount(self , threshold: int, rows: int, cols: int) -> int:
        # write code here、
        def calc_thre(x,y):
            ans = 0
            for i in str(x):
                ans += int(i)
            for i in str(y):
                ans += int(i)
            return ans <= threshold
        
        vis = [[0] * cols for _ in range(rows)]
        directions = [[0,1],[0,-1],[1,0],[1,-1]]
        
        def dfs(x,y):
            if 0<=x<rows and 0<=y<cols and not vis[x][y] and calc_thre(x,y):
                vis[x][y] = 1
                num = 1
                for dx, dy in directions:
                    num += dfs(x+dx,y+dy)
                return num
            return 0
            
        return dfs(0,0)       
               
全部评论
有一点没搞懂,案例1按你这个dfs(0,0)走到(0,1)就没得走了呀,是怎么找到(1,0)的呢
点赞 回复 分享
发布于 2022-12-05 21:33 江苏

相关推荐

评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务