深度优先搜索.docx
《深度优先搜索.docx》由会员分享,可在线阅读,更多相关《深度优先搜索.docx(10页珍藏版)》请在冰豆网上搜索。
![深度优先搜索.docx](https://file1.bdocx.com/fileroot1/2022-11/21/bf5cd27c-5736-4750-b368-0e0d3038e4b0/bf5cd27c-5736-4750-b368-0e0d3038e4b01.gif)
深度优先搜索
2488AKnight'sJourney
TimeLimit:
1000MS
MemoryLimit:
65536K
TotalSubmissions:
20354
Accepted:
6857
Description
Background
Theknightisgettingboredofseeingthesameblackandwhitesquaresagainandagainandhasdecidedtomakeajourney
aroundtheworld.Wheneveraknightmoves,itistwosquaresinonedirectionandonesquareperpendiculartothis.Theworldofaknightisthechessboardheislivingon.Ourknightlivesonachessboardthathasasmallerareathanaregular8*8board,butitisstillrectangular.Canyouhelpthisadventurousknighttomaketravelplans?
Problem
Findapathsuchthattheknightvisitseverysquareonce.Theknightcanstartandendonanysquareoftheboard.
Input
Theinputbeginswithapositiveintegerninthefirstline.Thefollowinglinescontainntestcases.Eachtestcaseconsistsofasinglelinewithtwopositiveintegerspandq,suchthat1<=p*q<=26.Thisrepresentsap*qchessboard,wherepdescribeshowmanydifferentsquarenumbers1,...,pexist,qdescribeshowmanydifferentsquarelettersexist.ThesearethefirstqlettersoftheLatinalphabet:
A,...
Output
Theoutputforeveryscenariobeginswithalinecontaining"Scenario#i:
",whereiisthenumberofthescenariostartingat1.Thenprintasinglelinecontainingthelexicographicallyfirstpaththatvisitsallsquaresofthechessboardwithknightmovesfollowedbyanemptyline.Thepathshouldbegivenonasinglelinebyconcatenatingthenamesofthevisitedsquares.Eachsquarenameconsistsofacapitalletterfollowedbyanumber.
Ifnosuchpathexist,youshouldoutputimpossibleonasingleline.
SampleInput
3
11
23
43
SampleOutput
Scenario#1:
A1
Scenario#2:
impossible
Scenario#3:
A1B3C1A2B4C2A3B1C3A4B2C4
Source
TUDProgrammingContest2005,Darmstadt,Germany
3083
Language:
ChildrenoftheCandyCorn
TimeLimit:
1000MS
MemoryLimit:
65536K
TotalSubmissions:
6545
Accepted:
2851
Description
ThecornfieldmazeisapopularHalloweentreat.Visitorsareshowntheentranceandmustwanderthroughthemazefacingzombies,chainsaw-wieldingpsychopaths,hippies,andotherterrorsontheirquesttofindtheexit.
Onepopularmaze-walkingstrategyguaranteesthatthevisitorwilleventuallyfindtheexit.Simplychooseeithertherightorleftwall,andfollowit.Ofcourse,there'snoguaranteewhichstrategy(leftorright)willbebetter,andthepathtakenisseldomthemostefficient.(Italsodoesn'tworkonmazeswithexitsthatarenotontheedge;thosetypesofmazesarenotrepresentedinthisproblem.)
Astheproprieterofacornfieldthatisabouttobeconvertedintoamaze,you'dliketohaveacomputerprogramthatcandeterminetheleftandright-handpathsalongwiththeshortestpathsothatyoucanfigureoutwhichlayouthasthebestchanceofconfoundingvisitors.
Input
Inputtothisproblemwillbeginwithalinecontainingasingleintegernindicatingthenumberofmazes.Eachmazewillconsistofonelinewithawidth,w,andheight,h(3<=w,h<=40),followedbyhlinesofwcharacterseachthatrepresentthemazelayout.Wallsarerepresentedbyhashmarks('#'),emptyspacebyperiods('.'),thestartbyan'S'andtheexitbyan'E'.
Exactlyone'S'andone'E'willbepresentinthemaze,andtheywillalwaysbelocatedalongoneofthemazeedgesandneverinacorner.Themazewillbefullyenclosedbywalls('#'),withtheonlyopeningsbeingthe'S'and'E'.The'S'and'E'willalsobeseparatedbyatleastonewall('#').
Youmayassumethatthemazeexitisalwaysreachablefromthestartpoint.
Output
Foreachmazeintheinput,outputonasinglelinethenumberof(notnecessarilyunique)squaresthatapersonwouldvisit(includingthe'S'and'E')for(inorder)theleft,right,andshortestpaths,separatedbyasinglespaceeach.Movementfromonesquaretoanotherisonlyallowedinthehorizontalorverticaldirection;movementalongthediagonalsisnotallowed.
SampleInput
2
88
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
95
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########
SampleOutput
3755
17179
Source
SouthCentralUSA2006
3009Curling2.0
TimeLimit:
1000MS
MemoryLimit:
65536K
TotalSubmissions:
6807
Accepted:
2841
Description
OnPlanetMM-21,aftertheirOlympicgamesthisyear,curlingisgettingpopular.Buttherulesaresomewhatdifferentfromours.Thegameisplayedonanicegameboardonwhichasquaremeshismarked.Theyuseonlyasinglestone.Thepurposeofthegameistoleadthestonefromthestarttothegoalwiththeminimumnumberofmoves.
Fig.1showsanexampleofagameboard.Somesquaresmaybeoccupiedwithblocks.Therearetwospecialsquaresnamelythestartandthegoal,whicharenotoccupiedwithblocks.(Thesetwosquaresaredistinct.)Oncethestonebeginstomove,itwillproceeduntilithitsablock.Inordertobringthestonetothegoal,youmayhavetostopthestonebyhittingitagainstablock,andthrowagain.
Fig.1:
Exampleofboard(S:
start,G:
goal)
Themovementofthestoneobeysthefollowingrules:
∙Atthebeginning,thestonestandsstillatthestartsquare.
∙Themovementsofthestonearerestrictedtoxandydirections.Diagonalmovesareprohibited.
∙Whenthestonestandsstill,youcanmakeitmovingbythrowingit.Youmaythrowittoanydirectionunlessitisblockedimmediately(Fig.2(a)).
∙Oncethrown,thestonekeepsmovingtothesamedirectionuntiloneofthefollowingoccurs:
oThestonehitsablock(Fig.2(b),(c)).
▪Thestonestopsatthesquarenexttotheblockithit.
▪Theblockdisappears.
oThestonegetsoutoftheboard.
▪Thegameendsinfailure.
oThestonereachesthegoalsquare.
▪Thestonestopsthereandthegameendsinsuccess.
∙Youcannotthrowthestonemorethan10timesinagame.Ifthestonedoesnotreachthegoalin10moves,thegameendsinfailure.
Fig.2:
Stonemovements
Undertherules,wewouldliketoknowwhetherthestoneatthestartcanreachthegoaland,ifyes,theminimumnumberofmovesrequired.
WiththeinitialconfigurationshowninFig.1,4movesarerequiredtobringthestonefromthestarttothegoal.TherouteisshowninFig.3(a).Noticewhenthestonereachesthegoal,theboardconfigurationhaschangedasinFig.3(b).
Fig.3:
ThesolutionforFig.D-1andthefinalboardconfiguration
Input
Theinputisasequenceofdatasets.Theendoftheinputisindicatedbyalinecontainingtwozerosseparatedbyaspace.Thenumberofdatasetsneverexceeds100.
Eachdatasetisformattedasfollows.
thewidth(=w)andtheheight(=h)oftheboard
Firstrowoftheboard
...
h-throwoftheboard
Thewidthandtheheightoftheboardsatisfy:
2<=w<=20,1<=h<=20.
Eachlineconsistsofwdecimalnumbersdelimitedbyaspace.Thenumberdescribesthestatusofthecorrespondingsquare.
0
vacantsquare
1
block
2
startposition
3
goalposition
ThedatasetforFig.D-1isasfollows:
66
100210
110000
000003
000000
100001
011111
Output
Foreachdataset,printalinehavingadecimalintegerindicatingtheminimumnumberofmovesalongaroutefromthestarttothegoal.Iftherearenosuchroutes,print-1instead.Eachlineshouldnothaveanycharacterotherthanthisnumber.
SampleInput
21
32
66
100210
110000
000003
000000
100001
011111
61
112113
61
102113
121
201111111113
131
2011111111113
00
SampleOutput
1
4
-1
4
10
-1
Source
Japan2006Domestic
1321棋盘问题
TimeLimit:
1000MS
MemoryLimit:
10000K
TotalSubmissions:
15034
Accepted:
7428
Description
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。
要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
Input
输入含有多组测试数据。
每组数据的第一行是两个正整数,nk,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。
n<=8,k<=n
当为-1-1时表示输入结束。
随后的n行描述了棋盘的形状:
每行有n个字符,其中#表示棋盘区域,.表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
对于每一组数据,给出一行输出,输出摆放的方案数目C(数据保证C<2^31)。
SampleInput
21
#.
.#
44
...#
..#.
.#..
#...
-1-1
SampleOutput
2
1
Source
蔡错@pku
2251
DungeonMaster
TimeLimit:
1000MS
MemoryLimit:
65536K
TotalSubmissions:
11510
Accepted:
4465
Description
Youaretrappedina3Ddungeonandneedtofindthequickestwayout!
Thedungeoniscomposedofunitcubeswhichmayormaynotbefilledwithrock.Ittakesoneminutetomoveoneunitnorth,south,east,west,upordown.Youcannotmovediagonallyandthemazeissurroundedbysolidrockonallsides.
Isanescapepossible?
Ifyes,howlongwillittake?
Input
Theinputconsistsofanumberofdungeons.EachdungeondescriptionstartswithalinecontainingthreeintegersL,RandC(alllimitedto30insize).
Listhenumberoflevelsmakingupthedungeon.
RandCarethenumberofrowsandcolumnsmakinguptheplanofeachlevel.
ThentherewillfollowLblocksofRlineseachcontainingCcharacters.Eachcharacterdescribesonecellofthedungeon.Acellfullofrockisindicatedbya'#'andemptycellsarerepresentedbya'.'.Yourstartingpositionisindicatedby'S'andtheexitbytheletter'E'.There'sasingleblanklineaftereachlevel.InputisterminatedbythreezeroesforL,RandC.
Output
Eachmazegeneratesonelineofoutput.Ifitispossibletoreachtheexit,printalineoftheform
Escapedinxminute(s).
wherexisreplacedbytheshortesttimeittakestoescape.
Ifitisnotpossibletoescape,printtheline
Trapped!
SampleInput
345
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####