King and 100 prisoners

Source: Internet
Author: User

The king recruited 100 prisoners and said to them, "You have committed the sin of death and should have killed all of you, but I am compassionate and give you a chance to survive. 15 minutes later, you will be put in a prison with 100 isolated cells. Each person in a cell is isolated from the outside, and you cannot hear or see anything. You cannot even calculate the time, let alone obtain any external information. (Except for meals, but it is also irregular)

The prison has a yard, which opens the door of a cell randomly (note that it is completely random) every day to bring the prisoner to the yard to let the wind shine. There is a street lamp in the yard, and the tornado prisoner can control its switch to turn it on or off. No one except the prisoner will touch the switch. This lamp will always have sufficient energy supply. If the light bulb breaks down or the electric road fails, it will be repaired immediately. Of course, the repair personnel will not change the light state (ON or OFF ).

In addition to switching on the light, any other trace left by the prisoner during the wind will be cleared at night (including any marks on the light ).

The cell is completely closed and the lights in the yard are invisible to the cell. Only those who let the wind out of the yard can see it.

Now I want to make a request to you. If you do, you can release all of them: several days later, if one of you can prove to me that all of you have been in the yard, you will be all released. Of course there must be evidence! Because I will only give you one chance. If the person who proves to me cannot justify himself, you will cut your heads. Therefore, we must cherish this opportunity. If you never do what I want, you will be shut down to death.

Now we will give you 15 minutes to discuss your plan. 15 minutes later, you will be put in the prison I just mentioned and will never be able to communicate again.

 

 

Initial thought:

The task of the first person to go out: Turn on the light and only turn on the light. Other people's tasks: Turn off the lights and turn off the lights only when they have never been turned off.

 

In this way, the first person saw 99 lights closed after countless times, and they were free.

 

Thoughts on netizens:

I have several points on this question:
1. Prisoners do not know the time. That is to say, they do not know who they will be. According to the conditions, they do not know the day.
2. Random exposure of prisoners. Here, the same prisoner may be exposed for many times.
3. The street lamp is the only tool for communication.
4. The biggest problem now is the initial state of the unknown lights.

My solution:
1. Count prisoners. 100 specify a unique counting prisoner.
2. Rules:
(1) All prisoners, except for counting prisoners, set the light to open if the light is turned off for 1st times, and then set the light to be turned on without interfering with the light. However, if the light is on for 1st times, I think I have never been in the wind.
(2) count the number of times prisoners turn on the light. After counting, turn off the light. End when the count reaches 99.

Details: When counting prisoners 1st times out, if the light is on. There are three cases: 1. the light is on by default, and the light is 1st. 2. The light is on by default, and other prisoners do not touch the switch. 3. The light was originally turned off, and a tornado prisoner switched it on.
Now the problem arises. because we do not know the initial state of the lights, there is an error in counting 1st prisoners. The error is one person. But if the lights are turned off when the counting prisoners see the wind, everything will be fine.

This idea is quite good, but I still have a question:
When the light is turned off 99 times, it indicates that everyone has come out. If the light is turned on at first, the first person who turns the light off must be the counting worker, the clerk thinks that one person has already come out (in fact no one has come out). When the number reaches 98th, the clerk has already reached 99 and thinks everyone has come out, then everyone was pulled out and hacked. If the clerk considers this situation, most of them will go to 100. When the initial light is turned off, they will never go out, because everyone only opens it once, it will never reach 100.

 

This idea is quite good, but I still have a question:
When the light is turned off 99 times, it indicates that everyone has come out. If the light is turned on at first, the first person who turns the light off must be the counting worker, the clerk thinks that one person has already come out (in fact no one has come out). When the number reaches 98th, the clerk has already reached 99 and thinks everyone has come out, then everyone was pulled out and hacked. If the clerk considers this situation, most of them will go to 100. When the initial light is turned off, they will never go out, because everyone only opens it once, it will never reach 100.

 

 

 

