Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- // best path
- const int N = 100;
- int a[N][N];
- int n, m;
- bool valid (int row, int col) {
- return (row >= 0 && col >= 0 && row < n && col < m);
- }
- int best_path (int row, int col) {
- if (!valid(row, col))
- return false;
- if(row == n - 1 && col == m - 1)
- return a[row][col];
- int path1 = best_path(row + 1, col);
- int path2 = best_path(row, col + 1);
- return a[row][col] + max(path1, path2);
- }
- int main () {
- // freopen("/home/hamoudi/clion/hello.in", "rt", stdin);
- cin >> n >> m;
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- cin >> a[i][j];
- }
- }
- cout << best_path(0, 0) << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement