HDU 1176 免费馅饼

/*
转化为数塔问题
dp[i][j] i 表示第i秒 j表示第j个位置 能够拿到的最大馅饼数
状态转移方程
dp[i][j] = max{dp[i+1][j-1],dp[i+1][j],dp[i+1][j+1]} + a[i][j];

时刻注意边界
*/
#include
<stdio.h>
#include
<string.h>
const int MAX = 100001;
int dp[MAX][11]; //1 ~ 100000秒
int max2(int d1,int d2){
return d1 > d2 ? d1 : d2;
}
int max3(int d1,int d2, int d3){
int max = d1 > d2 ? d1 : d2;
max
= max > d3 ? max : d3;
return max;
}
void DP(int maxt){ //自低向上递推
for(int i = maxt - 1; i >= 0; i--){
for(int j = 0; j <= 10;j++){
if(j == 0){
dp[i][j]
+= max2( dp[i+1][j],dp[i+1][j+1]);
}
else if(j == 10){
dp[i][j]
+= max2( dp[i+1][j], dp[i+1][j-1]);
}
else {
dp[i][j]
+= max3( dp[i+1][j], dp[i+1][j+1],dp[i+1][j-1]);
}
}
}
}
int main(){
int n, x, t, maxt;
while(scanf("%d",&n), n){
memset(dp,
0,sizeof(dp)); //初始化
maxt = 0;
while( n-- ){
scanf(
"%d%d",&x, &t); //数塔建立 很重要
dp[t][x]++; //第t秒能x位置能接到的馅饼数
maxt = maxt > t ? maxt : t;
}
DP(maxt);
printf(
"%d\n",dp[0][5]);
}
return 0;
}
posted @ 2011-04-09 02:59  L..  阅读(229)  评论(0编辑  收藏  举报