HDU 1176 免费馅饼

题解:我们把每一秒可以走到的地方标注起来

           5

        4 5 6

      3 4 5 6 7

    2 3 4 5 6 7 8

  1 2 3 4 5 6 7 8 9 

0 1 2 3 4 5 6 7 8 9 10

很显然,这是一座数塔,f[i,j]只可以从f[i+1,j],f[i+1,j-1],f[i+1,j+1]中来,那么就是简单的DP了。

#include <cstdio>
#include <iostream>
using namespace std;
int f[100001][13];

int max(int a,int b)
{return(a>b?a:b);}

int main()
{
    int n;
    while(scanf("%d",&n),n!=0)
    {
        int s,t,tm=0;
        memset(f,0,sizeof(f));
        for(int i=0; i<n; i++){
            scanf("%d%d",&s,&t);
            f[t][s+1]++;
            if(tm<t)tm=t; 
        }
        for(int i=tm-1; i>=0; i--)
        for(int j=1; j<=11; j++)
        f[i][j]=f[i][j]+max(f[i+1][j-1],max(f[i+1][j],f[i+1][j+1]));
        printf("%d
",f[0][6]);
    }
    return 0;
}

注意:C++中二维数组是f[i][j],而不是f[i,j]    

 

原文地址:https://www.cnblogs.com/forever97/p/3529380.html