First, the problem description
On a dark night, a total of four travelers came to a narrow, non-guardrail bridge. If you don't use a flashlight, everyone is afraid to cross the bridge anyway. Unfortunately, four people had only one flashlight, and the bridge was narrow enough to allow two people to cross at the same time. If they cross the bridge alone, the time required for the four is 1, 2, 5, 8 minutes, and if two people cross the bridge simultaneously, the time required is the time for the slower person to move alone. The question is how to design a plan to get the four people to cross the bridge as quickly as possible.
Second, the answer to the question
The problem itself is not too difficult, even with simple enumeration methods to find the correct answer.
After two people cross the bridge, need to send a flashlight, so the most easy to think of is to let the fastest person to send a flashlight to the person. Therefore, this first method is: First let a B past (2 minutes), a back (1 minutes), a past (5 minutes), a back (1 minutes), a ding and then the past (8 minutes), a total of 17 minutes to allow four people to pass.
And the correct answer is the second method: first let a B past (2 minutes), a back (1 minutes), the past (8 minutes), B back (2 minutes), a B again (2 minutes), a total of 15 minutes will be able to let four people over.
One of the key points here is to get two of the slowest people to cross the bridge at the same time.
three, simple extension
If the time required for four people to change the difference, it is 1, 4, 5, 8 minutes.
The first method: a B in the past (4 minutes), a back (1 minutes), a past (5 minutes), a back (1 minutes), a ding and then the past (8 minutes), a total of 19 minutes to allow four people to pass.
The second method: first let a B past (4 minutes), a back (1 minutes), the past (8 minutes), B back (4 minutes), a B again (4 minutes), a total of 21 minutes to allow four people to pass.
This time, the two slowest people were in the past, but even slower.
We compare the differences between the two scenarios: the second fastest person should not also pass a flashlight.
Suppose that four people crossing the river time is T1,t2,t3,t4 and T1
The total time for the first method of crossing the river is: T2+T1+T3+T1+T4
The total time for the second crossing method is: T2+t1+t4+t2+t2
The difference between the two is: (T1+T3) -2t2.
The conclusion is: if (T1+T3) is greater than 2t2, the second method is superior, if (T1+T3) is less than 2t2, the first method is excellent, if (t1+t3) equals 2t2, there is no difference between the two methods.
Iv. Promotion of issues
Now we promote this problem: if there are N (n greater than or equal to 4) travelers, suppose they have their own required bridge time is fast and slow, different. In the case of only one flashlight, how can you find the quickest way to cross a bridge?
Now we assume that the time for the n individual to cross the bridge is T1,t2,t3,......, Tn, and meet t1
After analysis, to meet the fastest bridge, the reasonable arrangement includes the following points:
(1) Let the fastest flashlight to send as many times as possible.
(2) In some cases, the flashlight may also be sent by the second-fastest torch.
(3) Keep the number of slow crossings as low as possible;
(4) The fastest two first cross the bridge to ensure that the two people can send the flashlight back and forth;
We use the above conclusions to analyze the multi-person situation gradually.
When N=5, the first T1, T2 two people cross the bridge, T1 to send back the torch, did not cross the bridge has become T1, T3, T4, T5 4 people situation. This time, need to compare the size of T1+t4 and 2t3?
The first option, or choose T1 to send the torch, the total time of the bridge: T2+T3+T1+T4+T1+T5
The second scenario, let the slow go together, but because the flashlight is not T3, but faster T2, the total bridge time for
T2+t5+t2+t3+t1+t2.
The difference between the two schemes is t1+t4-2t2, which has nothing to do with T3.
When N=6, the first T1, T2 two people cross the bridge, T1 to send back the torch, did not cross the bridge has become T1, T3, T4, T5, T6 5 people situation. According to the analysis just now, we should compare the size of t1+t5-2t2.
And so on, the cross-bridge problem of many people, the difference between the two schemes, is only related to the time of the fastest person, the second-fastest person and the second-slowest person crossing the bridge, but not with the speed of others.
At this point, the best solution for many people crossing the river and its total time, it is easy to solve.
Exercise 1: In the dark of the night, a total of six travelers came to a narrow and no guardrail of the bridge side. If you don't use a flashlight, everyone is afraid to cross the bridge anyway. Unfortunately, six people had only one flashlight, and the bridge was narrow enough to allow two people to cross at the same time. If they cross the bridge alone, the time required for six people is 1, 5,6,7,8, 9 minutes respectively, and if two people cross the bridge at the same time, it is time to walk the slower person alone. The question is how to design a scheme to get the six people to cross the bridge as quickly as possible.
V. Imitation exercises
Exercise 2: In the Dark of the night, a total of seven travelers came to a narrow and no guardrail of the bridge side. If you don't use a flashlight, everyone is afraid to cross the bridge anyway. Unfortunately, seven people had only one flashlight, and the bridge was narrow enough to allow two people to cross at the same time. If they cross the bridge alone, the time required for 74 people is 5, 8,9,10, 11, 12, 13 minutes, and if two people cross the bridge simultaneously, the time required is the time to walk the slower person alone. The question is how to design a scheme to get the 76 people to cross the bridge as quickly as possible.
Cross-river problem (greedy algorithm)