HDU 2391 Filthy Rich

原题目链接HDU2391


分类

HDU 动态规划


题意

(dp初步)类似与数塔
输入一张整数表,求从左上角到右下角按每次向右,向下,或者是右下这种方式行走,到达右下角时能够收集的最大财富;

样例输入输出

Sample Input

1
3 4
1 10 8 8
0 0 1 8
0 27 0 4

Sample Output

Scenario #1:
42

想法

数塔,经典dp,化为子问题,一步步求解


代码

795ms

#include<bits/stdc++.h>
#define maxn 1111
using namespace std;
int Map[maxn][maxn],dp[maxn][maxn]; 
int main(){
	int T,kase=0;
	scanf("%d",&T);
	while(T--){
		int n,m;
		scanf("%d%d",&n,&m);
		memset(Map,0,sizeof(Map));
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				scanf("%d",&Map[i][j]);
			}
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				dp[i][j] = max(dp[i-1][j],max(dp[i-1][j-1],dp[i][j-1]))+Map[i][j];
			}
		}
		printf("Scenario #%d:\n",++kase);
		printf("%d\n\n",dp[n][m]);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40456064/article/details/84171309
今日推荐