Flashlight Bridge Problem Detailed

Source: Internet
Author: User

This is a very classic question, but I read a lot of explanations are not very systematic, but by the interpretation of the inspiration, here hope to write a more rigorous system of explanation.

The title is this: in the dark of the night, four of travelers came to a narrow and no guardrail of the bridge. If you don't use a flashlight, everyone is afraid to cross the bridge anyway. Unfortunately, four people took only one flashlight, and the bridge was narrow enough to allow two people to pass at the same time. If they cross the bridge alone, the time required for the four is 1,2,5,10 minutes, and if the two people cross the bridge at the same time, it is time to take the slower person to move alone. The question is, how do you design a scheme that takes the least amount of time. (17 minutes)

This problem can actually be abstracted into a more general question: N People cross the bridge, each person needs time is t[i], the bridge rule is not changed, ask all people to bridge the shortest time.

Most of the puzzle is to write directly, choose two fastest people, two slowest people, and then choose what way to cross the bridge and then recursion. In fact, this lack of theoretical rigor. Many people will ask: Why choose these four people?

OK, let's take a rigorous analysis.

First of all, the following statements are obvious:

1. Everyone needs to cross the bridge;

2. The slowest two people will never come back after crossing the bridge (because no one will need them to help the bridge, nor will they need to send a flashlight, of course the default n>=4 to ensure the slowest and fastest non-coincident)

3. The slowest two persons cannot be two persons alone to cross the bridge; (otherwise it will violate the second, a person must come back to send a flashlight)

Well, with these three sentences, it's easy to get two inferences.

1. To consider the best time to send an n person to the past, you need to first consider how to send the slowest two people to the best time of the past. (similar to the no-effect in dynamic planning);

2. Since the slowest two people cannot cross the bridge alone, they must be helped by others. So who needs help? is obviously the quickest person to help. What's the quickest number of people? Discussion. (But it can be proved that no more than two people, because if there is a third person, any step of his help has no effect)

In this way, we already know why we have chosen these four people. Now to analyze the optimal solution.

Hypothesis, T[1]~t[n] is from small to large order. Set A=t[1],b=t[2],c=t[n-1],d=t[n].

Start the discussion now:

1. If c,d needs a person's help to cross the bridge. Then the best plan must be a to send a C to the past, a back, a d sent to the past, a back. (A must come back, because there is at least a B bar here)

T1=c+a+d+a

2. If the c,d needs two people to help the bridge. So the best solution is definitely a to send B over, a back, C and D past, B back. (b must come back, because there is at least a bar here)

T2=b+a+d+b

Therefore, it is only necessary to compare the size of T1 and T2 to determine which scheme to choose. By the formula, obviously only need to compare the size of A+c and 2*b.

Moreover, the initial state and termination state of the two schemes are the same, and the effect after selection is two fewer people on the left.

Then the problem is naturally transformed in order to N-2 personal bridge problems. Is the idea of recursion.

The boundary is analyzed below.

If there are three people a,b,c (from small to large). Obviously is to send c,a back with a, a send B. Time T=a+b+c.

If there are two of them. Time is the biggest time.

A man. Time is the man's time.

The algorithm is detailed here. code can be searched from the Internet, you can see if you can understand it?

Flashlight Bridge Problem Detailed

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.