Tarango

Candy 1

Jul 17th, 2015
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int DP[100][100];
  5. int Mat[100][100];
  6. int row,col;
  7.  
  8. int call(int r,int c,bool taken){
  9.     if(r >= row) return 0;
  10.     if(DP[r][c] != -1) return DP[r][c];
  11.  
  12.     int res = 0;
  13.     if(taken == true){
  14.         if(c >= col){
  15.             int ret1 = call(r+2,0,false);
  16.             int ret2 = call(r+2,0,true);
  17.             res = max(ret1,ret2);
  18.         }else{
  19.             int ret1 = call(r,c+2,taken) + Mat[r][c];
  20.             int ret2 = call(r,c+1,taken);
  21.             res = max(ret1,ret2);
  22.         }
  23.     }else{
  24.         int ret1 = call(r+1,0,false);
  25.         int ret2 = call(r,0,true);
  26.         res = max(ret1,ret2);
  27.     }
  28.     return DP[r][c] = res;
  29. }
  30.  
  31. int main() {
  32.     while(scanf("%d %d",&row,&col) == 2){
  33.         if(row == 0 && col == 0) break;
  34.         for(int i = 0;i<row;i++){
  35.             for(int j = 0;j<col;j++){
  36.                 scanf("%d",&Mat[i][j]);
  37.             }
  38.         }
  39.         memset(DP,-1,sizeof(DP));
  40.         int res = call(0,0,false);
  41.         printf("%d\n",res);
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment