0
点赞
收藏
分享

微信扫一扫

nyoj 吃土豆 234 (双层DP)


吃土豆


1000 ms  |           内存限制: 65535


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.



nyoj  吃土豆  234  (双层DP)_#include



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



//题意: //给你n行m列的矩阵,矩阵上的数代表此处有多少豆子,问最多能吃多少豆子 //但吃豆子有一定的规则。 //规则::如果你吃了某处的豆子,那么你不能再吃它左边和右边的两处的豆子,并且不能吃它上下两行的豆子 //思路: //双层DP,先求出每行的最大值,再求全部的最大值。

#include<stdio.h>
#include<string.h>
int max(int x,int y)
{
	return x>y?x:y;
}
int dp[510];
int a[510][510];
int main()
{
	int n,m,i,j,w;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(dp,0,sizeof(dp));
		memset(a,0,sizeof(a));
		for(i=3;i<n+3;i++)
		{
			for(j=3;j<m+3;j++)
			{
				scanf("%d",&w);
				a[i][j]=max(a[i][j-2],a[i][j-3])+w;
			}
		}
		for(i=3;i<n+3;i++)
			dp[i]=max(dp[i-2],dp[i-3])+max(a[i][m+1],a[i][m+2]);
		printf("%d\n",max(dp[n+1],dp[n+2]));
	}
	return 0;
}


举报

相关推荐

0 条评论