NYOJ 234 吃土豆

吃土豆

时间限制:1000 ms  |           内存限制:65535 KB
难度:4
 
描述
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 ?
 
输入
There 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<=500.
输出
For each case, you just output the MAX qualities you can eat and then get.
样例输入
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
样例输出
242
//开始没考虑全面只考虑了一个dp,后来发现原来横着的最大和也是一个DP,题中用value[]数组来存放了………………
 1 #include<iostream>
 2 #include<memory.h>
 3 using namespace std;
 4 #define max(a,b) a>b?a:b
 5 int dp[505], value[505];
 6 int main()
 7 {
 8 //    freopen("in.txt","r",stdin);
 9     int m,n,i,j,x,y;
10     while(cin>>m>>n)
11     {
12         memset(dp,0,sizeof(dp));
13         memset(value,0,sizeof(value));
14         for(i = 0; i < m; ++i)
15         {
16             x = y = 0;
17             for(j = 0; j  < n; ++j)
18             {
19                 cin>>value[j];
20                 if(j == 1) value[1] = max( value[0], value[1] );
21                 if(j > 1)
22                     value[j] = max( value[j-1], value[j] + value[j-2]);
23             }
24             dp[i] = value[n-1];
25         }
26         dp[1] = max(dp[0],dp[1]);
27         for(i = 2; i < m; ++i)
28             dp[i] = max( dp[i-1] ,dp[i] + dp[i-2]);
29         cout<<dp[m-1]<<endl;
30     }
31     return 0;
32 }
 
原文地址:https://www.cnblogs.com/yaling/p/3035324.html