Graphs 算法竞赛入门经典 刘汝佳Word文档下载推荐.docx
《Graphs 算法竞赛入门经典 刘汝佳Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Graphs 算法竞赛入门经典 刘汝佳Word文档下载推荐.docx(21页珍藏版)》请在冰豆网上搜索。
SampleInput
11
*
35
*@*@*
**@**
18
@@****@*
55
****@
*@@*@
*@**@
@@@*@
@@**@
00
SampleOutput
1
2
657-Thedieiscast
InterGamesisahigh-techstartupcompanythatspecializesindevelopingtechnologythatallowsuserstoplaygamesovertheInternet.Amarketanalysishasalertedthemtothefactthatgamesofchanceareprettypopularamongtheirpotentialcustomers.BeitMonopoly,ludoorbackgammon,mostofthesegamesinvolvethrowingdiceatsomestageofthegame.
Ofcourse,itwouldbeunreasonableifplayerswereallowedtothrowtheirdiceandthenentertheresultintothecomputer,sincecheatingwouldbewaytoeasy.So,instead,InterGameshasdecidedtosupplytheiruserswithacamerathattakesapictureofthethrowndice,analyzesthepictureandthentransmitstheoutcomeofthethrowautomatically.
Forthistheydesperatelyneedaprogramthat,givenanimagecontainingseveraldice,determinesthenumbersofdotsonthedice.
Wemakethefollowingassumptionsabouttheinputimages.Theimagescontainonlythreedif-ferentpixelvalues:
forthebackground,thediceandthedotsonthedice.Weconsidertwopixels
connected
iftheyshareanedge-meetingatacornerisnotenough.Inthefigure,pixelsAandBareconnected,butBandCarenot.
Aset
S
ofpixelsisconnectedifforeverypair(a,b)ofpixelsin
S,thereisasequence
in
suchthat
a
=
a1
b
ak
and
ai
ai+1
areconnectedfor
.
Weconsiderallmaximallyconnectedsetsconsistingsolelyofnon-backgroundpixelstobedice.`Maximallyconnected'
meansthatyoucannotaddanyothernon-backgroundpixelstothesetwithoutmakingitdis-connected.Likewiseweconsidereverymaximalconnectedsetofdotpixelstoformadot.
Theinputconsistsofpicturesofseveraldicethrows.Eachpicturedescriptionstartswithalinecontainingtwonumberswandh,thewidthandheightofthepicture,respectively.Thesevaluessatisfy
Thefollowing
h
linescontain
w
characterseach.Thecharacterscanbe:
``.'
'
forabackgroundpixel,``*'
forapixelofadie,and``X'
forapixelofadie'
sdot.
Dicemayhavedifferentsizesandnotbeentirelysquareduetoopticaldistortion.Thepicturewillcontainatleastonedie,andthenumbersofdotsperdieisbetween1and6,inclusive.
Theinputisterminatedbyapicturestartingwith
=0,whichshouldnotbeprocessed.
Foreachthrowofdice,firstoutputitsnumber.Thenoutputthenumberofdotsonthediceinthepicture,sortedinincreasingorder.
Printablanklineaftereachtestcase.
3015
..............................
...............*..............
...*****......****............
...*X***.....**X***...........
...*****....***X**............
...***X*.....****.............
...*****.......*..............
........***........******.....
.......**X****.....*X**X*.....
......*******......******.....
.....****X**.......*X**X*.....
Throw1
1224
784-MazeExploration
Amazeofrectangularroomsisrepresentedonatwodimensionalgridasillustratedinfigure1a.Eachpointofthegridisrepresentedbyacharacter.Thepointsofroomwallsaremarkedbythesamecharacterwhichcanbeanyprintablecharacterdifferentthan`*'
`_'
andspace.Infigure1thischaracteris`X'
.Alltheotherpointsofthegridaremarkedbyspaces.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXX###X###X###XXX
XXXXX###########XXX
XXXXXXXXXXXXXXXXXXXXXXXXX#XXX#XXXXXXXXXX
XXXXXXXX###X###X###X###X
XX*XXX###############X
a)Initialmaze
b)Paintedmaze
Figure1.Mazesofrectangularrooms
Allroomsofthemazeareequalsizedwithallwalls3pointswideand1pointthickasillustratedinfigure2.Inaddition,awallissharedonitsfulllengthbytheseparatedrooms.Theroomscancommunicatethroughdoors,whicharepositionedinthemiddleofwalls.Therearenooutdoordoors.
door
|
XXXX
X.Xmeasuredfromwithintheroom
door-...--wallsare3pointswide
X.X__
XXXXX|
|___wallsareonepointthick
Figure2.Aroomwith3doors
Yourproblemistopaintallroomsofamazewhichcanbevisitedstartingfromagivenroom,calledthe`startroom'
whichismarkedbyastar(`*'
)positionedinthemiddleoftheroom.Aroomcanbevisitedfromanotherroomifthereisadooronthewallwhichseparatestherooms.Byconvention,aroomispaintedifitsentiresurface,includingthedoors,ismarkedbythecharacter`#'
asshowninfigure1b.
Theprograminputisatextfilestructuredasfollows:
1.
Thefirstlinecontainsapositiveintegerwhichshowsthenumberofmazestobepainted.
2.
Therestofthefilecontainsthemazes.
Thelinesoftheinputfilecanbeofdifferentlength.Thetextwhichrepresentsamazeisterminatedbyaseparationlinefullofunderscores(`_'
).Thereareatmost30linesandatmost80charactersinalineforeachmaze
Theprogramreadsthemazesfromtheinputfile,paintsthemandwritesthepaintedmazesonthestandardoutput.
Theoutputtextofapaintedmazehasthesameformatasthatwhichhasbeenreadforthatmaze,includingtheseparationlines.Theexamplebelowillustratesasimpleinputwhichcontainsasinglemazeandthecorrespondingoutput.
XXXXXXXXX
XXX
X*X
XX
XXXXX
_____
X###X###X
X#######X
X###X
705-SlashMaze
Byfillingarectanglewithslashes(/)andbackslashes(
),youcangeneratenicelittlemazes.Hereisanexample:
Asyoucansee,pathsinthemazecannotbranch,sothewholemazeonlycontainscyclicpathsandpathsenteringsomewhereandleavingsomewhereelse.Weareonlyinterestedinthecycles.Inourexample,therearetwoofthem.
Yourtaskistowriteaprogramthatcountsthecyclesandfindsthelengthofthelongestone.Thelengthisdefinedasthenumberofsmallsquaresthecycleconsistsof(theonesborderedbygraylinesinthepicture).Inthisexample,thelongcyclehaslength16andtheshortonelength4.
Theinputcontainsseveralmazedescriptions.Eachdescriptionbeginswithonelinecontainingtwointegers
(
),thewidthandtheheightofthemaze.Thenext
linesrepresentthemazeitself,andcontain
characterseach;
allthesecharacterswillbeeither``/"
or``\"
Theinputisterminatedbyatestcasebeginningwith
=0.Thiscaseshouldnotbeprocessed.
Foreachmaze,firstoutputtheline``Maze#n:
where
isthenumberofthemaze.Then,outputtheline``kCycles;
thelongesthaslength
l.'
k
isthenumberofcyclesinthemazeand
l
thelengthofthelongestofthecycles.Ifthemazedoesnotcontainanycycles,outputtheline``Therearenocycles."
Outputablanklineaftereachtestcase.
64
\//\\/
\///\/
//\\/\
\/\///
33
///
\//
\\\
Maze#1:
2Cycles;
thelongesthaslength16.
Maze#2:
Therearenocycles.
439-KnightMoves
Afriendofyouisdoingresearchonthe
TravelingKnightProblem(TKP)
whereyouaretofindtheshortestclosedtourofknightmovesthatvisitseachsquareofagivensetof
squaresonachessboardexactlyonce.Hethinksthatthemostdifficultpartoftheproblemisdeterminingthesmallestnumberofknightmovesbetweentwogivensquaresandthat,onceyouhaveaccomplishedthis,findingthetourwouldbeeasy.
Ofcourseyouknowthatitisviceversa.Soyouofferhimtowriteaprogramthatsolvesthe"
difficult"
part.
Yourjobistowriteaprogramthattakestwosquares
asinputandthendeterminesthenumberofknightmovesonashortestroutefrom
to
b.
InputSpecification
Theinputfilewillcontainoneormoretestcases.Eachtestcaseconsistsofonelinecontainingtwosquaresseparatedbyonespace.Asquareisastringconsistingofaletter(a-h)representingthecolumnandadigit(1-8)representingtherowonthechessboard.
OutputSpecification
Foreachtestcase,printonelinesaying"
Togetfrom
xx
t