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.

P-5:

Question:
Four people need to cross a rickety rope bridge to get back to their camp at night. Unfortunately, they only have one flashlight and it only has enough light left for seventeen minutes. The bridge is too dangerous to cross without a flashlight, and it’s only strong enough to support two people at any given time.
Each of the campers walks at a different speed. One can cross the bridge in 1 minute, another in 2 minutes, the third in 5 minutes, and the slow poke takes 10 minutes to cross. How do the campers make it across in 17 minutes?

Answer:
To get everyone across in 17 minutes, we need get the two slowest people across together; otherwise we are wasting too much time. Once we get them across, how do we not make one of them walk back with the flashlight? Just have one of the faster people already there waiting to sprint the flashlight back across.

person A: 1 minute
person B: 2 minutes
person C: 5 minutes
person D:10 minutes

1. A & B cross.  total time:  2 minutes.
 
 C |==========================| A
 D |                          | B
   |==========================| flashlight
 
2. B comes back. total time:  4 minutes.
 
 C |==========================| A
 D |                          | 
 B |==========================| 
flashlight
 
3. C & D cross.  total time: 14 minutes.
 
 B |==========================| A
   |                          | C
   |==========================| D
                               flashlight
 
4. A comes back. total time: 15 minutes.
 
 A |==========================| C
 B |                          | D
   |==========================| 
flashlight
 
5. A & B cross.  total time: 17 minutes.
 
   |==========================| A
   |                          | B
   |==========================| C D
                              flashlight 
 
Another valid solution is to have A bring the flashlight back in step 2.