[HDU] 2391 Filthy Rich 入门dp

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2391

方法:建立状态转移方程,设F(x,y)为走到x,y点已经获得了的最大黄金数量,curent(x,y)为该点本身具有的黄金数量,则

     {

     0 , x<1|| y<1;

F(x,y)= Max(F(x-1,y)+current(x,y), F(x,y-1)+current(x,y),F(x-1,y-1)+current(x,y));//因为每一个位置最多可以从3个方向到达。

     current(1,1) , x==1 && y==1;

     }

感想:适合dp入门练习。

代码:

View Code
#include<iostream>
using namespace std;

int MyMax(int x,int y, int z)
{
    if(x>=y)
    {
        if(y>=z)
            return x;
        if(z>=x)
            return z;
        return x;
    }
    if(y<=z)
        return z;
    return y;
}
int dp[1001][1001];
int main()
{
    int tc=0,t;
    scanf("%d",&t);
    int a,b;
    while(tc<t)
    {
        memset(dp,0,sizeof(dp));
        scanf("%d %d",&a,&b);
        for(int i =1;i<=a;i++)
            for(int j =1;j<=b;j++)
                scanf("%d",&dp[i][j]);
        for(int i =1;i<=a;i++)
            for(int j =1;j<=b;j++)
                dp[i][j] = MyMax(dp[i][j]+dp[i-1][j],dp[i][j]+dp[i][j-1],dp[i][j]+dp[i-1][j-1]);
        cout<<"Scenario #"<<tc+1<<":"<<endl<<dp[a][b]<<endl<<endl;
        tc++;
    }
    return 0;
} 
原文地址:https://www.cnblogs.com/kbyd/p/3035359.html