Advertisement
Hamoudi30

best path

Jul 29th, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. // best path
  4. const int N = 100;
  5. int a[N][N];
  6. int n, m;
  7. bool valid (int row, int col) {
  8.     return (row >= 0 && col >= 0 && row < n && col < m);
  9. }
  10. int best_path (int row, int col) {
  11.     if (!valid(row, col))
  12.         return false;
  13.     if(row == n - 1 && col == m - 1)
  14.         return a[row][col];
  15.     int path1 = best_path(row + 1, col);
  16.     int path2 = best_path(row, col + 1);
  17.     return a[row][col] + max(path1, path2);
  18. }
  19. int main () {
  20. //    freopen("/home/hamoudi/clion/hello.in", "rt", stdin);
  21.     cin >> n >> m;
  22.     for (int i = 0; i < n; ++i) {
  23.         for (int j = 0; j < m; ++j) {
  24.             cin >> a[i][j];
  25.         }
  26.     }
  27.     cout << best_path(0, 0) << '\n';
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement