Advertisement
Fastrail08

Gold Mine 2

Apr 27th, 2025
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int dx[4] = {-1, 0, 1, 0};
  5. const int dy[4] = {0, 1, 0, -1};
  6.  
  7. int getGold(int r, int c, vector<vector<int> > &matrix){
  8.     if(r < 0 || r >= matrix.size() || c < 0 || c >= matrix[0].size() || matrix[r][c] == 0){
  9.         return 0;
  10.     }
  11.     int currCellGold = matrix[r][c];
  12.     matrix[r][c] = 0;
  13.     for(int i = 0; i < 4; i++){
  14.         currCellGold += getGold(r + dx[i], c + dy[i], matrix);
  15.     }
  16.     return currCellGold;
  17. }
  18.  
  19. int main() {
  20.     // your code goes here
  21.     int n, m;
  22.     cin >> n >> m;
  23.     vector<vector<int> > matrix(n, vector<int>(m, 0));
  24.     for(int i = 0; i < n; i++){
  25.         for(int j = 0; j < m; j++){
  26.             cin >> matrix[i][j];
  27.         }
  28.     }
  29.     int maxGold = 0;
  30.     int maxInAPath = 0;
  31.     for(int i = 0; i < n; i++){
  32.         for(int j = 0; j < m; j++){
  33.             if(matrix[i][j] != 0){
  34.                 // maxGold += getGold(i, j, matrix);
  35.                 int recGold = 0;
  36.                 recGold = getGold(i, j, matrix);
  37.                 maxGold += recGold;
  38.                 maxInAPath = max(maxInAPath, recGold);
  39.             }
  40.         }
  41.     }
  42.     cout << maxGold << '\n';
  43.     cout << maxInAPath << '\n';
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement