Pages

Tuesday, May 11, 2010

P-6:

Question:
This one was recently given on Car Talk, and it's been making the rounds right now also...

The warden meets with 23 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 must move one, but only one of the switches. He can't move both but 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.' and be 100% sure.
"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."
What is the strategy they come up with so that they can be free?

Answer:

 This can be solved in following manner - State 0 means off, State 1 means On. Henceforth
00 - A is Off, B is Off
01 - A is Off, B is On
10 - A is On, B is Off.

Let us have two categories of states :-
1) Valid - 00 & 01
2) Invalid - 10 & 11

Also have a variable "Count" maintained by every prisoner that is local to each prisoner. The "Count" indicates total prisoners that have visited the switch room.

Process :-
1) Now whenever prisoner enters the switch room and encounters "Valid" state he increments his variable "Count" by one.
2) Whenever prisoner enters the switch room and encounters "InValid" state his variable "Count" remains same.
3) If the prisoner visited the switch room for the first time he will change the current state of switch to "Valid".
4) If the prisoner didn't visit the switch room for the first time he will change the current state of switch to "InValid".
5) If at any time a prisoner find his variable "Count" equal to total prisoners +1 i.e. 24 in current problem, he will declare that everyone has visited the Switch Room.

No comments:

Post a Comment