ZOJ上BFS问题.docx
- 文档编号:29856691
- 上传时间:2023-07-27
- 格式:DOCX
- 页数:26
- 大小:115.67KB
ZOJ上BFS问题.docx
《ZOJ上BFS问题.docx》由会员分享,可在线阅读,更多相关《ZOJ上BFS问题.docx(26页珍藏版)》请在冰豆网上搜索。
ZOJ上BFS问题
<++++++++++++++++++++++BreadthFirstSearchProblemsonZOJ+++++++++++++++++++++++++>
ZOJProblemSet-1091nightMoves
TimeLimit:
1Second MemoryLimit:
32768KB
AfriendofyouisdoingresearchontheTravelingKnightProblem(TKP)whereyouaretofindtheshortestclosedtourofknightmovesthatvisitseachsquareofagivensetofnsquaresonachessboardexactlyonce.Hethinksthatthemostdifficultpartoftheproblemisdeterminingthesmallestnumberofknightmovesbetweentwogivensquaresandthat,onceyouhaveaccomplishedthis,findingthetourwouldbeeasy.
Ofcourseyouknowthatitisviceversa.Soyouofferhimtowriteaprogramthatsolvesthe"difficult"part.
Yourjobistowriteaprogramthattakestwosquaresaandbasinputandthendeterminesthenumberofknightmovesonashortestroutefromatob.
InputSpecification
Theinputfilewillcontainoneormoretestcases.Eachtestcaseconsistsofonelinecontainingtwosquaresseparatedbyonespace.Asquareisastringconsistingofaletter(a-h)representingthecolumnandadigit(1-8)representingtherowonthechessboard.
OutputSpecification
Foreachtestcase,printonelinesaying"Togetfromxxtoyytakesnknightmoves.".
SampleInput
e2e4
a1b2
b2c3
a1h8
a1h7
h8a1
b1c3
f6f6
SampleOutput
Togetfrome2toe4takes2knightmoves.
Togetfroma1tob2takes4knightmoves.
Togetfromb2toc3takes2knightmoves.
Togetfroma1toh8takes6knightmoves.
Togetfroma1toh7takes5knightmoves.
Togetfromh8toa1takes6knightmoves.
Togetfromb1toc3takes1knightmoves.
Togetfromf6tof6takes0knightmoves.
Source:
UniversityofUlmLocalContest1996
ZOJProblemSet–1103HikeonaGraph
TimeLimit:
1Second MemoryLimit:
32768KB
"HikeonaGraph"isagamethatisplayedonaboardonwhichanundirectedgraphisdrawn.Thegraphiscompleteandhasallloops,i.e.foranytwolocationsthereisexactlyonearrowbetweenthem.Thearrowsarecoloured.Therearethreeplayers,andeachofthemhasapiece.Atthebeginningofthegame,thethreepiecesareinfixedlocationsonthegraph.Inturn,theplayersmaydoamove.Amoveconsistsofmovingone'sownpiecealonganarrowtoanewlocationontheboard.Thefollowingconstraintisimposedonthis:
thepiecemayonlybemovedalongarrowsofthesamecolourasthearrowbetweenthetwoopponents'pieces.
Inthesixties("makelovenotwar")aone-personvariantofthegameemerged.Inthisvariantonepersonmovesallthethreepieces,notnecessarilyoneaftertheother,butofcourseonlyoneatatime.Goalofthisgameistogetallpiecesontothesamelocation,usingasfewmovesaspossible.Findoutthesmallestnumberofmovesthatisnecessarytogetallthreepiecesontothesamelocation,foragivenboardlayoutandstartingpositions.
InputSpecification
Theinputfilecontainsseveraltestcases.Eachtestcasestartswiththenumbern.Inputisterminatedbyn=0.Otherwise,1<=n<=50.Thenfollowthreeintegersp1,p2,p3with1<=pi<=ndenotingthestartinglocationsofthegamepieces.Thecoloursofthearrowsaregivennextasam×mmatrixofwhitespace-separatedlower-caseletters.Theelementmijdenotesthecolourofthearrowbetweenthelocationsiandj.Sincethegraphisundirected,youcanassumethematrixtobesymmetrical.
OutputSpecification
Foreachtestcaseoutputonasinglelinetheminimumnumberofmovesrequiredtogetallthreepiecesontothesamelocation,ortheword"impossible"ifthatisnotpossibleforthegivenboardandstartinglocations.
SampleInput
3123
rbr
bbb
rbr
2122
yg
gy
0
SampleOutput
2
impossible
Source:
UniversityofUlmLocalContest2000
ZOJProblemSet–1136Multiple
TimeLimit:
10Seconds MemoryLimit:
32768KB
aprogramthat,givenanaturalnumberNbetween0and4999(inclusively),andMdistinctdecimaldigitsX1,X2..XM(atleastone),findsthesmalleststrictlypositivemultipleofNthathasnootherdigitsbesidesX1,X2..XM(ifsuchamultipleexists).
Theinputfilehasseveraldatasetsseparatedbyanemptyline,eachdatasethavingthefollowingformat:
Onthefirstline-thenumberN
Onthesecondline-thenumberM
OnthefollowingMlines-thedigitsX1,X2..XM.
Foreachdataset,theprogramshouldwritetostandardoutputonasinglelinethemultiple,ifsuchamultipleexists,and0otherwise.
Anexampleofinputandoutput:
Input
22
3
7
0
1
2
1
1
Output
110
0
Source:
SoutheasternEurope2000
ZOJProblemSet–1301TheNewVilla
TimeLimit:
1Second MemoryLimit:
32768KB SpecialJudge
Mr.Blackrecentlyboughtavillainthecountryside.Onlyonethingbothershim:
althoughtherearelightswitchesinmostrooms,thelightstheycontrolareofteninotherroomsthantheswitchesthemselves.Whilehisestateagentsawthisasafeature,Mr.Blackhascometobelievethattheelectricianswereabitabsent-minded(toputitmildly)whentheyconnectedtheswitchestotheoutlets.
Onenight,Mr.Blackcamehomelate.Whilestandinginthehallway,henotedthatthelightsinallotherroomswereswitchedoff.Unfortunately,Mr.Blackwasafraidofthedark,soheneverdaredtoenteraroomthathaditslightsoutandwouldneverswitchoffthelightsoftheroomhewasin.
Aftersomethought,Mr.Blackwasabletousetheincorrectlywiredlightswitchestohisadvantage.Hemanagedtogettohisbedroomandtoswitchoffalllightsexceptfortheoneinthebedroom.
Youaretowriteaprogramthat,givenadescriptionofavilla,determineshowtogetfromthehallwaytothebedroomifonlythehallwaylightisinitiallyswitchedon.Youmayneverenteradarkroom,andafterthelastmove,alllightsexceptfortheoneinthebedroommustbeswitchedoff.Ifthereareseveralpathstothebedroom,youhavetofindtheonewhichusesthesmallestnumberofsteps,where``movefromoneroomtoanother'',``switchonalight''and``switchoffalight''eachcountasonestep.
Input
Theinputcontainsseveralvilladescriptions.Eachvillastartswithalinecontainingthreeintegersr,d,ands.risthenumberofroomsinthevilla,whichwillbeatmost10.disthenumberofdoors/connectionsbetweentheroomsandsisthenumberoflightswitchesinthevilla.Theroomsarenumberedfrom1tor;roomnumber1isthehallway,roomnumberristhebedroom.
Thislineisfollowedbydlinescontainingtwointegersiandjeach,specifyingthatroomiisconnectedtoroomjbyadoor.Thenfollowslinescontainingtwointegerskandleach,indicatingthatthereisalightswitchinroomkthatcontrolsthelightinrooml.
Ablanklineseparatesthevilladescriptionfromthenextone.Theinputendswithavillahavingr=d=s=0,whichshouldnotbeprocessed.
Output
Foreachvilla,firstoutputthenumberofthetestcase(`Villa#1',`Villa#2',etc.)inalineofitsown.
IfthereisasolutiontoMr.Black'sproblem,outputtheshortestpossiblesequenceofstepsthatleadshimtohisbedroomandonlyleavesthebedroomlightswitchedon.(Outputonlyoneshortestsequenceifyoufindmorethanone.)Adheretotheoutputformatshowninthesamplebelow.
Ifthereisnosolution,outputalinecontainingthestatement`Theproblemcannotbesolved.'
Outputablanklineaftereachtestcase.
SampleInput
334
12
13
32
12
13
21
32
212
21
11
12
000
SampleOutput
Villa#1
Theproblemcanbesolvedin6steps:
-Switchonlightinroom2.
-Switchonlightinroom3.
-Movetoroom2.
-Switchofflightinroom1.
-Movetoroom3.
-Switchofflightinroom2.
Villa#2
Theproblemcannotbesolved.
Source:
SouthwestEurope1996
ZOJProblemSet–1310Robot
TimeLimit:
1Second MemoryLimit:
32768KB
TheRobotMovingInstituteisusingarobotintheirlocalstoretotransportdifferentitems.Ofcoursetherobotshouldspendonlytheminimumtimenecessarywhentravellingfromoneplaceinthestoretoanother.Therobotcanmoveonlyalongastraightline(track).Alltracksformarectangulargrid.Neighbouringtracksareonemeterapart.ThestoreisarectangleNxMmetersanditisentirelycoveredbythisgrid.Thedistanceofthetrackclosesttothesideofthestoreisexactlyonemeter.Therobothasacircularshapewithdiameterequalto1.6meter.Thetrackgoesthroughthecenteroftherobot.Therobotalwaysfacesnorth,south,westoreast.Thetracksareinthesouth-northandinthewest-eastdirections.Therobotcanmoveonlyinthedirectionitfaces.Thedirectioninwhichitfacescanbechangedateachtrackcrossing.Initiallytherobotstandsatatrackcrossing.Theobstaclesinthestoreareformedfrompiecesoccupying1mx1montheground.Eachobstacleiswithina1x1squareformedbythetracks.Themovementoftherobotiscontrolledbytwocommands.ThesecommandsareGOandTURN.
TheGOcommandhasoneintegerparameternin{1,2,3}.Afterreceivingthiscommandtherobotmovesnmetersinthedirectionitfaces.
TheTURNcommandhasoneparameterwhichiseitherleftorright.Afterreceivingthiscommandtherobotchangesitsorientationby90ointhedirectionindicatedbytheparameter.
Theexecutionofeachcommandlastsonesecond.
HelpresearchersofRMItowriteaprogramwhichwilldeterminetheminimaltimeinwhichtherobotcanmovefromagivenstartingpointtoagivendestination.
Input
Theinputconsistsofblocksoflines.ThefirstlineofeachblockcontainstwointegersM<=50andN<=50separatedbyonespace.IneachofthenextMlinesthereareNnumbersoneorzeroseparatedbyonespace.Onerepresentsobstaclesandzerorepresentse
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ZOJ BFS 问题