|
| 1 | +# Gold Mine Problem using Dynamic Programming |
| 2 | +Language used : **Python 3** |
| 3 | + |
| 4 | +## 🎯 Aim |
| 5 | +The aim of this script is to find out the maximum amount of gold that can be collected from the gold mine. |
| 6 | + |
| 7 | +## 👉 Purpose |
| 8 | +The main purpose of this script is to show the implementation of Dynamic Programming to find out the maximum amount of gold can be collected from the gold mine. |
| 9 | + |
| 10 | +## 📄 Description |
| 11 | +Given a gold mine of n*m dimensions. Each field in this mine contains a positive integer which is the amount of gold in tons. Initially the miner is at first column but can be at any row. He can move only `(right->,right up /,right down\)` that is from a given cell, the miner can move to the cell diagonally up towards the right or right or diagonally down towards the right. Find out maximum amount of gold he can collect. |
| 12 | +Examples: |
| 13 | + |
| 14 | +``` |
| 15 | +Input : mat[][] = {{1, 3, 3}, |
| 16 | + {2, 1, 4}, |
| 17 | + {0, 6, 4}}; |
| 18 | +Output : 12 |
| 19 | +{(1,0)->(2,1)->(1,2)} |
| 20 | +
|
| 21 | +Input: mat[][] = { {1, 3, 1, 5}, |
| 22 | + {2, 2, 4, 1}, |
| 23 | + {5, 0, 2, 3}, |
| 24 | + {0, 6, 1, 2}}; |
| 25 | +Output : 16 |
| 26 | +(2,0) -> (1,1) -> (1,2) -> (0,3) OR |
| 27 | +(2,0) -> (3,1) -> (2,2) -> (2,3) |
| 28 | +``` |
| 29 | +## 🧮 Algorithm |
| 30 | +- Create a 2-D matrix `goldTable[][])` of the same as given matrix `mat[][]`. If we observe the question closely, we can notice following. |
| 31 | +- Amount of gold is positive, so we would like to cover maximum cells of maximum values under given constraints. |
| 32 | +- 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 |
| 33 | +- 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. |
| 34 | +- 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. |
| 35 | + |
| 36 | +## 💻 Input and Output |
| 37 | +- **Test Case 1 :** |
| 38 | +``` |
| 39 | +Input Given : |
| 40 | +{ {1, 3, 1, 5}, |
| 41 | +{2, 2, 4, 1}, |
| 42 | +{5, 0, 2, 3}, |
| 43 | +{0, 6, 1, 2}}; |
| 44 | +``` |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +- **Test Case 2 :** |
| 49 | +``` |
| 50 | +Input Given : |
| 51 | +{{1, 3, 3}, |
| 52 | +{2, 1, 4}, |
| 53 | +{0, 6, 4}}; |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | +## ⏰ Time and Space complexity |
| 58 | +- **Time Complexity :** `O(m*n)`. |
| 59 | +- **Space Complexity :** `O(m*n)`. |
| 60 | + |
| 61 | +--------------------------------------------------------------- |
| 62 | +## 🖋️ Author |
| 63 | +**Code contributed by, _Abhishek Sharma_, 2021 [@abhisheks008](github.com/abhisheks008)** |
| 64 | + |
| 65 | +[](https://www.python.org/) |
0 commit comments