-
Notifications
You must be signed in to change notification settings - Fork 5
/
gold_mine.cpp
40 lines (25 loc) · 951 Bytes
/
gold_mine.cpp
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
#include<bits/stdc++.h>
using namespace std;
const int MAX = 100;
int getMaxGold(int gold[][MAX], int m, int n)
{
int goldTable[m][n];
memset(goldTable, 0, sizeof(goldTable));
for (int col=n-1; col>=0; col--)
{
for (int row=0; row<m; row++)
{
int right = (col==n-1)? 0: goldTable[row][col+1];
int right_up = (row==0 || col==n-1)? 0:
goldTable[row-1][col+1];
int right_down = (row==m-1 || col==n-1)? 0:
goldTable[row+1][col+1];
goldTable[row][col] = gold[row][col] +
max(right, max(right_up, right_down));
}
}
int res = goldTable[0][0];
for (int i=1; i<m; i++)
res = max(res, goldTable[i][0]);
return res;
}