forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
minimum-cost-to-make-at-least-one-valid-path-in-a-grid.py
71 lines (65 loc) · 2.18 KB
/
minimum-cost-to-make-at-least-one-valid-path-in-a-grid.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
63
64
65
66
67
68
69
70
71
# Time: O(m * n)
# Space: O(m * n)
import collections
# A* Search Algorithm without heap
class Solution(object):
def minCost(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
def a_star(grid, b, t):
R, C = len(grid), len(grid[0])
f, dh = 0, 1
closer, detour = [b], []
lookup = set()
while closer or detour:
if not closer:
f += dh
closer, detour = detour, closer
b = closer.pop()
if b == t:
return f
if b in lookup:
continue
lookup.add(b)
for nd, (dr, dc) in enumerate(directions, 1):
nb = (b[0]+dr, b[1]+dc)
if not (0 <= nb[0] < R and 0 <= nb[1] < C and nb not in lookup):
continue
(closer if nd == grid[b[0]][b[1]] else detour).append(nb)
return -1
return a_star(grid, (0, 0), (len(grid)-1, len(grid[0])-1))
# Time: O(m * n)
# Space: O(m * n)
# 0-1 bfs solution
class Solution2(object):
def minCost(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
R, C = len(grid), len(grid[0])
b, t = (0, 0), (R-1, C-1)
dq = collections.deque([(b, 0)])
lookup = {b: 0}
while dq:
b, d = dq.popleft()
if b == t:
return d
if lookup[b] < d:
continue
for nd, (dr, dc) in enumerate(directions, 1):
nb = (b[0]+dr, b[1]+dc)
cost = 1 if nd != grid[b[0]][b[1]] else 0
if not (0 <= nb[0] < R and 0 <= nb[1] < C and
(nb not in lookup or lookup[nb] > d+cost)):
continue
lookup[nb] = d+cost
if not cost:
dq.appendleft((nb, d))
else:
dq.append((nb, d+cost))
return -1 # never reach here