JAVA Implementation Demo:

  1. ImportJava. util. Random;
  2. Public ClassPrisoner2 {
  3. /**
  4. * Number of prisoners (greater than 1)
  5. */
  6. Private Static Final IntMaid = 100;
  7. /**
  8. * @ Param ARGs
  9. */
  10. Public Static VoidMain (string [] ARGs ){
  11. IntMinyear = integer. max_value;
  12. IntMaxyear = integer. min_value;
  13. For(IntI = 0; I <100; I ++ ){
  14. IntYear = prisonbreak ();
  15. If(Minyear> year ){
  16. Minyear = year;
  17. }
  18. If(Maxyear <year ){
  19. Maxyear = year;
  20. }
  21. System. Out. Print (prisonbreak () + ",");
  22. }
  23. System. Out. println ();
  24. System. Out. println ("minyear:" + minyear );
  25. System. Out. println ("maxyear:" + maxyear );
  26. }
  27. Private Static IntPrisonbreak (){
  28. Random r =NewRandom ();
  29. BooleanLighton = R. nextboolean (); // initial state of the light
  30. Int[] Prisoner =New Int[Prisoner_count]; // prisoner. The value is the number of lights on, up to two times.
  31. Final IntCounter_id = R. nextint (prisoner_count); // specify a Random counter and only turn off the light
  32. IntCounter = 0; // count the number of lights that the clerk noted down
  33. IntDays = 0; // Total number of days spent
  34. IntFreecount = (prisoner_count-1) * 2; // when the number of lights is reached, it is free.
  35. While(Counter <freecount ){
  36. Days ++;
  37. IntN = R. nextint (prisoner_count); // The prisoner who randomly releases the attack
  38. If(N = counter_id) {// counter_id
  39. If(Lighton) {// turn off the light when the light is on
  40. Lighton =False;
  41. Counter ++;
  42. Prisoner [counter_id] ++; // The counting worker records the number of switched lights.
  43. }
  44. }Else{// Other prisoners
  45. If(! Lighton & prisoner [N] <2) {// The light is off and the number of lights cannot exceed 2
  46. Lighton =True;
  47. Prisoner [N] ++;
  48. }
  49. }
  50. }
  51. // Check result
  52. IntSum = 0;
  53. For(IntI = 0; I <prisoner_count; I ++ ){
  54. If(Prisoner [I]> 0) {// if the light is turned on, it indicates that the light is idle.
  55. Sum ++;
  56. }
  57. }
  58. AssertSum = prisoner_count: "something wrong .";
  59. ReturnDays/365; // about
  60. }
  61. }

 

 

The optimal logical solution does not consider the initial state of the lamp:

Clear conditions:
1. Every person in a cell is isolated from the outside world. Nothing can be heard or seen, and time cannot be calculated.
Note: No one knows how many days he or she came out, so don't discuss which prisoner came out on the first day as a stenographer.
2. the door of a cell is opened randomly every day (note that it is completely random), so that the prisoner can come to the yard to let the wind go.
Note: The prisoner can only know that he is going to come out and go back, and the status of the light is displayed.

Here, we only solve the problem to solve the problem. We don't need to consider the problem of the question itself. We don't have to worry about how many days the teenager spent and whether the king will die.
If the condition is to randomly place a prisoner every 10 minutes, it will be much faster, so there is no need to ignore the rationality of the proposition itself, as long as you consider whether there is
100% of the no-mistakes solutions do not bring a trace of luck.

Solution:
1. Specify 99 normal prisoners. They have two actions: one is to turn off the light in the active state; the other is to count the number of times they turn off the light, if two times later, the light will no longer be turned on or off.
2. Designate a stenographer. He has only two actions: one is to turn on the light in the off state; the other is to take statistics from himself for the first time (Note: not the First Day) if the light is on for the first time, it indicates that the light is on, and the light starts to be on. If the light is on for the first time, it is impossible to confirm the status of the light and the number of times the light was turned off reached 198 times, then we can confirm that all people have been to the yard.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.