Graphs 算法竞赛入门经典 刘汝佳.docx

上传人:b****4 文档编号:4415123 上传时间:2022-12-01 格式:DOCX 页数:21 大小:42.68KB
下载 相关 举报
Graphs 算法竞赛入门经典 刘汝佳.docx_第1页
第1页 / 共21页
Graphs 算法竞赛入门经典 刘汝佳.docx_第2页
第2页 / 共21页
Graphs 算法竞赛入门经典 刘汝佳.docx_第3页
第3页 / 共21页
Graphs 算法竞赛入门经典 刘汝佳.docx_第4页
第4页 / 共21页
Graphs 算法竞赛入门经典 刘汝佳.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

Graphs 算法竞赛入门经典 刘汝佳.docx

《Graphs 算法竞赛入门经典 刘汝佳.docx》由会员分享,可在线阅读,更多相关《Graphs 算法竞赛入门经典 刘汝佳.docx(21页珍藏版)》请在冰豆网上搜索。

Graphs 算法竞赛入门经典 刘汝佳.docx

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1