深度优先搜索Word文档下载推荐.docx
《深度优先搜索Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《深度优先搜索Word文档下载推荐.docx(10页珍藏版)》请在冰豆网上搜索。
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
6545
2851
ThecornfieldmazeisapopularHalloweentreat.Visitorsareshowntheentranceandmustwanderthroughthemazefacingzombies,chainsaw-wieldingpsychopaths,hippies,andotherterrorsontheirquesttofindtheexit.
Onepopularmaze-walkingstrategyguaranteesthatthevisitorwilleventuallyfindtheexit.Simplychooseeithertherightorleftwall,andfollowit.Ofcourse,there'
snoguaranteewhichstrategy(leftorright)willbebetter,andthepathtakenisseldomthemostefficient.(Italsodoesn'
tworkonmazeswithexitsthatarenotontheedge;
thosetypesofmazesarenotrepresentedinthisproblem.)
Astheproprieterofacornfieldthatisabouttobeconvertedintoamaze,you'
dliketohaveacomputerprogramthatcandeterminetheleftandright-handpathsalongwiththeshortestpathsothatyoucanfigureoutwhichlayouthasthebestchanceofconfoundingvisitors.
Inputtothisproblemwillbeginwithalinecontainingasingleintegernindicatingthenumberofmazes.Eachmazewillconsistofonelinewithawidth,w,andheight,h(3<
=w,h<
=40),followedbyhlinesofwcharacterseachthatrepresentthemazelayout.Wallsarerepresentedbyhashmarks('
#'
),emptyspacebyperiods('
.'
),thestartbyan'
S'
andtheexitbyan'
E'
.
Exactlyone'
andone'
willbepresentinthemaze,andtheywillalwaysbelocatedalongoneofthemazeedgesandneverinacorner.Themazewillbefullyenclosedbywalls('
),withtheonlyopeningsbeingthe'
and'
.The'
willalsobeseparatedbyatleastonewall('
).
Youmayassumethatthemazeexitisalwaysreachablefromthestartpoint.
Foreachmazeintheinput,outputonasinglelinethenumberof(notnecessarilyunique)squaresthatapersonwouldvisit(includingthe'
)for(inorder)theleft,right,andshortestpaths,separatedbyasinglespaceeach.Movementfromonesquaretoanotherisonlyallowedinthehorizontalorverticaldirection;
movementalongthediagonalsisnotallowed.
2
88
########
#......#
#.####.#
#...#..#
#S#E####
95
#########
#.#.#.#.#
S.......E
3755
17179
SouthCentralUSA2006
3009Curling2.0
6807
2841
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
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
Foreachdataset,printalinehavingadecimalintegerindicatingtheminimumnumberofmovesalongaroutefromthestarttothegoal.Iftherearenosuchroutes,print-1instead.Eachlineshouldnothaveanycharacterotherthanthisnumber.
21
32
66
100210
110000
000003
000000
100001
61
112113
102113
121
201111111113
131
2011111111113
00
1
4
-1
10
Japan2006Domestic
1321棋盘问题
10000K
15034
7428
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。
要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
输入含有多组测试数据。
每组数据的第一行是两个正整数,nk,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。
n<
=8,k<
=n
当为-1-1时表示输入结束。
随后的n行描述了棋盘的形状:
每行有n个字符,其中#表示棋盘区域,.表示空白区域(数据保证不出现多余的空白行或者空白列)。
对于每一组数据,给出一行输出,输出摆放的方案数目C(数据保证C<
2^31)。
#.
.#
44
...#
..#.
.#..
#...
-1-1
蔡错@pku
2251
DungeonMaster
11510
4465
Youaretrappedina3Ddungeonandneedtofindthequickestwayout!
Thedungeoniscomposedofunitcubeswhichmayormaynotbefilledwithrock.Ittakesoneminutetomoveoneunitnorth,south,east,west,upordown.Youcannotmovediagonallyandthemazeissurroundedbysolidrockonallsides.
Isanescapepossible?
Ifyes,howlongwillittake?
Theinputconsistsofanumberofdungeons.EachdungeondescriptionstartswithalinecontainingthreeintegersL,RandC(alllimitedto30insize).
Listhenumberoflevelsmakingupthedungeon.
RandCarethenumberofrowsandcolumnsmakinguptheplanofeachlevel.
ThentherewillfollowLblocksofRlineseachcontainingCcharacters.Eachcharacterdescribesonecellofthedungeon.Acellfullofrockisindicatedbya'
andemptycellsarerepresentedbya'
.Yourstartingpositionisindicatedby'
andtheexitbytheletter'
.There'
sasingleblanklineaftereachlevel.InputisterminatedbythreezeroesforL,RandC.
Eachmazegeneratesonelineofoutput.Ifitispossibletoreachtheexit,printalineoftheform
Escapedinxminute(s).
wherexisreplacedbytheshortesttimeittakestoescape.
Ifitisnotpossibletoescape,printtheline
Trapped!
345
S....
.###.
.##..
###.#
#####
##.##
##...