您的位置:首页 > 其它

ZOJ 1093 Monkey and Banana(动态规划)

2013-04-07 07:04 351 查看
MonkeyandBananaTimeLimit:2SecondsMemoryLimit:65536KB
AgroupofresearchersaredesigninganexperimenttotesttheIQofamonkey.Theywillhangabananaattheroofofabuilding,andatthemeantime,providethemonkeywithsomeblocks.Ifthemonkeyiscleverenough,itshallbeabletoreachthebananabyplacingoneblockonthetopanothertobuildatowerandclimbuptogetitsfavoritefood.

Theresearchershaventypesofblocks,andanunlimitedsupplyofblocksofeachtype.Eachtype-iblockwasarectangularsolidwithlineardimensions(xi,yi,zi).Ablockcouldbereorientedsothatanytwoofitsthreedimensionsdeterminedthedimensionsofthebaseandtheotherdimensionwastheheight.

Theywanttomakesurethatthetallesttowerpossiblebystackingblockscanreachtheroof.Theproblemisthat,inbuildingatower,oneblockcouldonlybeplacedontopofanotherblockaslongasthetwobasedimensionsoftheupperblockwerebothstrictlysmallerthanthecorrespondingbasedimensionsofthelowerblockbecausetherehastobesomespaceforthemonkeytostepon.Thismeant,forexample,thatblocksorientedtohaveequal-sizedbasescouldn'tbestacked.

Yourjobistowriteaprogramthatdeterminestheheightofthetallesttowerthemonkeycanbuildwithagivensetofblocks.

InputSpecification

Theinputfilewillcontainoneormoretestcases.Thefirstlineofeachtestcasecontainsanintegern,
representingthenumberofdifferentblocksinthefollowingdataset.Themaximumvaluefornis30.
Eachofthenextnlinescontainsthreeintegersrepresentingthevaluesxi,yiandzi.
Inputisterminatedbyavalueofzero(0)forn.

OutputSpecification

Foreachtestcase,printonelinecontainingthecasenumber(theyarenumberedsequentiallystartingfrom1)andtheheightofthetallestpossibletowerintheformat"Casecase:maximumheight=height"

SampleInput

1
102030
2
6810
555
7
111
222
333
444
555
666
777
5
314159
265358
979323
846264
338327
0

SampleOutput

Case1:maximumheight=40
Case2:maximumheight=21
Case3:maximumheight=28
Case4:maximumheight=342

动态规划的实质是记忆化搜索,也可以写成递推的形式

ViewCode

#include<stdio.h>
#include<stdlib.h>

intmax(inta,intb)
{
returna>b?a:b;
}

structblock
{
intx,y,z;
}b[1200];
inth[1200];
intcmp(constvoid*a,constvoid*b)
{
return((block*)b)->x-((block*)a)->x;
}

intmain()
{
intn;
inti,j;
intcases=1;
intx,y,z,height;
while(scanf("%d",&n)&&n)
{
for(i=0;i<n;i++)
{
scanf("%d%d%d",&x,&y,&z);
b[6*i].x=x;
b[6*i].y=y;
b[6*i].z=z;
b[6*i+1].x=x;
b[6*i+1].y=z;
b[6*i+1].z=y;
b[6*i+2].x=y;
b[6*i+2].y=x;
b[6*i+2].z=z;
b[6*i+3].x=y;
b[6*i+3].y=z;
b[6*i+3].z=x;
b[6*i+4].x=z;
b[6*i+4].y=x;
b[6*i+4].z=y;
b[6*i+5].x=z;
b[6*i+5].y=y;
b[6*i+5].z=x;
}
qsort(b,6*n,sizeof(b[0]),cmp);
h[0]=b[0].z;
for(i=1;i<6*n;i++)
{
height=0;
for(j=0;j<i;j++)
if(b[j].x>b[i].x&&b[j].y>b[i].y&&h[j]>height)
height=h[j];
h[i]=height+b[i].z;
}

height=h[0];
for(i=1;i<6*n;i++)
height=max(height,h[i]);
printf("Case%d:maximumheight=%d\n",cases++,height);
}
return0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: