We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This is good problem for learning game theory. Hope my comments help you:
The key to this problem is Grundy Theorem wich sounds like:
Any position of an impartial game is equivalent to a nim pile of a certain size.
or in more simple way:
Suppose there is a composite game (more than one sub-game) made up of N sub-games and two players, A and B. Then Sprague-Grundy Theorem says that if both A and B play optimally (i.e., they don’t make any mistakes), then the player starting first is guaranteed to win if the XOR(also known as nim-addition) of the grundy numbers(also known as nimbers) of position in each sub-games at the beginning of the game is non-zero. Otherwise, if the XOR evaluates to zero, then player A will lose definitely, no matter what.
So, we need now find all Grundy nunbers (shortly nimbers) and XOR all of them.
If from current game position there is no moves (Zero 0 stones in pile) then nimber Gn(0) = 0.
Nimber of N-position equal to minimal nimber of positions not reachable from current position.
For better understanding let's consiter term 'Mex': ‘Minimum excludant’ a.k.a ‘Mex’ of a set of numbers is the smallest non-negative number not present in the set.details here.
Now we can say:
Gn=Mex{Gn's of all possible positions we can reach from current}
Let's now find nimbers of our game. First we need to devide or game into sub games. in this case each subgame is tree in given forest.
The trees looks like this one where nuber of child nodes depend on given k:
We need to highlight following properties of tree:
-if number of nodes N>2, then we can remove either one leaf or whole tree N --> N-1 or N --> 0.
-but when N=2, then both nodes become leafs, in this case we can remove only one of the leaf N --> N-1
if N=3 we can move to 0-position or to 2-position:
Gn(3) = Mex{Gn(0), Gn(2)} = Mex{0,1} = 2
if N=4 we can move to 0-position or to 3-position:
Gn(4) = Mex{Gn(0), Gn(3)} = Mex{0,2} = 1
if N=5 we can move to 0-position or to 4-position:
Gn(5) = Mex{Gn(0), Gn(4)} = Mex{0,1} = 2
if N=6 we can move to 0-position or to 5-position:
Gn(6) = Mex{Gn(0), Gn(5)} = Mex{0,2} = 1
...and so on. We can see that:
for n =0 and n =2: G(n) = 0
for all other n: Gn(n) = (n-1)%2+1
Now we can calculate Grundy numbers (nimbers) for a tree of any height. And we only need to find XOR of all trees in given forest.
Below is my JavaScript solution passed all the tests:
functionGrudyNumber(tree){//function will return GrudyNumber (nimber) for given treereturn(tree==0||tree==2)?0:(tree-1)%2+1}functionbobAndBen(forest){letGrudyNumers=[]//find GrudyNumber(nimbers) for each treefor(lettreeofforest){GrudyNumers.push(GrudyNumber(tree[0]))}//return XOR (also known as nim-sum) of all Grundy numbersreturnGrudyNumers.reduce((a,b)=>a^b)!=0?'BOB':'BEN'}functionmain(){constg=parseInt(readLine(),10);for(letgItr=0;gItr<g;gItr++){constn=parseInt(readLine(),10);letforest=Array(n);for(leti=0;i<n;i++){forest[i]=readLine().split(' ').map(trees=>parseInt(trees,10));}console.log(bobAndBen(forest))}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bob and Ben
You are viewing a single comment's thread. Return to all comments →
This is good problem for learning game theory. Hope my comments help you:
The key to this problem is Grundy Theorem wich sounds like:
or in more simple way:
So, we need now find all Grundy nunbers (shortly nimbers) and XOR all of them.
Grundy nunber (or nimber or Gn) definition:
Let's now find nimbers of our game. First we need to devide or game into sub games. in this case each subgame is tree in given forest. The trees looks like this one where nuber of child nodes depend on given k:
We need to highlight following properties of tree:
-if number of nodes N>2, then we can remove either one leaf or whole tree N --> N-1 or N --> 0.
-but when N=2, then both nodes become leafs, in this case we can remove only one of the leaf N --> N-1
Back to our nimbers:
if thre is N=0 nodes in tree
if N=1, then we can move only to 0-position,
if N=2, we can move only to 1 position
if N=3 we can move to 0-position or to 2-position:
if N=4 we can move to 0-position or to 3-position:
if N=5 we can move to 0-position or to 4-position:
if N=6 we can move to 0-position or to 5-position:
...and so on. We can see that:
for n =0 and n =2: G(n) = 0
for all other n: Gn(n) = (n-1)%2+1
Now we can calculate Grundy numbers (nimbers) for a tree of any height. And we only need to find XOR of all trees in given forest.
Below is my JavaScript solution passed all the tests: