This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programming Diversions and the fly likes 23 Prisoners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "23 Prisoners" Watch "23 Prisoners" New topic

23 Prisoners

Arjun Shastry
Ranch Hand

Joined: Mar 13, 2003
Posts: 1893
Appeared recently in one newspaper Those who know the solution,wait till week end and then post your solution)
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.'
"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."
There exists a strategy such that, if the prisoners adopt it, they are guaranteed to be set free (given enough time). What is the strategy?

HS Thomas
Ranch Hand

Joined: May 15, 2002
Posts: 3404
A- on B- off
I think the switches are not relevant. Given enough time each prisoner will have visited the switch room. The strategy would be to calculate how much time is enough time.
23 x 23 = 529
After 529 days may be enough time ?
23 x 23 x 23 days ?
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11957

The problem doesn't say anything about how many times each will visit, or how many days inbetween visits. It just says "from time to time". maybe the warden goes for 2 years and never lets anyone in the switch room.
at least, that's how i'm reading the problem (somebody let me know if i'm wrong). So, i don't see how you can just say 23 X 23 days, or 23 ^ 3 days.
i have no idea what the answer is, but i'm curious now.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
HS Thomas
Ranch Hand

Joined: May 15, 2002
Posts: 3404
I was thinking along the lines :
Max no of days between calls for each prisoner = 23 days x 23 prisoners.= 529 days seemed reasonable.For each day that the prisoner does get called between the 23 days add another 23 days. The warden should tire of this game and the alligators would die of hunger anyway.
I forget what the logic behind 23 ^ 3 was - but that's just over 33 years and I doubt the prisoners would accept that.
No I think they'll need some cunning strategy to get out quicker.
Two false parts to a statement make the statement true.Perhaps they can play around with the statement - 'We have all visited the switch room.'
something the warden CANNOT refute
'It's false that we have all visited the switch room'
'Isn't it true that not all visited the switch room ?'
C'mon Cap. True or false ?
[ December 10, 2003: Message edited by: HS Thomas ]
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

For those truly interested but don't want to wait for the answer, this was posted in this forum in August
The solution is really pretty neat, though, so try to solve it before reading that thread (which doesn't actually contain the solution, anyway, although it does link to it).

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
HS Thomas
Ranch Hand

Joined: May 15, 2002
Posts: 3404
Originally posted by Joel McNary:
For those truly interested but don't want to wait for the answer

No harm in trying different approaches though, IMHO.
While truly impressed with the answer ( like to meet a sixth-grader who could come up with that solution) I couldn't help thinking : what if a prisoner like,you know, died...or couldn't count over long periods of interruptions or became withdrawn.
[ December 11, 2003: Message edited by: HS Thomas ]
Don't get me started about those stupid light bulbs.
subject: 23 Prisoners
It's not a secret anymore!