Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled with different qualities beans. Meantime, there is only one bean in any 1*1 grid. Now you want to eat the beans and collect the qualities, but everyone must obey by the following rules: if you eat the bean at the coordinate(x, y), you can’t eat the beans anyway at the coordinates listed (if exiting): (x, y-1), (x, y+1), and the both rows whose abscissas are x-1 and x+1.
Now, how much qualities can you eat and then get ?
Now, how much qualities can you eat and then get ?
InputThere are a few cases. In each case, there are two integer M (row number) and N (column number). The next M lines each contain N integers, representing the qualities of the beans. We can make sure that the quality of bean isn't beyond 1000, and 1<=M*N<=200000.OutputFor each case, you just output the MAX qualities you can eat and then get.Sample Input
4 6
11 0 7 5 13 9
78 4 81 6 22 4
1 40 9 34 16 10
11 22 0 33 39 6
Sample Output
242
ideas: to do the most and discontinuous
#include <the iostream> #include <the iostream> #include <cstdio> #include < String > #include <CString> #include <algorithm> #include <stdio.h> #include < String .h> #include <Vector> # the include < SET > the using namespace STD; int dpm [ 2009 ] [ 2009 ], dpn [ 2009 ]; // do not seek a consecutive sequence and each row dpm, dpn seeking discrete sequence of each column and int LEN1, LEN2; int main() { int n , m ; while(cin >> n >> m) { memset(dpn , 0 , sizeof(dpn)); memset(dpm , 0 , sizeof(dpm)); for(int i = 3 ; i < n + 3 ; i++) { for(int j = 3 ; j < m + 3 ; j++) { scanf("%d" , &dpm[i][j]); dpm[i][j] Max = + ([I] DPM [J- 2 ], DPM [I] [J- . 3 ]); // find dp [m + 1] and dp [m + 2] represent two ways to select the maximum discontinuous row and } } for ( int I = . 3 ; I <n-+ . 3 ; I ++ ) { DPN [I] = max (DPN [I- 2 ], DPN [I- . 3 ]) + max (DPM [I] [m + . 1 ], [I DPM] [m + 2 ]); // find the maximum discontinuous sequences and columns } int ANS = 0 ; for ( int I = . 3 ; I <n-+ . 3 ; i++) { if(dpn[i] > ans) ans = dpn[i]; } printf("%d\n" , ans); } return 0; }