-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsw_13.py
More file actions
34 lines (30 loc) · 805 Bytes
/
sw_13.py
File metadata and controls
34 lines (30 loc) · 805 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution:
def movingCount(self, m: int, n: int, k: int) -> int:
result, queue, visited = 1, [(0, 0)], {(0, 0)}
while queue:
node = queue.pop(0)
row, col = node[0], node[1]
for x, y in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
newRow, newCol = row + x, col + y
if 0 <= newRow < m and 0 <= newCol < n \
and (newRow, newCol) not in visited \
and self.getNum(newRow) + self.getNum(newCol) <= k:
result += 1
visited.add((newRow, newCol))
queue.append((newRow, newCol))
return result
def getNum(self, num):
result = 0
while num > 0:
result += num % 10
num = num // 10
return result
m = 2
n = 3
k = 1
m = 3
n = 1
k = 0
s = Solution()
result = s.movingCount(m, n, k)
print(result)