leetcode-面试题13-机器人的运动范围

 题目描述:

leetcode-面试题13-机器人的运动范围

 方法一:dfs/bfs

class Solution:
    def movingCount(self, m: int, n: int, k: int) -> int:
        def digitsum(n):
            ans = 0
            while n:
                ans += n %10
                n//=10
            return ans
        from queue import Queue
        q = Queue()
        q.put((0,0))
        s = set()
        while not q.empty():
            x,y = q.get()
            if (x,y) not in s and 0 <=x <m and 0<=y<n and digitsum(x)+digitsum(y) <=k:
                s.add((x,y))
                for nx,ny in [(x+1,y),(x,y+1)]:
                    q.put((nx,ny))
        return len(s)

方法二:递推

class Solution:
    def movingCount(self, m: int, n: int, k: int) -> int:
        def digitsum(n):
            ans = 0
            while n:
                ans += n %10
                n//=10
            return ans
        vis = set([(0,0)])
        for i in range(m):
            for j in range(n):
                if ((i-1,j) in vis or (i,j-1) in vis) and digitsum(i) + digitsum(j) <= k:
                    vis.add((i,j))
        return len(vis)

相关推荐