6
2
8
1
AZXU5
1GU2D4B
K
PO4IUTFV
THE
Q34NBVC78
T
1961
XWS34WQ

LNGLNSNXTTPG
ED
MN
MLMNG
?
4
2
6
2
QWERTY
FLHL
?
3
1
8
3
whensingle
colmnyou
canget
correct
output
?
4
3
4
1
1
bdd2

d4nxt1

f3
gcdxx1
h
ijk3
jzy4aaa1b2ovc3zzd4aga1lkb2drc3yud4qqe1mnf2weg3poh4ovxrsz

cde
true

falseerrorstatusagainoverdne
?
1
1
1
1
abcd
?
3
4
4
1
?
30
2
24
1
ProblemB
Patience
Astheproverbsays
Patienceisbitterbutitsfruitissweet
Writingprogramswithinthelimitedtimemayimposesomepatience
onyoubutyouenjoyitandwinthecontestwehope

Thewordpatiencehasthemeaningofperseverancebutithasanothermeaning
incardgamesCardgamesforoneplayerare
calledpatienceintheUKandsolitaireintheUS

Letsplayapatienceinthisproblem

Inthiscardgameyouuseonlytwentycardswhosefacevaluesare
positiveandlessthanorequalto5
Justfourcardsareavailableforeachfacevalue

AtthebeginningthetwentycardsarelaidinfiverowsbyfourcolumnsSee
Figure1AllthecardsaredealtfaceupAnexampleof
theinitiallayoutisshowninFigure2
Thepurposeofthegameistoremoveasmanycardsas
possiblebyrepeatedlyremovingapairofneighboringcardsofthesame
facevalueLetuscallsuchapairamatchingpair

Thephraseapairofneighboringcards
meansapairofcardswhichareadjacenttoeachotherForexampleinFigure1C6is
adjacenttoanyofthefollowingeightcards
C1C2C3C5C7C9C10andC11IncontrastC3isadjacenttoonlythefollowing
threecardsC2C6andC7
Everytimeyouremoveapairyoumustrearrangetheremainingcardsascompactaspossible
ToputitconcretelyeachremainingcardCimustbeexaminedinturninits
subscriptordertobeshiftedtotheuppermostleftmost
space
Howtoplay
1Searchamatchingpair
2Whenyoufindmorethanonepairchooseone
InFigure3youdecidedtoremovethepairofC6andC9
3Removethepair
4Shifttheremainingcardstotheuppermostleftmostspace
5Repeattheaboveprocedureuntilyoucannotremoveanypair
Ifyoucanremoveallthetwentycardsyouwinthegameandyourpenaltyis0
Ifyouleavesomecardsyoulosethegameandyour
penaltyisthenumberoftheremainingcards
Wheneveryoufindmultiplematchingpairsyoumustchooseonepairoutofthemasinthestep
2oftheaboveprocedureTheresult
ofthegamedependsonthesechoices
Yourjobistowriteaprogramwhichanswerstheminimalpenaltyforeachinitiallayout

Input

TheinputconsistsofmultiplecardlayoutsTheinputisgiveninthefollowingformat

N
Layout0
Layout1

LayoutN1

NisthenumberofcardlayoutsEachcardlayoutgivestheinitialstateofagame
Acardlayoutisgiveninthefollowingformat

C0C1C2C3
C4C5C6C7
C8C9C10C11
C12C13C14C15
C16C17C18C19

Ciisanintegerfrom1to5whichrepresentsthefacevalueofthecard

Output

Foreveryinitialcardlayouttheminimalpenaltyshouldbeoutputeachinaseparateline

SampleInput

4
1452
3143
5422
4523
1135
5151
4532
3214
1453
2342
1212
5454
2121
3534
3354
4231
2531
3542
1541
4532

OutputfortheSampleInput

0
4
12
0
0

?
35
3
16
1
AincludeJBstdioDhC
AincludeJBsysRtypesDhC
AincludeJBsysRsocketDhC
AincludeJBnetinetRinDhC
AincludeJBstringDhC

AdefineJWQLENJ5

voidJerrorEcharJZsF
G
JJperrorEsFJI
JJexitE1FJI
H

intJSetupForClientEcharJZipKJcharJZsprotoKJcharJZportnumF
G
JJintJfdJI
JJstructJsockaddrLinJcliJI
JJintJtypeKJprotoJI

