# Hackenbush

please somebody help me to understand how game value is calculated in game like hackenbush(for reference question CHEFGM asked in november long challenge can be taken)

2 Likes

Hey,
Check out this link. Has beautiful description of both partisan and non partisan games including hackenbush

3 Likes

The game given in the problem CHEFGM can be reduced to blue-red Hackenbush game. There are two types of hackenbush games: hackenbush stalk and hackenbush trees(there may be more). For the problem we are concerned with Hackenbush stalks since all piles are independent of each other. First we assign sign to the two possible moves, say even is taken as positive and odd as negative. The piles are arranged in such a way that removing any element would mean removing all elements that are placed above it in the same pile. For this, we sort the piles and remove duplicates.

Consider an example: This is the pile for which we want to calculate the value

10

7

5

4

2

We start from the lowest level and go up. The first number will get the value +1 or -1 depending upon whether it is even or odd. Here 2 gets +1. Now as we go up we assign +1 to every number till we encounter an odd number ie. all consecutive even numbers starting from the ground will be assigned +1.
So 4 also gets +1. Now when the first odd number is reached, every number will be assigned half the value that was assigned to the number just below(along with the sign). So 5 gets -1/2 , 7 gets -1/4 , 10 gets +1/8.

10…+1/8

7…-1/4

5…-1/2

4…+1

2…+1

So the total value of the pile becomes +1+1-1/2-1/4+1/8 = 11/8.

Another example:

10…+1/16

9…-1/8

8…+1/4

6…+1/2

5…-1

3…-1

1…-1

Total value is -1-1-1+1/2+1/4-1/8+1/16 = -37/16.

This way we calculate the value of all piles and add them. Now if the total sum comes out to be positive, then it means that if the first player chose even, he/she will always win the game. If this sum is negative, then it means odd is winning choice. And if it is 0 then it means the first player can never win.
You can refer to my solution http://www.codechef.com/viewsolution/2974201

6 Likes

Thank you! @sikander_nsit and @nitinj

I exactly had the same problem when I started reading Hackenbush. Have a look at this question http://math.stackexchange.com/questions/556014/what-is-worth-of-a-stalk-in-red-blue-hackenbush

1 Like

Hare on this http://www.essayhell.org/ link is the great discussion about Hackenbush that would be really interesting and useful. The number of professionals is having great programming skills and they also help others to give the solution of the problem. Hope the above answers are useful and help a lot to make things correct.

@sikander_nsit the place where i am getting confused is how we can assign such values to these numbers

1 Like

See the link put up by @nitinj.

@sikander_nsit approach to solve for hackenbush trees??

1 Like