1

Given is a row of at most 30 stones which can either be black or white. No gaps are allowed at the start of the game, but there can be less than 30 stones. The goal is to remove all the stones. Only black stones can be removed, if a stone is removed, its neighbours change colours. If the stone removed was in the middle, this produces a gap that can't be filled any more; the neighbours of that stone are not considered neighbours after the stone is removed.

Now, I have created a program that solves this game using brute force. I have concluded that the game is only solvable if there are any black stones at all (obviously) and if the number of black stones is odd. Also, if the number of black stones is odd, the game can be solved by recursively removing the first black stone of the row.

My problem is I can't prove this condition that the number of black stones must be odd and that removing the first stone will solve the game. How can I correctly prove this algorithm?

I already tried to use induction, but I'm stuck:

Row(a,b) = a*black + b*white

RemoveFirstBlack(Row(1, b)) = RemoveFirstBlack(black + b*white) = 0 (if a=1 or n = 0 where a=2n+1 and n an integer)

Assume RemoveFirstBlack(Row(k*a, b)) = RemoveFirstBlack(k*a*black + b*white) = 0 with k = 2p + 1 and p an integer.

RemoveFirstBlack(Row((k+1)*a, b)) = RemoveFirstBlack((k+1)*a*black + b*white) = RemoveFirstBlack((2(p+1)(2n+1))*black + b*white) = RemoveFirstBlack(2(p+1)*a*black + b*white) = 0?

Thanks in advance for any pointers at all!

Michael J. Barber
  • 24,518
  • 9
  • 68
  • 88
rbnvrw
  • 347
  • 3
  • 15
  • 1
    Using math and game theory. You can't find proof positive of anything by showing it's true for more and more examples. The best you could hope for would be a result that proved you wrong. – Alexander Corwin Mar 14 '12 at 13:07
  • @AlexanderCorwin, what do you mean? An inductive proof would be perfectly valid in this case. – aioobe Mar 14 '12 at 13:25
  • Isn't there a separate "Theoretical Computer Science" StackExchange site? – Charles Duffy Mar 14 '12 at 13:34
  • @aioobe you can't inductively prove something with software. The question doesn't belong on stack overflow; if he's trying to do it with software, he cant, and if he's trying to do it with proper induction, he's asking the wrong people. – Alexander Corwin Mar 14 '12 at 13:37
  • @AlexanderCorwin, of course you can prove things in software, *especially* using induction. Trust me, I've been a teaching assistant on *Logic for Computer Scientists* for several years, and have about 90% of a Ph.D. in formal methods :-) BTW, I'm not sure he's asking the wrong people. He's asking a question about an algorithm (should be in scope of SO). The question just requires a slightly more formal answer than the usual ones on this site. – aioobe Mar 14 '12 at 13:41
  • 2
    You mention gaps. If I remove a stone in the middle, is there now a gap separating two distinct rows? Are the neighbors of a stone I remove now considered neighbors? – Michael J. Barber Mar 14 '12 at 14:06
  • 1
    Thanks for your insights. I asked my question here because I believe algorithms are closely related to programming, and the correctness of the algorithm to the correctness of the program, but correct me if I'm wrong ;) @MichaelJ.Barber: if a stone in the middle is removed, there is a gap that can't be filled any more. So the neighbours of that stone are not considered neighbours after the stone is removed. – rbnvrw Mar 15 '12 at 08:26
  • It would probably help to edit your question to spell out more precisely what the rules of the game are. – Michael J. Barber Mar 15 '12 at 08:59
  • @MichaelJ.Barber: all the rules there are to this game are explained in the first alinea. You start with a consecutive row of black and white stones, black ones can be removed and their removal triggers a change of colour in their immediate neighbours. Sorry, I can't state it any clearer. – rbnvrw Mar 15 '12 at 09:02
  • I've taken a shot at clarifying the explanation of the gaps. Please improve further if it's still not right. – Michael J. Barber Mar 15 '12 at 09:11
  • @MichaelJ.Barber: yes, that is completely right. Thanks for the extra clarification. – rbnvrw Mar 15 '12 at 09:15

