码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2845(dp)

时间:2015-01-27 21:35:09      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2845

 

Beans

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3097    Accepted Submission(s): 1495


Problem Description
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 ?
 

 

Input
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<=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
 
 
题意:按照规则取出的数字和最大。规则:取了坐标为(x,y)的数字就不能再取坐标为(x,y-1)、(x,y+1)、(x-1,*)和(x+1,*)的数字,*表示任意位置。
分析:首先找出每一行每个位置和最佳情况;每个数字可取可不取,即:dpx[i] = max(dpx[i-1], dpx[i-2] + 该行第 i 个数字)。然后再找列的和最佳情况,此时只有一列,即每一行的最大值组成的列,则:dyp[i] = max(dpy[i-1], dpy[i-2] + 第 i 行的最大值)。
 
技术分享
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 int n,m,s;
 7 int dpx[222222],dpy[222222];
 8 
 9 int main ()
10 {
11     int i,j,k;
12     while (scanf ("%d%d",&n,&m)==2)
13     {
14         memset(dpx, 0, sizeof(dpx));
15         memset(dpy, 0, sizeof(dpy));//先对两个数组清零
16         for (i=2; i<=n+1; i++)
17         {
18             for (j=2; j<=m+1; j++)
19             {
20                 scanf ("%d",&s);
21                 dpx[j] = max(dpx[j-1], dpx[j-2] + s);//每个状态的值等于之前的某个状态加上另一个状态
22             }
23             dpy[i] = max(dpy[i-1], dpy[i-2] + dpx[m+1]);//因为只有加法,dpx[m+1]为每一行的最大值
24         }
25         printf ("%d\n",dpy[n+1]);
26     }
27     return 0;
28 }
View Code

 

HDU 2845(dp)

标签:

原文地址:http://www.cnblogs.com/dxd-success/p/4253825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!