Friday, November 02, 2007

Prisoner Puzzle

A warden meets with 'N' new prisoners when they arrive. He tells them, "You may meet today and plan a strategy. But after today, you will be in isolated cells and will have no communication with one another.

"In the prison is a switch room, which contains two light switches labeled A and B, each of which can be in either the 'On' or the 'Off' position. I am not telling you their present positions. The switches are not connected to anything.


"After today, from time to time whenever I feel so inclined, I will select one prisoner at random and escort him to the switch room. This prisoner will select one of the two switches and reverse its position. He has to move atleast one of the switches. He can't move none, either. Then he'll be led back to his cell.

"No one else will enter the switch room until I lead the next prisoner there, and he'll be instructed to do the same thing. I'm going to choose prisoners at random. I may choose the same guy three times in a row, or I may jump around and come back.

"But, given enough time, everyone will eventually visit the switch room as many times as everyone else. At any time anyone of you may declare to me, 'We have all visited the switch room.'

"If it is true, then you will all be set free. If it is false, and somebody has not yet visited the switch room, you will be fed to the alligators."

Here's the question:
Is there a strategy the prisoners devise can devise? If YES, What? If NO, why NOT?

Answer to be posted as a comment in three days.

3 comments:

Shantanu said...

1. Let the two switches be A & B
2. Elect a leader.
3. Rest always toggle B. However, if A is in OFF state, they turn it
ON only once. i.e if they already have turned ON A once, then in
their subsequent visits , even if they see the switch OFF, they will not disturb it.
4. When leader enters, if A is turned ON he turns it OFF and increases his count.
5. Essentially when he counts N-1, he would be sure of everyone having visited.

Anonymous said...

But what if switch A is initially set to On? If the non-leaders leave it alone, the leader will count it as a prisoner.

Let's say there are only two prisoners. That means there is Leader (L) and the Rest (R). The initial setting of A is On. L is the first one led into the room. He sees A is On, flips it off, increases his count, then says "oh! N-1 is 1, and my count is now 1." and goes to the warden to say "we've all been to the switch room." It's alligator lunch-time.

Shantanu said...

I agree (and the simplification is apt).

Just to be sure , Leader(L) can wait till the count has reached N (and not N-1).

Mr. anonymous, I bet you are a tester who gives nightmares to the developers.