fork download
  1. #include <bits/stdc++.h>
  2. #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  3. using namespace std;
  4.  
  5. int path_sum(int grid[100][100], int row, int col, int ROWS, int COLS) {
  6. if (row == ROWS - 1 && col == COLS - 1) return grid[row][col];
  7.  
  8. int max_val = 0;
  9. int next_row = row, next_col = col;
  10.  
  11. // Check all 3 possible directions
  12. if (row + 1 < ROWS && grid[row + 1][col] > max_val) {
  13. max_val = grid[row + 1][col];
  14. next_row = row + 1;
  15. next_col = col;
  16. }
  17. if (col + 1 < COLS && grid[row][col + 1] > max_val) {
  18. max_val = grid[row][col + 1];
  19. next_row = row;
  20. next_col = col + 1;
  21. }
  22. if (row + 1 < ROWS && col + 1 < COLS && grid[row + 1][col + 1] > max_val) {
  23. max_val = grid[row + 1][col + 1];
  24. next_row = row + 1;
  25. next_col = col + 1;
  26. }
  27.  
  28. return grid[row][col] + path_sum(grid, next_row, next_col, ROWS, COLS);
  29. }
  30. void solve() {
  31. int r = 3,c=3;
  32. int grid[100][100] = {{1,7,8},{2,10,11},{20,5,9}};
  33. cout << path_sum(grid,0,0,r,c);
  34. }
  35.  
  36. int main() {
  37. IOS;
  38. solve();
  39. return 0;
  40. }
Success #stdin #stdout 0s 5300KB
stdin
Standard input is empty
stdout
31