Skip to content

Commit f778d28

Browse files
author
Partho Biswas
committed
1197. Minimum Knight Moves
1 parent ac5f2ab commit f778d28

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
432432
|21| **[743. Network Delay Time](https://tinyurl.com/yd88jryl)** | [Python](https://tinyurl.com/wu6rdaw/743_Network_Delay_Time.py)| **[Official](https://tinyurl.com/rmcr2xk)**, **[Dijkstra 1](https://tinyurl.com/r8omw8l)**, **[Dijkstra 2](https://tinyurl.com/sscslz7)**, [Vid 1](https://tinyurl.com/vucexbc), **[Art 1](https://tinyurl.com/vbetlaq)**, [Art 2](https://tinyurl.com/yxdzrw3k), **[Art 3](https://tinyurl.com/whnbv4o)**, **[Art 4](https://tinyurl.com/roq3udj)** | Medium | **TODO: Check again. Very Important. Learned (Dijkstra, Bellman, Floyed). Check references section** |
433433
|22| **[261. Graph Valid Tree](https://tinyurl.com/uw9ndt6)** | [Python](https://tinyurl.com/wu6rdaw/261_Graph_Valid_Tree.py)| [Art 1](https://tinyurl.com/yx6ehrfu), **[Art 2](https://tinyurl.com/tlw6vda)**, **[Art 3](https://tinyurl.com/yhs85tj3)**, [Art 4](https://tinyurl.com/ydkfcc5x) | Medium | Important. BFS, DFS and [Union Find](https://tinyurl.com/yhs85tj3) |
434434
|23| **[1168. Optimize Water Distribution in a Village](https://tinyurl.com/ygh8sahd)** | [Python](https://tinyurl.com/wu6rdaw/1168_Optimize_Water_Distribution_in_a_Village.py)| **[Art 1](https://tinyurl.com/yjwwoxv3)**, **[Art 2](https://tinyurl.com/yhc3a7yr)**, **[Art 3](https://tinyurl.com/yf7fjz4v)**, [Art 4](https://tinyurl.com/yh7n9wps) | Hard | TODO: Check AGain. Prim's and Kruskal's algorithm. Important. |
435+
|24| **[1197. Minimum Knight Moves](https://tinyurl.com/svox8jw)** | [Python](https://tinyurl.com/wu6rdaw/1197_Minimum_Knight_Moves.py)| **[Art 1](https://tinyurl.com/wed5c6s)** | Medium | TODO: Check AGain. Important. |
435436

436437

437438
</p>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from collections import deque
2+
3+
# My original code - GOT TLE
4+
class Solution(object):
5+
def minKnightMoves(self, x, y):
6+
"""
7+
:type x: int
8+
:type y: int
9+
:rtype: int
10+
"""
11+
origin, destination = (0, 0), (abs(x), abs(y))
12+
currentLevel = 0
13+
if origin == destination:
14+
return currentLevel
15+
queue = deque([(origin, currentLevel)])
16+
visitedSet = set()
17+
visitedSet.add(origin)
18+
while queue:
19+
currentPosition, currentLevel = queue.popleft()
20+
currentPossileMoves = self.getPossileMoves(currentPosition[0], currentPosition[1])
21+
for move in currentPossileMoves:
22+
if move not in visitedSet and move[0] > -4 and move[1] > -4:
23+
if currentPosition == destination:
24+
return currentLevel
25+
visitedSet.add(move)
26+
queue.append((move, currentLevel + 1))
27+
return currentLevel
28+
29+
def getPossileMoves(self, x, y):
30+
moves = [(x + 2, y + 1), (x + 2, y - 1), (x - 2, y + 1), (x - 2, y - 1), (x + 1, y + 2), (x - 1, y + 2), (x + 1, y - 2), (x - 1, y - 2)]
31+
return moves
32+
33+
34+
35+
36+
37+
# Source: https://tinyurl.com/wed5c6s Same as mine but - GOT Accepted
38+
from collections import deque
39+
class Solution(object):
40+
def minKnightMoves(self, x, y):
41+
"""
42+
:type x: int
43+
:type y: int
44+
:rtype: int
45+
"""
46+
if (x, y)==(0, 0):return 0
47+
def bfs(i, j, x, y):
48+
open_list = deque([(i,j,0)])
49+
seen = {(i, j)}
50+
while open_list:
51+
i, j, d = open_list.popleft()
52+
for di, dj in [(1,2),(2,1),(1,-2),(2,-1),
53+
(-1,2),(-2,1), (-1,-2),(-2,-1)]:
54+
r, c = i+di, j+dj
55+
if (r,c) not in seen and r>-4 and c>-4:
56+
if (r,c)==(x,y):return d+1
57+
seen.add((r,c))
58+
open_list.append((r,c,d+1))
59+
return bfs(0,0,abs(x), abs(y))

0 commit comments

Comments
 (0)