-
Notifications
You must be signed in to change notification settings - Fork 0
/
1219. Path with Maximum Gold.py
75 lines (46 loc) · 2.08 KB
/
1219. Path with Maximum Gold.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
72
73
74
75
class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
cur_gold = 0
max_gold = 0
r, c = len(grid), len(grid[0])
visited = [[0 for i in range(c)] for j in range(r)]
options = [[-1,0],[1,0],[0,1],[0,-1]]
def backtrack(x, y, r, c, grid):
nonlocal cur_gold, max_gold
if grid[x][y] == 0:
return
# add to cur_gold amount
cur_gold += grid[x][y]
# check max
max_gold = max(max_gold, cur_gold)
# take gold out from cur suqare
gold_at_cell = grid[x][y]
grid[x][y] = 0
for i, j in options:
next_x, next_y = x +i, y+j
if 0 <= next_x < r and 0 <= next_y < c:
backtrack(next_x, next_y, r, c, grid)
cur_gold -= gold_at_cell
grid[x][y] = gold_at_cell
def dfs1(x, y, r, c, visited, grid):
if x < 0 or x >= r or y < 0 or y >= c or visited[x][y] or grid[x][y] == 0:
return 0
visited[x][y] = 1
left = dfs1(x,y-1,r,c, visited, grid)
right = dfs1(x,y+1,r,c, visited, grid)
up = dfs1(x-1,y,r,c, visited, grid)
down = dfs1(x+1,y,r,c, visited, grid)
visited[x][y] = 0
return max(left, right, up, down) + grid[x][y]
"""
res = 0
for i in range(r):
for j in range(c):
if grid[i][j]:
res = max(res, dfs1(i,j,r,c,visited,grid))
return res
"""
for i in range(r):
for j in range(c):
backtrack(i,j, r, c, grid)
return max_gold