In this example, I have shared the gold mine problem in Java. There are gold mines along a river, and each mine produces tons of gold. In order to collect the mined gold, we want to redistribute and consolidate.
Given a gold mine of n*m measurements, On every cell right now a positive whole number which is the measure of gold in tons. At first, the excavator is at the main segment yet can be at any column. He can move just right or straight up or directly down from the present cell, i.e., the digger can move to the cell corner to corner up towards the privilege or right or slantingly down towards the right. Discover the most extreme measure of gold he can gather.
Below is an example of Gold mine problem in Java programming language.
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 | import java.util.Arrays; class GMP { static final int MAX = 100; static int getMaxGold(int gold[][], int m, int n) { int goldTable[][] = new int[m][n]; for(int[] rows:goldTable) Arrays.fill(rows, 0); 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] + Math.max(right, Math.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 = Math.max(res, goldTable[i][0]); return res; } public static void main(String arg[]) { int gold[][]= { {1, 3, 1, 5}, {2, 2, 4, 1}, {5, 0, 2, 3}, {0, 6, 1, 2} }; int m = 4, n = 4; System.out.print(getMaxGold(gold, m, n)); } } |
Program Output
1 | 16 |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.