Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int DP[100][100];
- int Mat[100][100];
- int row,col;
- int call(int r,int c,bool taken){
- if(r >= row) return 0;
- if(DP[r][c] != -1) return DP[r][c];
- int res = 0;
- if(taken == true){
- if(c >= col){
- int ret1 = call(r+2,0,false);
- int ret2 = call(r+2,0,true);
- res = max(ret1,ret2);
- }else{
- int ret1 = call(r,c+2,taken) + Mat[r][c];
- int ret2 = call(r,c+1,taken);
- res = max(ret1,ret2);
- }
- }else{
- int ret1 = call(r+1,0,false);
- int ret2 = call(r,0,true);
- res = max(ret1,ret2);
- }
- return DP[r][c] = res;
- }
- int main() {
- while(scanf("%d %d",&row,&col) == 2){
- if(row == 0 && col == 0) break;
- for(int i = 0;i<row;i++){
- for(int j = 0;j<col;j++){
- scanf("%d",&Mat[i][j]);
- }
- }
- memset(DP,-1,sizeof(DP));
- int res = call(0,0,false);
- printf("%d\n",res);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment