-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path1197-minimum-knight-moves.py
31 lines (24 loc) · 1019 Bytes
/
1197-minimum-knight-moves.py
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
from collections import deque
class Solution:
def minKnightMoves(self, x: int, y: int) -> int:
offsets = [(1, 2), (2, 1), (2, -1), (1, -2),
(-1, -2), (-2, -1), (-2, 1), (-1, 2)]
def bfs(x, y):
visited = set()
queue = deque([(0, 0)])
steps = 0
while queue:
currLevelCnt = len(queue)
# iterate through the current level
for i in range(currLevelCnt):
currX, currY = queue.popleft()
if (currX, currY) == (x, y):
return steps
for offsetX, offsetY in offsets:
nextX, nextY = currX + offsetX, currY + offsetY
if (nextX, nextY) not in visited:
visited.add((nextX, nextY))
queue.append((nextX, nextY))
# move on to the next level
steps += 1
return bfs(x, y)