Daily question: bridge Problem

Source: Internet
Author: User
Daily question: bridge Problem

Problem description:

Today, I happened to see a puzzle problem in reader. I tried to solve it. It still seems very interesting. I Googled it and said it was Microsoft interview questions at night, but I looked for it, it is mentioned in the book "How to slove it. I don't know who CP is. Well, let's talk about the problem: U2 has to arrive at the concert in 17 minutes. They have to go through a bridge on the way, and four people start from the same end of the bridge. You have to help them get to another one, the sky is dark, but they have only one flashlight in their hands. Since then, at most two people can bridge the bridge, and they must hold a flashlight when crossing the bridge, so someone has to bring the flashlight. The flashlight cannot be lost. The walking speed of four people varies. The time for two people to cross the bridge must be determined by the person who is slow. The time required for A, B, C, and D is 1, 2, 5, and 10 minutes. So how to bridge the bridge within 177 minutes?

Ideas:

The first thought was to use the "greedy" strategy. Select two people who need the shortest time to bridge the bridge on one side of the bridge, and select a person with the shortest time to send back the flashlight each time on the opposite side of the bridge. However, the obtained time is indeed 19 minutes. Okay. Now, it takes a long time to think about the above "greedy" idea. Obviously, it takes 10 minutes to bridge A and D in the last step, but the speed difference between A and D is too large. If C and D are to bridge the bridge together, will it reduce the total bridge time. Follow these steps to get the following solution:

1 and 2 first bridge the bridge, it takes 2 minutes, then 1 to send the flashlight, it takes 1 minute, then 5 to 10 to bridge the bridge, it takes 10 minutes, and 2 to send the flashlight, it takes 2 minutes, and then 1 and 2 bridge the bridge together. The total time is 2 + 1 + 10 + 2 + 2 = 17 minutes.

Continue, can this problem be converted into a certain model to solve. OK. The idea is as follows: construct such a graph G. each node in G indicates the set of people who have already crossed the bridge. The edge in G represents
Time is used as the weight of a directed edge.

if graph theory is used for modeling, A directed graph can be constructed using the state of four people at both ends of the bridge as the node, as shown in. The state of the person who has crossed the bridge is used as the node of the graph. At the beginning, no one crosses the bridge, therefore, expressed as null, two people crossed the bridge in the first round and there were six possible combinations ), it takes 2, 5, 10, 5, 10 minutes to switch from an empty state to these states. The time is used as the weight of the directed edge. When two people bridge the bridge, they need to take a flashlight back to pick up other people. There are four possible situations: 1, 2, 5, 10, one of them stayed on the other side of the river, (1, 2) this state can only be converted to (1) (2) two states, the corresponding edge weight is 2, 1 minutes, (1, 2) to (1) it takes 2 minutes to return the result, and the following graph theory model can be created.


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.