JJmemsetEMcliK0KsizeofEcliFFJI
JJcliDsinLfamilyJQJAFLINETJI
JJcliDsinLaddrDsLaddrJQJinetLaddrEipFJI
JJcliDsinLportJQJhtonsEatoiEportnumFFJI
JJifEPJstrcmpEsprotoKOtcpOFFJG
JJJJtypeJQJSOCKLSTREAMJIJprotoJQJIPPROTOLTCPJI
JJH
JJelseJG
JJJJtypeJQJSOCKLDGRAMJIJprotoJQJIPPROTOLUDPJI
JJH
JJfdJQJsocketEAFLINETKJtypeKJprotoFJI
JJifEfdJBJ0FJerrorEOcanJnotJopenJsocketOFJI
JJifEbindEfdKJEstructJsockaddrJZFMcliKJsizeofEcliFFJBJ0F
JJJJerrorEOcanJnotJconnectOFJI
JJifEtypeJQQJSOCKLSTREAMJMMJlistenEfdKJWQLENFF
JJJJerrorEOcanJnotJsetJlistenOFJI
JJreturnJfdJI
H

AdefineJCOMMANDLENJ1024
AdefineJMAXBUFJ256

voidJReturnErrorEFILEJZsfpKJintJcodeKJcharJZmessageF
G
JJfprintfEsfpKJOHTTPR1D0JVdJVsSrSnOKJcodeKJmessageFJI
JJfprintfEsfpKJOContentNtypeYJtextRplainSrSnOFJI
JJfprintfEsfpKJOSrSnOFJI
JJfprintfEsfpKJOErrorYJcodeQVdJVsSrSnOKJcodeKJmessageFIJ
JJfflushEsfpFJI
H
voidJRecvCommandEFILEJZrfpKJcharJZcommandF
G
JJifEfgetsEcommandKJCOMMANDLENKJrfpFJQQJNULLF
JJJJerrorEOcanJnotJreceiveJcommandOFJI
H
voidJSendHeaderEFILEJZsfpF
G
JJfprintfEsfpKJOHTTPR1D0J200JOKSrSnOFJI
JJfprintfEsfpKJOContentNtypeYJtextRplainSrSnOFJI
JJfprintfEsfpKJOSrSnOFJI
JJfflushEsfpFJI
H
voidJSendBodyEFILEJZsfpKJFILEJZfpF
G
JJintJlenJI
JJcharJbufT1024VJI
JJwhileEElenJQJfreadEbufKsizeofEcharFKJsizeofEbufFKJfpFFJCJ0F
JJJJfwriteEbufKsizeofEcharFKJlenKJsfpFJI
JJfflushEsfpFJI
H

intJServerProcessEFILEJZrfpKJFILEJZsfpF
G
JJFILEJZfpJI
JJcharJbufTCOMMANDLENVJI
JJcharJcmdTMAXBUFVKJpathTMAXBUFVKJverTMAXBUFVJI

JJRecvCommandErfpKJbufFJI
JJifEsscanfEbufKJOVsJVsJVsOKJcmdKJpathKJverFJCJ3JWW
JJJJJstrcmpEcmdKJOGETOFJWWJstrcmpEverKJOHTTPR1D0OFFJG
JJJJReturnErrorEsfpKJ501KJOinvalidJcommandOFJIJ
JJJJreturnJ501JI
JJH
JJifEEfpJQJfopenEpathX1KOrOFFJQQJNULLFJG
JJJJReturnErrorEsfpKJ404KJOfileJnotJfoundOFJIJ
JJJJreturnJ404JI
JJH
JJSendHeaderEsfpFJI
JJSendBodyEsfpKJfpFJI
JJfcloseEfpFJI
H

mainEintJargcKJcharJZargvTVF
G
JJintJiKJfdKJlKJdfdKJclenKddJI
JJFILEJZsfpKJZrfpJI
JJstructJsockaddrLinJcliJI
JJcharJrequestT1024VIJ
JJ
JJfdJQJSetupForClientEargvT1VKJOtcpOKJargvT2VFJI
JJprintfEOserverYJwaitngJforJclientSnOFJI
JJclenJQJsizeofEcliFJI
JJdfdJQJacceptEfdKJEstructJsockaddrJZFMcliKJMclenFJI
JJifEdfdJBJ0FJerrorEOacceptJerrorOFJI
JJprintfEOserverYJconnectedJfromJVxSnOKJntohlEcliDsinLaddrDsLaddrFFJI
JJrfpJQJfdopenEdfdKJOrOFJIJsfpJQJfdopenEdfdKJOwOFJIJ
JJifErfpJQQJNULLJWWJsfpJQQJNULLFJerrorEOcanJnotJfdopenOFJI
JJServerProcessErfpKJsfpFJI
JJfcloseEsfpFJIJJfcloseErfpFJI
JJcloseEdfdFJI
JJcloseEfdFJI
H
?
0