2 Answers2

2

I suggest you try to treat two moves as one. (I.e. that the 30 stones are removed in 15 moves.)

This would allow you to show that the property of having an odd or even number of black stones is invariant throughout the game. A proof sketch follows:

Base cases: Two stones left. Odd number of blacks. Both stones can be removed in one double-move:

b w       -> _ b        ->  _ _
w b       -> b _        ->  _ _

For four stones or more, list the various different possible prefixes where O and E stands for a suffix-sequence of stones with an odd and even number of blacks respectively.

Here's two cases to get you started:

b b w b E  -> _ w b b E  ->  _ w _ w E
b b w w O  -> _ w b w O  ->  _ b _ b O
....

In each case you note that the resulting sequence (_ w _ w O for instance) contains an odd number of blacks.

Since if a sequence consists of one stone, and the number of stones is odd, then that single stone must neccesarily be black, which means that also the last stone can be removed.


Noticed that you also wanted to show that it was impossible if there was an even number of black stones. This is just as easy. The base cases (b b and w w) are impossible to solve and since each double-move removes an even number of black stones, you're out of luck if you start with an even number :-)

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Thank you aioobe, this is very helpful! I will try it this way and post back whether I succeeded ;) – rbnvrw Mar 15 '12 at 08:30
  • There is one thing that still bugs me: suppose I have started with bbwwO, this leads to _b_b O as above. This is unsolvable because the _bO has become a new row of stones with an even number of blacks, right? – rbnvrw Mar 15 '12 at 08:39
  • Although the basic idea is good, this isn't correct. Since the rows can be split into independent sub-problems, it is not an invariant that the number of black stones will be odd. Consider `wwbww`. Clearly solvable, but you must go through states with an even number of black stones. – Michael J. Barber Mar 15 '12 at 08:57
  • Hmm.. I interpreted the middle stone of x_x_x having two neighbors. My misunderstanding I guess. – aioobe Mar 15 '12 at 09:07
  • Sorry if it is unclear, but if we have x1_x2_x3, the stone x2 has no neighbours, because there are only gaps around it. – rbnvrw Mar 15 '12 at 09:14
2

Assume we have a stone group that is solvable without splitting the group (if you must split the group then you actually have two groups that do not need split). Last stone to remove from the group must be single black [B]. The only way to get to [B] is through [WB], there is no other way. To get to [WB] you need either [BBB] or [WWB]. From here the pattern emerges. The only way to get to [xxW] is through [xxBB], and to get to [xxB] is through [xxWB]. In all these transitions the parity is unchanged, and final number of black stones is odd (one), so parity of a single non-splittable group must be odd.

Let's say that solution requires splitting a group to two non-splittable sub-groups. We already concluded that those two sub-groups must have odd parity. If we exclude the black stone that will make a transition to a new state those two groups actually have even number of blacks. If we add them, and add the black stone that will make the transitions we can conclude that a group that is solved by splitting it to two non-splittable groups must also have odd number of black stones.

Using induction on single-splitting groups we can conclude that any group must have odd number of blacks.

The solution to the original problem does not require brute force. Just pick the first black stone that you come along.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • Could you comment on the fact that the removal of one group may affect the colors of the stones of neighbouring groups? – aioobe Mar 14 '12 at 20:29
  • I don't quite understand. Group is removed when the last stone (black) is removed. That removal does not affect any other group or stone, because there are no neighboring stones. – Dialecticus Mar 14 '12 at 23:44
  • Thanks for your answer! I think I know what you're getting at. We start at a possible solution and do the logic backwards. Suppose we have a small group of stones n=3, the only solvable cases are [WWB] and [BBB] (order can be reversed). These small subgroups can be patched together only by adding a [B] in between if they still must be solvable. But how do we prove this? If this is proofed, we can indeed prove it via induction. And yes, I know this doesn't require brute force, but it was my first line of attack. I quickly discovered this other method ;) – rbnvrw Mar 15 '12 at 08:57
  • BTW, the problem is much more interesting and harder if gaps do not prevent stones to neighbor one another. – Dialecticus Mar 15 '12 at 15:51