A---Bridge problem with classical algorithm

Source: Internet
Author: User

Topic:

In the dark of the night, four of travelers came to a narrow and non-guardrail side 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,8 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.

For the previous algorithm problem--bridge problem is as follows

The answer is also easy, ANI these four words is enough to describe the solution-the short-time people have to run a few more trips in order to pass the flashlight.
Set these four people called a,b,c,d, they need time is 1,2,5,8 minutes respectively.
The first step: A and B Bridge, spend 2 minutes.
Step two: A comes back and takes 1 minutes.
Step three: C and D Bridge, take 8 minutes.
Fourth step: B back, take 2 minutes.
Fifth step: A and B Bridge, spend 2 minutes.
So just spend 2+1+8+2+2=15 minutes, and then consider how to use the program to solve such problems, before writing the program has a detail to consider, such as a,b,c,d Four people need time is 1,8,9,10 minutes respectively.
programme I
The first step: A and B Bridge, spend 8 minutes.
Step two: A comes back and takes 1 minutes.
Step three: C and D Bridge, take 10 minutes.
Fourth step: B back, take 8 minutes.
Fifth step: A and B Bridge, spend 8 minutes.
It takes 8+1+10+8+8=35 minutes altogether.
Scenario Two
The first step: A and B Bridge, spend 8 minutes.
Step two: A comes back and takes 1 minutes.
Step three: A and C bridge, take 9 minutes.
Fourth step: A back, spend 1 minutes.
Fifth step: A and D bridge, take 10 minutes.
It takes 8+1+9+1+10=29 minutes altogether.
The result is a more granular solution-either the fastest one will send the slowest 2, or the fastest 2 will send the slowest 2 to the bridge. The people who are going to cross the bridge are arranged from small to large on the bridge, set to a A, B, ... Y,z. Where A and B are the fastest two, y and Z are the slowest two. Then there are two options:
programme IThe fastest one will send the slowest 2 to the bridge.
The first step: A and Z bridge, spend Z minutes.
Step two: A back, spend a minute.
Step three: A and Y bridge, spend y minutes.
Fourth step: A back, spend a minute.
The total number of these four steps is reduced by 2, which takes a + a + Y + z minutes.
Scenario TwoThe fastest 2 will send the slowest 2 to the bridge.
The first step: A and B bridge, spend B minutes.
Step two: A back, spend a minute.
Step three: Y and Z Bridge, spend Z minutes.
Fourth step: B back, spend B minutes.
The total number of these four steps is also reduced by 2, which takes A + B + B + z minutes.
This will reduce the total number of people by up to 2 each time you compare these two scenarios. Then we'll consider some boundary conditions:
There are three people across the bridge set to A,b,c (already lined up, the same as below). Should spend A + B + C minutes.
There are two people across the bridge set to a A, B. Then it must cost B minutes.
There was a man who crossed the bridge and set it to a. Definitely spend a minute.
So we only need to compare (B+B); (a+y)
int m= (2B > (a+y)) 2B: (a+y)
Total time: b+a+z+m

This problem for Qi is a bit difficult, is adding memory every day, I hope we can help!

A---Bridge problem with classical algorithm

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.