您的位置:首页 > 产品设计 > UI/UE

ZOJ 1013 Great Equipment

2010-11-10 20:45 357 查看
动态规划,水题。

最烦这种题目长的。

数据:

carry[i][j]数组,存放商队可以放第一种武器i个,第二种j个,第三种carry[i][j]个。最终结果是存放所有装备运输的可能组合。

trade[i][j]数组,存放动态规划过程中,到目前为止所有商队,可以运输第一种i个,第二种 j个,第三种trade[i][j]个。

AC代码:
#include <stdio.h>
#include <memory.h>
int trade[501][501], carry[501][501];
int min(int a, int b)
{
return (a<b)?a:b;
}
int main()
{
int iCase = 0;
int n;
while(scanf("%d", &n) && n)
{
if(iCase++) printf("/n");
int w[4],s[4],d[4];
int data;
for(data = 1; data <= 3; data++)
{
scanf("%d%d%d",&w[data], &s[data], &d[data]);
}
int c1, c2, c3, d4;
scanf("%d%d%d%d", &c1, &c2, &c3, &d4);
d4 -= c1*d[1] + c2*d[2] + c3*d[3];      /*组合装备比普通装备多的防御力*/
memset(carry, 255, sizeof(carry));
carry[0][0] = 0;
int row = 0;
int col = 0;
int i, j, k;
int ja;
int ka;
for(i = 0; i < n; i++)
{
int weight, size;
scanf("%d%d", &weight, &size);
memset(trade, 255, sizeof(trade));
int newRow = row;                 /*当前的行数*/
int newCol = col;                 /*当前的列数*/
int weight1, size1, weight2, size2;
for(j = 0; j <= row; j++)
for(k = 0; k <= col; k++)
if(carry[j][k] >= 0)
for(ja = j,weight1 = size1 = 0;
(weight1 <= weight && size1 <= size);
weight1 += w[1], size1 += s[1], ja++)
for(ka = k,weight2 = weight1, size2 = size1;
(weight2 <= weight && size2 <= size);
weight2 += w[2], size2 += s[2], ka++)
{
if(newRow < ja)    newRow = ja;
if(newCol < ka)    newCol = ka;
int bootWeight = (weight - weight2) / w[3];
int bootSize = (size - size2) / s[3];
if(bootWeight > bootSize ) bootWeight = bootSize;
bootWeight += carry[j][k];  /*第三种武器的数量*/
if(trade[ja][ka] < bootWeight) trade[ja][ka] = bootWeight;
}
memcpy(carry,trade,sizeof(trade));
row = newRow;
col = newCol;
}
int iBest = 0;
for(j = 0; j <= row; j++)
for(k = 0; k<= col; k++)
if(carry[j][k] >= 0)
{
int defend = j*d[1] + k*d[2] + carry[j][k]*d[3];
int helms = j / c1;
int armors = k /c2;
int boots = carry[j][k] / c3;
if(d4 > 0)
defend += d4*min(helms,min(armors,boots));
if(iBest < defend)  iBest = defend;
}
printf("Case %d: %d/n",iCase, iBest);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: