http://www.ideserve.co.in/learn/gold-mine-problem
http://www.geeksforgeeks.org/gold-mine-problem/
https://innosamcodes.wordpress.com/2014/04/17/gold-mine-maximum-amount-of-gold-he-can-collect-and-path-followed-by-him/
Given a gold mine of n*m dimension. Each field in this mine contains an integer which is amount of gold in tons. Initially miner is in first column but could be at any row i. He can move only (right ->, right up /, right down \). Find out maximum amount of gold he can collect and path followed by him.
9 | public int getMaxGold() { |
10 | |
11 | if (goldMine == null || goldMine.length == 0) { |
12 | return 0; |
13 | } |
14 | int rowLength = goldMine.length; |
15 | int colLength = goldMine[0].length; |
16 | |
17 | |
18 | int[][] goldMineTable = new int[rowLength][colLength]; |
19 | for (int i = 0; i < rowLength; i++) { |
20 | for (int j = 0; j < colLength; j++) { |
21 | goldMineTable[i][j] = 0; |
22 | } |
23 | } |
24 | for (int col = colLength - 1; col >= 0; col--) { |
25 | for (int row = 0; row < rowLength; row++) { |
26 | |
27 | int right = col == colLength - 1 ? 0 |
28 | : goldMineTable[row][col + 1]; |
29 | |
30 | int rightUp = (row == 0 || col == colLength - 1 ? 0 |
31 | : goldMineTable[row - 1][col + 1]); |
32 | |
33 | int rightDown = (row == rowLength - 1 || col == colLength - 1 ? 0 |
34 | : goldMineTable[row + 1][col + 1]); |
35 | |
36 | goldMineTable[row][col] = goldMine[row][col] |
37 | + Math.max(rightUp, Math.max(right, rightDown)); |
38 | } |
39 | } |
40 | int max = 0; |
41 | |
42 | for (int i = 0; i < rowLength; i++) { |
43 | max = max < goldMineTable[i][0] ? goldMineTable[i][0] : max; |
44 | } |
45 | return max; |
46 | } |
Create a 2-D matrix goldTable[][]) of the same as given matrix mat[][]. If we observe the question closely, we can notice following.
- Amount of gold is positive, so we would like to cover maximum cells of maximum values under given constraints.
- In every move, we move one step toward right side. So we always end up in last column. If we are at the last column, then we are unable to move right
If we are at the first row or last column, then we are unable to move right-up so just assign 0 otherwise assign the value of goldTable[row-1][col+1] to right_up. If we are at the last row or last column, then we are unable to move right down so just assign 0 otherwise assign the value of goldTable[row+1][col+1] to right up.
Now find the maximum of right, right_up, and right_down and then add it with that mat[row][col]. At last, find the maximum of all rows and first column and return it.
Now find the maximum of right, right_up, and right_down and then add it with that mat[row][col]. At last, find the maximum of all rows and first column and return it.
int
getMaxGold(
int
gold[][MAX],
int
m,
int
n)
{
// Create a table for storing intermediate results
// and initialize all cells to 0. The first row of
// goldMineTable gives the maximum gold that the miner
// can collect when starts that row
int
goldTable[m][n];
memset
(goldTable, 0,
sizeof
(goldTable));
for
(
int
col=n-1; col>=0; col--)
{
for
(
int
row=0; row<m; row++)
{
// Gold collected on going to the cell on the right(->)
int
right = (col==n-1)? 0: goldTable[row][col+1];
// Gold collected on going to the cell to right up (/)
int
right_up = (row==0 || col==n-1)? 0:
goldTable[row-1][col+1];
// Gold collected on going to the cell to right down (\)
int
right_down = (row==m-1 || col==n-1)? 0:
goldTable[row+1][col+1];
// Max gold collected from taking either of the
// above 3 paths
goldTable[row][col] = gold[row][col] +
max(right, max(right_up, right_down));
;
}
}
// The max amount of gold collected will be the max
// value in first column of all rows
int
res = goldTable[0][0];
for
(
int
i=1; i<m; i++)
res = max(res, goldTable[i][0]);
return
res;
}