Advertisement
El_GEMMY

The maximum path sum

Mar 26th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3.  
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6.  
  7. typedef long long ll;
  8. typedef unsigned long long ull;
  9. typedef tree<int,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
  10.  
  11. #define all(v) v.begin(),v.end()
  12. #define rall(v) v.rbegin(),v.rend()
  13. #define MOD 1000000007
  14. #define PI 3.14159265
  15. #define ceil(a, b) ((a / b) + (a % b ? 1 : 0))
  16. #define imin INT_MIN
  17. #define imax INT_MAX
  18. #define nl '\n'
  19.  
  20. void Start_Crushing() {
  21.     ios::sync_with_stdio(false);
  22.     cin.tie(nullptr);
  23.     cout.tie(nullptr);
  24. #ifndef ONLINE_JUDGE
  25.     freopen("input.txt", "r", stdin);
  26.     freopen("output.txt", "w", stdout);
  27. #endif
  28. }
  29. //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
  30. //vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
  31. int n, m;
  32. vector<vector<int>> grid;
  33.  
  34. ll maxPath(int x = 0, int y = 0, ll path = 0){
  35.     if(x == n - 1 and y == m - 1)
  36.         return path + grid[x][y];
  37.     ll res = imin;
  38.  
  39.     if(x < n - 1)
  40.         res = max(res, maxPath(x + 1, y, path + grid[x][y]));
  41.     if(y < m - 1)
  42.         res = max(res, maxPath(x, y + 1, path + grid[x][y]));
  43.  
  44.     return res;
  45. }
  46.  
  47. void solve(){
  48.     cin >> n >> m;
  49.     grid.assign(n, vector<int>(m));
  50.     for(auto& i : grid){
  51.         for(auto& j : i) cin >> j;
  52.     }
  53.     cout << maxPath();
  54. }
  55.  
  56. int main(){
  57. //    freopen("cakes.in", "r", stdin);
  58.     Start_Crushing();
  59.  
  60.     int t = 1;
  61. //        /*is Single Test case?*/ cin >> t;
  62.     while (t--) {
  63.         solve();
  64.         if(!t) break;
  65.         cout << "\n";
  66.     }
  67.  
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement