[bo][un]multiple1902[/un] 在 2008-10-10 19:32 的发言:[/bo]
能否讲讲思路和状态转移方程?
直接给代码吧:
程序代码:
#include<stdio.h>
#include<string.h>
#define MAX 100
#define max2(a,b) ((a)>(b)?(a):(b))
#define max3(a,b,c) max2(max2(a,b),c)
#define min2(a,b) ((a)>(b)?(b):(a))
int res[MAX+1];
int solve(int high[],int len){
int idx;
res[1] = high[0];
res[2] = max2(high[0],high[1]);
res[3] = max3(high[0],high[1],high[2]);
for(idx = 4;idx <= len;idx ++){
res[idx] = max3(high[idx-1],high[idx-2],high[idx-3])+res[idx-3];
res[idx] = min2(res[idx],high[idx-1]+res[idx-1]);
res[idx] = min2(res[idx],max2(high[idx-1],high[idx-2])+res[idx-2]);
}
return res[len];
}
int main(){
int high[MAX],col,row,size,idx,x,y;
scanf("%d%d%d",&row,&col,&size);
memset(high,0,sizeof(high));
for(idx = 0;idx < size;idx ++){
scanf("%d%d",&x,&y);
high[x] = max2(high[x],y);
}
printf("%d\n",solve(high,col));
}
其中solve函数不足15行