-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path1197.py
More file actions
20 lines (18 loc) · 830 Bytes
/
1197.py
File metadata and controls
20 lines (18 loc) · 830 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
__________________________________________________________________________________________________
class Solution(object):
memo = {(0, 0): 0}
queue = [(0, 0, 0)]
for x, y, d in queue:
for dx, dy in ((2, -1), (2, 1), (-2, -1), (-2, 1), (1, -2), (1, 2), (-1, -2), (-1, 2)):
nx = x+dx
ny = y+dy
if -5 <= nx <= 302 and -5 <= ny <= 302:
if (nx, ny) not in memo:
memo[nx,ny] = d+1
queue.append((nx, ny, d+1))
def minKnightMoves(self, x, y):
x = abs(x)
y = abs(y)
return Solution.memo[x,y]
__________________________________________________________________________________________________
__________________________________________________________________________________________________