forked from maverickiam/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dp-on-matrices.cpp
59 lines (44 loc) · 1.01 KB
/
dp-on-matrices.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
const int N = 1003;
int n, m, a[N][N], dp[N][N], vis[N][N];
//Returns the max sum you can achieve
//when you start at (i,j) and end at (n-1, m-1)
//and move only in down and right directions
int go(int i, int j){
if(i == n-1 and j == m-1)
return a[i][j];
if(vis[i][j]) return dp[i][j];
vis[i][j] = 1;
int &ans = dp[i][j];
if(i<n-1 and j<m-1)
ans = a[i][j] + max(go(i, j+1), go(i+1, j));
else if(i == n-1)
ans = a[i][j]+ go(i, j+1);
else
ans = a[i][j] + go(i+1, j);
return ans;
}
int main() {
int i, j;
cin >> n >> m;
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
cin >> a[i][j];
//Display the matrix
for(i = 0; i < n; i++){
for(j = 0; j < m; j++)
cout << a[i][j] << "\t";
cout << endl;
}
//Print the answer
cout << go(0, 0) << endl;
//It should be 73 i.e 1+5+9+13+14+15+16
return 0;
}
//Input:
//4 4
//1 2 3 4
//5 6 7 8
//9 10 11 12
//13 14 15 16