The BEANS,DP of hdu2845
2012-12-08 16:12 533 People read review (0) Favorite Report
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Problem Description
Bean-eating is a interesting game, everyone owns an m*n matrix, which are filled with different qualities beans. Meantime, there is the only one bean in any 1*1 grid. Now your want to eat the beans and collect the qualities, but everyone must obey by the following rules:if you eat the BEA n 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 do you eat and then get?
Input
There is a few cases. In each case, there is the 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 beans isn ' t beyond, and 1<=m*n<=200000.
Output
For 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
The maximum value that can be obtained for a row is calculated, and then the maximum value that can be obtained for all rows is calculated.
#include <iostream> #include <cstdio> #include <cstdlib> #include < cstring> #include <string> #include <queue> #include <algorithm> #include <map> #include &
Lt;iomanip> #define INF 99999999 using namespace std;
const int max=200003;
The int dpx[max],dpy[max];//dpx is used to save the current row before the maximum value of the I column, dpy used to save the first row can be the maximum value.
int main () {int n,m,s;
while (Cin>>n>>m) {to (int i=2;i<=n+1;++i) {for (int j=2;j<=m+1;++j) {
scanf ("%d", &s);
Dpx[j]=max (dpx[j-1],dpx[j-2]+s);//The maximum value that can be obtained for the current row of J columns.
} dpy[i]=max (dpy[i-1],dpy[i-2]+dpx[m+1]);//The maximum value that can be obtained from the previous I line.
} cout<<dpy[n+1]<<endl;
} return 0; }