Advertisement
Fastrail08

Path with Maximum Gold (Goldmine)

Jun 4th, 2025
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int startMining(int row, int col, vector<vector<int> > &mine, vector<vector<int> > &memo){
  5.     if(row < 0 || row >= mine.size() || col >= mine.size()){
  6.         return 0;
  7.     }
  8.    
  9.     //memo check
  10.     if(memo[row][col] != 0){
  11.         return memo[row][col];
  12.     }
  13.     /*
  14.     Levels/States = Each Cell
  15.     Options/Transitions = 3 options (i - 1, j + 1), (i, j + 1), (i + 1, j + 1)
  16.     */
  17.     int upperRight = startMining(row - 1, col + 1, mine, memo);
  18.     int right = startMining(row, col + 1, mine, memo);
  19.     int lowerRight = startMining(row + 1, col + 1, mine, memo);
  20.    
  21.     return memo[row][col] = mine[row][col] +  max(upperRight, max(right, lowerRight));
  22. }
  23.  
  24.  
  25. int startMiningDP(vector<vector<int> > &mine){
  26.     //Storage & Meaning -  dp[i][j] = maximum gold that can be collected if we started from left wall and ended on jth wall
  27.     int n = mine.size();
  28.     int m = mine[0].size();
  29.     vector<vector<int> > dp(n, vector<int>(m, 0));
  30.    
  31.     // Direction - Smallest Problem = dp[i][m - 1] = if we were on the right wall (analyse recursive calls)
  32.     // for computing dp[i][j] = we should have answers to dp[i - 1][j + 1], dp[i][j + 1], dp[i + 1][j + 1];
  33.     // So that means we should be starting from right wall(smallest case), as for any column j, we should already have answer for j + 1, as seen above in recursive calls
  34.    
  35.     // Smallest Problem (The maximum gold that we can get if we are on the right wall, is the gold present in that mine cell) as after collecting the gold from that cell, we move on to next column, which will out of bounds.
  36.     // Also you can think of smallest problem from -
  37.     // Original Problem = Start on left wall (0th column) & End on right wall(m - 1)th column, what is the maximum
  38.     // Smallest Problem = Start on the right wall(m - 1)th column, & End on right wall (m - 1)th column, what is the maximum gold that we can get
  39.     for(int i = 0; i < n; i++){
  40.         dp[i][m - 1] = mine[i][m - 1];
  41.     }
  42.    
  43.     // Travel & Solve - As we figured out the smallest problem is at the right wall and largest problem is at left wall, we will traverse from right wall to left wall
  44.     // Try all the 3 options for each cell to figure out the optimal value for each cell
  45.     int maxGold = 0;
  46.     for(int j = m - 2; j >= 0; j--){
  47.         for(int i = 0; i < n; i++){
  48.             int upperRight = 0, right = 0, lowerRight = 0;
  49.            
  50.             //only call if we aren't in 0th row
  51.             if(i != 0){
  52.                 upperRight = dp[i - 1][j + 1];
  53.             }
  54.            
  55.             //only call if we aren't in the (n - 1)th row
  56.             if(i < n - 1){
  57.                 lowerRight = dp[i + 1][j + 1];
  58.             }
  59.             // this is always in bound
  60.             right = dp[i][j + 1];
  61.            
  62.             dp[i][j] = mine[i][j] + max(upperRight, max(right, lowerRight));
  63.             // We reached the left wall by filling from right to left, when we reach 0th column, it is the original problem
  64.             // But we need the maximum of the values in 0th column, as we could have started on any row on the left wall
  65.             if(j == 0){
  66.                 maxGold = max(maxGold, dp[i][0]);
  67.             }
  68.         }
  69.     }
  70.     return maxGold;
  71.    
  72. }
  73.  
  74. int main() {
  75.     // your code goes here
  76.     int n, m;
  77.     cin >> n >> m;
  78.     vector<vector<int> > mine(n, vector<int>(m, 0));
  79.     for(int i = 0; i < n; i++){
  80.         for(int j = 0; j < m; j++){
  81.             cin >> mine[i][j];
  82.         }
  83.     }
  84.     vector<vector<int> > memo(n, vector<int>(m, 0));
  85.     int maxGold = INT_MIN;
  86.     for(int i = 0; i < n; i++){
  87.          maxGold = max(maxGold, startMining(i, 0, mine, memo));
  88.     }
  89.     cout << maxGold << '\n';
  90.     cout << startMiningDP(mine) << '\n';
  91. }
  92.  
  93. /*
  94. 6 6
  95. 0 1 4 2 8 2
  96. 4 3 6 5 0 4
  97. 1 2 4 1 4 6
  98. 2 0 7 3 2 2
  99. 3 1 5 9 2 4
  100. 2 7 0 8 5 1
  101.  
  102.  
  103. 10 10
  104. 23 87 56 14 98 69 35 90 47 62
  105. 6 72 91 31 80 25 53 39 18 66
  106. 77 61 10 42 58 85 95 21 36 74
  107. 40 12 63 89 17 50 99 68 29 26
  108. 81 33 52 96 45 19 13 43 59 3
  109. 9 16 97 24 65 32 7 30 55 71
  110. 44 84 92 1 73 37 22 93 20 34
  111. 67 5 11 64 28 46 70 38 94 15
  112. 2 76 48 60 8 57 41 49 27 75
  113. 88 100 4 78 83 51 86 79 19 54
  114.  
  115.  
  116. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement