Prisoners Problem - Page 2
The surprising answer to this problem is that the prisoners will always go free, so long as they follow the correct strategy.
The prisoner at the back, who has to go first, cannot know whether they have a white or a black hat on their head. So, as it makes no difference whether they say white or black, they use their go to enable all the other prisoners to guess correctly, and thus all the prisoners go free. It works like this.
The prisoner at the back looks at the row of prisoners in front of him, and counts the number of black hats that he can see. If it is an odd number, he says 'black' and if it is an even number he says 'white'. He has a 50% chance of being correct as far as his own hat is concerned.
The prisoner in front of him counts how many black hats HE can see, and to this number he adds 1 if the prisoner behind him has said 'black'. If this number is odd, the second prisoner says 'black' since that will be the colour of his hat; however if the number is even, he should say 'white' and again this will be the colour of his hat.
Each prisoner in turn counts the number of black hats they can see in front of them, adds the number of times they have heard 'black' spoken, and they then say 'black' if the number is odd, and 'white' if the number is even.
Every prisoner will be able to guess correctly the colour of the hat they are wearing, apart from the prisoner at the back who guesses first. They have a 50% chance of being right. The jailor's conditions will have been met - not more than one mistake will have been made - and all the prisoners will go free.
Another way of thinking about the solution could be put this way... suppose the prisoner at the back can see 5 black hats in front of him, and he therefore says 'black' since 5 is an odd number. The prisoner in front of him happens to be wearing a white hat, and so he will also see 5 black hats in front of him. 5 plus 1 (the prisoner behind him said 'black') is an even number, therefore he guesses - correctly - that he is wearing a white hat. This will continue until we get to a prisoner who is actually waering a back hat. They will only see 4 black hats in front of them (they are wearing the 5th one!) and so they will add 4 and 1, which is an odd number, and correctly guess that they are wearing a black hat.