Sei sulla pagina 1di 19

Introductionto Combinatorial(noteconomics)

GameTheory
[AnilKishore] student,IIITHyderabad,India.

GeneralGameSetting

TwoplayersAandB Rulesofthegame:

Possiblemovesaplayercantakeinhis/herturn Winningcondition

Impartial:AandBhavesamesetofmoves Bothplay'optimally' IfAstartsfirst,whowillwin?

FirstGame:BowlingPins

Nbowlingpinsarrangedinarow PossibleMoves

Hitexactlyonepin Hitinmiddleofany twoadjacentpins

Winner:Onewhoknocksdownthelastpin GivenN,whowinsifAtakesthefirstmove?

Solution:FirstGameBowlingPins

Acanknockdownthemiddlepin(s)(1foroddN&2for evenN)andmaketwoindependentgamesofsamesize Now,justmimicwhatBdoes,intheotherhalf. Aalwayswins:)

StoneGame:RESN04onCodeChef

AliceandBobplaythefollowinggame:ThereareNpilesof stoneswithSistonesintheithpile.Pilesarenumberedfrom1 toN.AliceandBobplayalternately,withAlicestarting.Ina turn,theplayerchoosesanypileiwhichhasatleastistonesin it,andremovesexactlyistonesfromit.Thegameendswhen thereisnosuchpile.Theplayerwhoplayslastwinsthe game.AssumingAliceandBobplayoptimally,whowillwin thegame?

Solution:StoneGame

Idea:Exactly(S[i]/i)movescanbemadeatpilei TotalmovesinwholegameT=Sumoveri(S[i]/i) NothingAorBcandoclever.JustmakethoseTmovesalternatively DependsonlyontheparityofT


int nummoves = 0; for (int i=1; i<=N; i++) { nummoves += piles[i]/i; } puts( nummoves%2 ==0 ? BOB : ALICE );

WinLoseBruteforceStrategy
1.Allterminalpositionsarelosing. 2.Ifaplayerisabletomovetoalosingpositionthenhe isinawinningposition. 3.Ifaplayerisabletomoveonlytothewinning positionsthenheisinalosingposition.
boolean isWinning( position X ) { for (all positions Y that I can go to) if ( !isWinning(Y) ) return true; return false;

Game:Takeaway134

Therearencoins.Whenitisaplayer'sturnhecan takeaway1or3or4coins.Theplayerwhotakes thelastoneawayisdeclaredthewinner(inother words,theplayerwhocannotmakeamoveisthe loser). Question:Isn=11awinningorlosingposition?

Solution:Takeaway134

11iswinningposition

Game:Takeaway1toK

Therearencoins.Whenitisaplayer'sturnhecan takeawayatleast1andatmostKcoins.Theplayer whotakesthelastoneawayisdeclaredthewinner (inotherwords,theplayerwhocannotmakeamove istheloser). Question:ThoughexhaustivelyenumeratingWL works,canwefindsomepatternforLpositions?

Solution:Takeaway1toK

L O S E

W I N

Inourproblem,thepropertyPcanbe:mod(K+1)=0 FromX(Xmod(K+1)==0),allmovesleadtoaY(Ymod(K+1)!=0) FromX(Xmod(K+1)!=0),thereexsitsamovetoY(Ymod(K+1)=0) So,NisalosingpositionifN%(K+1)==0,elseitswinning

TheGameofNIM

GivensizesofNheapsofstones

Possiblemove:Pickanyheapandremovenonzero stonesfromit Winner:Onewhoremovesthelaststone GivenintSize[n],whowinsifAstartsfirst?

Solution:NIM

L O S E

W I N

// read input Size[] r = 0; for(i=0;i<n;i++) r = r ^ Size[i];

puts( r!=0 ? "A Wins" : "B Wins");

NewStoneGame

AlmostsameaspreviousStoneGameexceptfortheboldpart AliceandBobplaythefollowinggame:ThereareNpilesof stoneswithSistonesintheithpile.Pilesarenumberedfrom1 toN.AliceandBobplayalternately,withAlicestarting.Ina turn,theplayerchoosesanypileiwhichhasatleastistonesin it,andremovesk*istonesfromit,foranypositiveinteger kandofcoursek*i<=Si.Thegameendswhenthereisno suchpile.Theplayerwhoplayslastwinsthegame.Assuming AliceandBobplayoptimally,whowillwinthegame?

Solution:NewStoneGame

Idea:Frompilei,wecanremove1*ior2*ior3*i....or(S[i]/i)*istones LooksexactlylikeNIMwithheapsize(S[i]/i)

int r = 0; for (int i=1; i<=N; i++) { r ^= piles[i]/i; } puts( r==0 ? BOB : ALICE );

THEGAME:Quark'10BitsGoa

TomandHanksplaythefollowinggame.Onagameboard havingalineofsquareslabelledfrom0,1,2...certainnumber ofcoinsareplacedwithpossiblymorethanonecoinona singlesquare.Ineachturnaplayercanmoveexactlyone cointoanysquaretothelefti.e,ifaplayerwishestoremove acoinfromsquarei,hecanthenplaceitinanysquarewhich belongstotheset(0,1,...i1).Giventhedescriptionofthe boardandalsoassumingthatTomalwaysmakesthefirst moveyouhavetotellwhowinsthegame(AssumingBoth playOptimally). BoardsizeN(1<=N<=105).
Numberofcoinsoneachsquare:X(0<=X<=105)

Solution:THEGAME(QCJ6inCodeChef)

Idea:EachstoneatpositionP,correspondstoheapofsizePinNIM r=x^x^x^x.....^x(ntimes) neven:r=0 nodd:r=x scanf("%d",&n); r=0; for(i=1;i<=n;i++) { scanf("%d",&x); if(x&1) r ^= i; }

puts(r==0?"Hanks Wins":"Tom Wins");

TakeHomeGames (Notsomegamecategory:p,trytheseoutinyourrooms)

Takeaway1+Primes(={1,2,3,5,7,11,...}) IdentifyapatterninTakeaway{1,3,8} OrderedNim:Theheapsarenumbered1throughN,and aplayercanonlyremovestonesfromaheapifallthelower numberedheapsareempty. Twopileswithp&qstonesrespectively.Amovecanbe 1.)Takeonestonefromapile,2.)Takeonestonefrom eachpile,3.)Moveastonefromonepiletoother. Winner:Whotakesthelaststone.AwinsorBwins?

References&furtherplaying

Youcanseeavarietyofgameshereandalsoplaywithcomputer:) http://www.cuttheknot.org/Curriculum/index.shtml#games TopCoderTutorial http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=algorithmGames http://www.madras.fife.sch.uk/maths/games/ CodeChefTutorialonWythoff'sGame http://www.codechef.com/wiki/tutorialgametheory

Thatsallfolks...HappyGaming!

Potrebbero piacerti anche