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.
representingthenumberofdifferentblocksinthefollowingdataset.Themaximumvaluefornis30.
Eachofthenextnlinescontainsthreeintegersrepresentingthevaluesxi,yiandzi.
Inputisterminatedbyavalueofzero(0)forn.
ViewCode
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; }
相关文章推荐
- zoj1093 Monkey and Banana 动态规划
- Monkey and Banana (zoj 1093 动态规划)
- zoj1093 Monkey and Banana(DP)
- ZOJ_1093_MonkeyAndBanana
- zoj 1093 Monkey and Banana
- zoj 1093 Monkey and Banana
- ZOJ_1093_Monkey and Banana
- ZOJ Problem Set - 1093 Monkey and Banana
- ZOJ 1093—Monkey and Banana 解题报告
- ZOJ 1093 Monkey and Banana (DP)
- ZOJ 1093 Monkey and Banana
- ZOJ 1093 Monkey and Banana(dp)
- ZOJ_1093(Monkey and Banana)
- zoj1093题解 Monkey and Banana
- zoj 1093 || hdu 1069 Monkey and Banana( Easy DP )
- ZOJ 1093-Monkey and Banana
- ZOJ 1093 Monkey and Banana
- ZOJ 1093 Monkey and Banana
- HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径)
- 动态规划 zoj1093 Monkey and Banana