Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Goldmine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
def collectGold(gold, x, y, n, m):
if ((x < 0) or (x == n) or (y == m)):
return 0

rightUpperDiagonal = collectGold(gold, x - 1, y + 1, n, m)

# right
right = collectGold(gold, x, y + 1, n, m)

# Lower right diagonal
rightLowerDiagonal = collectGold(gold, x + 1, y + 1, n, m)

# Return the maximum and store the value
return gold[x][y] + max(max(rightUpperDiagonal, rightLowerDiagonal), right)


def getMaxGold(gold,n,m):

maxGold = 0

for i in range(n):

# Recursive function call for ith row.
goldCollected = collectGold(gold, i, 0, n, m)
maxGold = max(maxGold, goldCollected)

return maxGold

# Driver Code
gold = [[1, 3, 1, 5],
[2, 2, 4, 1],
[5, 0, 2, 3],
[0, 6, 1, 2]
]

m,n = 4,4
print(getMaxGold(gold, n, m))