标签:solution mamicode inf 递推 dfs info while 机器 empty
题目描述:
方法一: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)
标签:solution mamicode inf 递推 dfs info while 机器 empty
原文地址:https://www.cnblogs.com/oldby/p/12663843.html