-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#23.py
62 lines (46 loc) · 1.5 KB
/
#23.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sys
def minSteps(board, start, end):
memo = {}
def nextStep(curr):
memo[curr] = -1
rows = len(board)
cols = len(board[0])
minimum = sys.maxsize
for row, col in [(curr[0]-1, curr[1]), (curr[0]+1, curr[1]), (curr[0], curr[1]-1), (curr[0], curr[1]+1)]:
# Base Case (1 step to reach end coordinate)
if row == end[0] and col == end[1]:
memo[curr] = 1
return memo[curr]
# Verifies coordinates are in bounds and are not a wall
if row >= 0 and row < rows and col >= 0 and col < cols and not board[row][col]:
# Checks if steps needed was already computed
stepsToEnd = memo.get((row, col))
# -1 means not possible to get to end from current tile
if stepsToEnd != -1:
# None means not yet computed
if stepsToEnd != None:
if 1 + stepsToEnd < minimum:
minimum = 1 + stepsToEnd
else:
steps = nextStep((row, col))
if steps != -1 and 1 + steps < minimum:
minimum = 1 + steps
if minimum != sys.maxsize:
memo[curr] = minimum
# Return value is either -1 (Not Possible) or the least number of steps
return memo[curr]
return nextStep(start)
board = [[0, 0, 0, 0],
[1, 1, 1, 0],
[0, 0, 0, 0],
[0, 1, 1, 1],
[0, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 0, 0]]
start = [int(x) for x in input("Start: ").split(',')]
end = [int(x) for x in input("End: ").split(',')]
steps = minSteps(board, (start[0], start[1]), (end[0], end[1]))
if (steps == -1):
print("Not Possible")
else:
print("Minimum Steps Needed: " + str(steps))