NYOJ-171 填表法 普通dp

题目链接:

http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=171

分析:

kk每次只能向右边和下边走,所以取他的上面和左边的最大值加上自己

dp[i][j]=f_max(dp[i-1][j],dp[i][j-1])+a[i][j];

代码如下:

#include<bits/stdc++.h>
#define pai 3.1415926535898
using namespace std;
int f_max(int a,int b)
{
    if(a>b)
    {
        return a;
    }else
    {
        return b;
    }
}
int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    int a[n+1][m+1];
    memset(a,0,sizeof(a));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    int dp[n+1][m+1];
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            dp[i][j]=f_max(dp[i-1][j],dp[i][j-1])+a[i][j];
        }
    }
    printf("%d\n",dp[n][m]);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/yinbiao/p/8995205.html