Graphs 算法竞赛入门经典 刘汝佳.docx
- 文档编号:4415123
- 上传时间:2022-12-01
- 格式:DOCX
- 页数:21
- 大小:42.68KB
Graphs 算法竞赛入门经典 刘汝佳.docx
《Graphs 算法竞赛入门经典 刘汝佳.docx》由会员分享,可在线阅读,更多相关《Graphs 算法竞赛入门经典 刘汝佳.docx(21页珍藏版)》请在冰豆网上搜索。
Graphs算法竞赛入门经典刘汝佳
TheGeoSurvCompgeologicsurveycompanyisresponsiblefordetectingundergroundoildeposits.GeoSurvCompworkswithonelargerectangularregionoflandatatime,andcreatesagridthatdividesthelandintonumeroussquareplots.Itthenanalyzeseachplotseparately,usingsensingequipmenttodeterminewhetherornottheplotcontainsoil.
Aplotcontainingoiliscalledapocket.Iftwopocketsareadjacent,thentheyarepartofthesameoildeposit.Oildepositscanbequitelargeandmaycontainnumerouspockets.Yourjobistodeterminehowmanydifferentoildepositsarecontainedinagrid.
Input
Theinputfilecontainsoneormoregrids.Eachgridbeginswithalinecontainingm and n,thenumberofrowsandcolumnsinthegrid,separatedbyasinglespace.If m =0itsignalstheendoftheinput;otherwise
and
.Followingthisare m linesof n characterseach(notcountingtheend-of-linecharacters).Eachcharactercorrespondstooneplot,andiseither`*',representingtheabsenceofoil,or`@',representinganoilpocket.
Output
Foreachgrid,outputthenumberofdistinctoildeposits.Twodifferentpocketsarepartofthesameoildepositiftheyareadjacenthorizontally,vertically,ordiagonally.Anoildepositwillnotcontainmorethan100pockets.
SampleInput
11
*
35
*@*@*
**@**
*@*@*
18
@@****@*
55
****@
*@@*@
*@**@
@@@*@
@@**@
00
SampleOutput
0
1
2
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 S suchthat a = a1 and b = ak ,and ai and ai+1 areconnectedfor
.
Weconsiderallmaximallyconnectedsetsconsistingsolelyofnon-backgroundpixelstobedice.`Maximallyconnected'meansthatyoucannotaddanyothernon-backgroundpixelstothesetwithoutmakingitdis-connected.Likewiseweconsidereverymaximalconnectedsetofdotpixelstoformadot.
Input
Theinputconsistsofpicturesofseveraldicethrows.Eachpicturedescriptionstartswithalinecontainingtwonumberswandh,thewidthandheightofthepicture,respectively.Thesevaluessatisfy
.
Thefollowing h linescontain w characterseach.Thecharacterscanbe:
``.''forabackgroundpixel,``*''forapixelofadie,and``X''forapixelofadie'sdot.
Dicemayhavedifferentsizesandnotbeentirelysquareduetoopticaldistortion.Thepicturewillcontainatleastonedie,andthenumbersofdotsperdieisbetween1and6,inclusive.
Theinputisterminatedbyapicturestartingwith w = h =0,whichshouldnotbeprocessed.
Output
Foreachthrowofdice,firstoutputitsnumber.Thenoutputthenumberofdotsonthediceinthepicture,sortedinincreasingorder.
Printablanklineaftereachtestcase.
SampleInput
3015
..............................
..............................
...............*..............
...*****......****............
...*X***.....**X***...........
...*****....***X**............
...***X*.....****.............
...*****.......*..............
..............................
........***........******.....
.......**X****.....*X**X*.....
......*******......******.....
.....****X**.......*X**X*.....
........***........******.....
..............................
00
SampleOutput
Throw1
1224
784-MazeExploration
Amazeofrectangularroomsisrepresentedonatwodimensionalgridasillustratedinfigure1a.Eachpointofthegridisrepresentedbyacharacter.Thepointsofroomwallsaremarkedbythesamecharacterwhichcanbeanyprintablecharacterdifferentthan`*',`_'andspace.Infigure1thischaracteris`X'.Alltheotherpointsofthegridaremarkedbyspaces.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXX###X###X###XXX
XXXXX###########XXX
XXXXXXX###X###X###XXX
XXXXXXXXXXXXXXXXXXXXXXXXX#XXX#XXXXXXXXXX
XXXXXXXX###X###X###X###X
XX*XXX###############X
XXXXXXXX###X###X###X###X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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.
Input
Theprograminputisatextfilestructuredasfollows:
1.
Thefirstlinecontainsapositiveintegerwhichshowsthenumberofmazestobepainted.
2.
Therestofthefilecontainsthemazes.
Thelinesoftheinputfilecanbeofdifferentlength.Thetextwhichrepresentsamazeisterminatedbyaseparationlinefullofunderscores(`_').Thereareatmost30linesandatmost80charactersinalineforeachmaze
Theprogramreadsthemazesfromtheinputfile,paintsthemandwritesthepaintedmazesonthestandardoutput.
Output
Theoutputtextofapaintedmazehasthesameformatasthatwhichhasbeenreadforthatmaze,includingtheseparationlines.Theexamplebelowillustratesasimpleinputwhichcontainsasinglemazeandthecorrespondingoutput.
SampleInput
2
XXXXXXXXX
XXX
X*X
XXX
XXXXXXXXX
XX
XX
XX
XXXXX
_____
XXXXX
XX
X*X
XX
XXXXX
_____
SampleOutput
XXXXXXXXX
X###X###X
X#######X
X###X###X
XXXXXXXXX
XX
XX
XX
XXXXX
_____
XXXXX
X###X
X###X
X###X
XXXXX
_____
705-SlashMaze
Byfillingarectanglewithslashes(/)andbackslashes(
),youcangeneratenicelittlemazes.Hereisanexample:
Asyoucansee,pathsinthemazecannotbranch,sothewholemazeonlycontainscyclicpathsandpathsenteringsomewhereandleavingsomewhereelse.Weareonlyinterestedinthecycles.Inourexample,therearetwoofthem.
Yourtaskistowriteaprogramthatcountsthecyclesandfindsthelengthofthelongestone.Thelengthisdefinedasthenumberofsmallsquaresthecycleconsistsof(theonesborderedbygraylinesinthepicture).Inthisexample,thelongcyclehaslength16andtheshortonelength4.
Input
Theinputcontainsseveralmazedescriptions.Eachdescriptionbeginswithonelinecontainingtwointegers w and h (
),thewidthandtheheightofthemaze.Thenext h linesrepresentthemazeitself,andcontain w characterseach;allthesecharacterswillbeeither``/"or``\".
Theinputisterminatedbyatestcasebeginningwith w = h =0.Thiscaseshouldnotbeprocessed.
Output
Foreachmaze,firstoutputtheline``Maze#n:
'',where n isthenumberofthemaze.Then,outputtheline``kCycles;thelongesthaslength l.'',where k isthenumberofcyclesinthemazeand l thelengthofthelongestofthecycles.Ifthemazedoesnotcontainanycycles,outputtheline``Therearenocycles.".
Outputablanklineaftereachtestcase.
SampleInput
64
\//\\/
\///\/
//\\/\
\/\///
33
///
\//
\\\
00
SampleOutput
Maze#1:
2Cycles;thelongesthaslength16.
Maze#2:
Therearenocycles.
439-KnightMoves
Afriendofyouisdoingresearchonthe TravelingKnightProblem(TKP) whereyouaretofindtheshortestclosedtourofknightmovesthatvisitseachsquareofagivensetof n squaresonachessboardexactlyonce.Hethinksthatthemostdifficultpartoftheproblemisdeterminingthesmallestnumberofknightmovesbetweentwogivensquaresandthat,onceyouhaveaccomplishedthis,findingthetourwouldbeeasy.
Ofcourseyouknowthatitisviceversa.Soyouofferhimtowriteaprogramthatsolvesthe"difficult"part.
Yourjobistowriteaprogramthattakestwosquares a and b asinputandthendeterminesthenumberofknightmovesonashortestroutefrom a to b.
InputSpecification
Theinputfilewillcontainoneormoretestcases.Eachtestcaseconsistsofonelinecontainingtwosquaresseparatedbyonespace.Asquareisastringconsistingofaletter(a-h)representingthecolumnandadigit(1-8)representingtherowonthechessboard.
OutputSpecification
Foreachtestcase,printonelinesaying"Togetfrom xx t
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Graphs 算法竞赛入门经典 刘汝佳 算法 竞赛 入门 经典
![提示](https://static.bdocx.com/images/bang_tan.gif)