By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It only takes a minute to sign up. The inspiration for this code golf puzzle is the Bridge and Torch problemin which d people at the start of a bridge must all cross it in the least amount of time.

The catch is that at most two people can cross at once, otherwise the bridge will crush under their weight, and the group only has access to one torch, which must be carried to cross the bridge.

Each person in the whole puzzle has a specified time that they take to walk across the bridge. If two people cross together, the pair goes as slow as the slowest person. There is no set number of people that must cross the bridge; your solution MUST work for any value of d. You needn't use standard input for this problem, but for the sake of explaining the problem, I will be using the following input and output format for the explanation.

The first number, dis the number of people at the start of the bridge. Then, the code will scan for d numbers, each representing the speed of a person. The code output will be the LEAST amount of time required to cross everyone from the start of the bridge to the end of the bridge, while meeting the criteria explained earlier. Here are some input cases and output cases and the explanation for the first input case.

It is up to you to derive an algorithm from this information to solve the problem in the fewest bytes of code possible. Thanks to xnor for this paper. Thanks to lirtosiast save 2 bytes, movatica save 1 bytes and to gladed pointing at that my previous solution doesn't work. A solution using Theorem 1, A of this paper xnor linked. This is a solution based on the algorithm described in A of this paper on the Bridge and Torch Problem.

## Bridge and torch problem

Try it online! My solution is based on subproblem analysis. Uses the same formulation as my APL answerexcept that direct reduction turns out shorter in this case. Incidentally, this wins against all the previous entries except APL. Not great in general, but maybe not bad for a strongly-typed language.Details of the problem There are four people who wants to cross the bridge. But we don't know exactly who will cross the bridge in which time. However, we know that a person can cross the bridge in n1 minutes n1 is randomly selected from the range 1:n, n is the first input.

All crossing times are integers. They use Crossing Model to cross the bridge. In each turn, they randomly select the person s who will cross the bridge. What is the probability that they will cross the bridge less than or equal to t minutes t is the second input.

That means people will cross the bridge in 1, 2 or 3 minutes. Possibilities are listed below. If first line is the case, all of the people will cross the bridge in 1 minute. All of them will be less than or equal to 10 minutes which is input 2. If ninth line is the case, one person will cross the bridge in one minute, one person will cross the bridge in two minutes, and others will cross the bridge in 3 minutes. If last one is the case, all of them will cross the bridge in three minutes indicates that all of the journeys will take 15 minutes longer than input2 or 10 minutes.

Crossing Model: 2 out of 4 people will cross the bridge, one of them will return. Remaining two people will cross the bridge. Select every other element of a vector. Pythagorean perfect squares: find the square of the hypotenuse and the length of the other side.

Is A the inverse of B? Calculate the Hamming distance between two strings. Decrypt a secret message! Cryptography with A Square Matrix : Encoding. Fill the Matrix - 2. Digit concentration in Champernowne's constant.The second brain teaser is one of the river crossing puzzles.

Also note that with many of these problems it becomes much easier if you write down the options. An obvious first idea is that the cost of returning the torch to the people waiting to cross is an unavoidable price which should be minimized. This strategy makes A the torch bearer, shuttling each person across the bridge:.

This strategy makes it possible to do a crossing in 17 minutes. To find the correct solution, one must realize that forcing the two slowest people to cross individually wastes time which can be saved if they both cross together:.

### Subscribe to RSS

A second equivalent solution swaps the return trips. Basically, the two fastest people cross together on the 1st and 5th trips, the two slowest people cross together on the 3rd trip, and EITHER of the fastest people returns on the 2nd trip, and the other fastest person returns on the 4th trip.

Your email address will not be published. Four people come to a river in the night. There is a narrow bridge, but it can only hold two people at a time. Person A can cross the bridge in one minute, B in two minutes, C in five minutes, and D in eight minutes.

The question is, what is the fastest they can all get across? Click for solution. Tagged logicriver crossing. Bookmark the permalink. Leave a Reply Cancel reply. Action action rpg addictive android board game browser based dungeon crawl emulation exceptional fantasy Flash Flickr freeware graphic novel gtd horror indie ingenious iOS jrpg list logic mesmerizing movie multi platform open source PC plugins pretty good probability ps4 puzzle river crossing rpg sci fi security single player software stories strategy Summoner wars very good webcomic Windows wordpress.It is a dark night and there is only one torch between them.

A and B cross first using up 2 minutes. A comes back making it 3 C and D cross making it 13 minutes then B crosses back over making it 15 minutes. And finally A and B cross together to make it 17 minutes! By clicking "Sign up" you indicate that you have read and agree to the privacy policy and terms of service.

Crossing the Bridge Puzzle Bridge and torch problem. A bridge will collapse in 17 minutes. Only two people can cross at a time. Add comment. Votes Oldest. Best answer. Anand Guru Answered on 13th July Detective Expert Answered on 13th July Your Answer.

Post answer. By posting your answer, you agree to the privacy policy and terms of service. What is the logic behind these?

The pirates are all There are 10 stacks of 10 coins each. Each coin weights 10 gms. However, one stack of coins is defective Let it be simple and as direct as possible. Which clock works best? Paul, Sam and Dean are assigned the task of figuring out two numbers.

They get the following information: Both numbers Two fathers took their sons to a fruit stall. Each man and son bought an apple, But when they returnedWe shall discuss this perspective in due course. First, however, let us have a look at the puzzle itself. So here it is. It is dark, so they have to carry a torch with them whenever they cross the bridge.

That is, the crossing time of a group is equal to the crossing time of the slowest person in the group. Observe that as long as there are people on the southern bank, it is necessary to return the torch from the northern bank so that the next group can cross the bridge from south to north. It is assumed - without a formal proof - that under an optimal policy a single person, rather than a group of people, return the torch from north to south. Thus, typically groups move from south to north and individuals move back from north to south to return the torch.

The question is then: what is the optimal crossing policy, assuming that the objective is to minimize the total time required by the group to cross the bridge? In what follows we define the problem mathematically and derive a dynamic programming DP functional equation for this formulation.

An on-line interactive module is also provided for experimentation with the model. The conceptual framework we use to construct a mathematical model for the problem is sequential decision processes. That is, we regard the problem under consideration as a sequential decision problem.

By this we mean that a sequence of decisions are to be made to achieve a certain goal, subject to a variety of constraints. In addition to the decision variables, the model also includes state variablesnamely variables that are used to describe the state of the seuqential decision process. Needless to say, the value of k is not known in advance. Decision variables:. Note that it is not necessary to return the torch after the last crossing, hence there is no need for y k.

The use of X j rather than the conventional x j is intended to highlight the fact that this decision variable is a set. Given the above definitions, it follows that the dynamics of the state variables is governed by the following transition law:. The duration of the last crossing is equal to t X k as there is no need to return the torch to the southern bank. It what follows we formulate a Dynamic Programming DP functional equation for this problem.An investigation involving adding and subtracting sets of consecutive numbers.

Lots to find out, lots to explore. Main menu Search. Hide Menu. Problem Getting Started Solution Teachers' Resources You may also like Consecutive Numbers An investigation involving adding and subtracting sets of consecutive numbers. Crossing the Bridge. Four friends need to cross a bridge.

They start on the same side of the bridge. A maximum of two people can cross at any time. It is night and they have just one lamp. People that cross the bridge must carry the lamp to see the way. A pair must walk together at the rate of the slower person: Rachel: - takes 1 minute to cross Ben: - takes 2 minutes to cross George: - takes 7 minutes to cross Yvonne: - takes 10 minutes to cross The second fastest solution gets the friends across in 21 minutes.

Experiment with different speeds and work out when to use Strategy 1 and when to use Strategy 2. To support this aim, members of the NRICH team work in a wide range of capacities, including providing professional development for teachers wishing to embed rich mathematical tasks into everyday classroom practice. Register for our mailing list. University of Cambridge.

All rights reserved.The bridge and torch problem also known as The Midnight Train [1] and Dangerous crossing [2] is a logic puzzle that deals with four people, a bridge and a torch.

It is one of the category of river crossing puzzleswhere a number of objects must move across a river, with some constraints. Four people come to a river in the night. There is a narrow bridge, but it can only hold two people at a time. They have one torch and, because it's night, the torch has to be used when crossing the bridge.

Person A can cross the bridge in 1 minute, B in 2 minutes, C in 5 minutes, and D in 8 minutes. When two people cross the bridge together, they must move at the slower person's pace. The question is, can they all get across the bridge if the torch lasts only 15 minutes? An obvious first idea is that the cost of returning the torch to the people waiting to cross is an unavoidable expense which should be minimized.

This strategy makes A the torch bearer, shuttling each person across the bridge: [4]. This strategy does not permit a crossing in 15 minutes.

### Program for Bridge and Torch problem

To find the correct solution, one must realize that forcing the two slowest people to cross individually wastes time which can be saved if they both cross together: [4]. A second equivalent solution swaps the return trips. Basically, the two fastest people cross together on the 1st and 5th trips, the two slowest people cross together on the 3rd trip, and EITHER of the fastest people returns on the 2nd trip, and the other fastest person returns on the 4th trip.

Assume that a solution minimizes the total number of crossings. This gives a total of five crossings - three pair crossings and two solo-crossings. Also, assume we always choose the fastest for the solo-cross. First, we show that if the two slowest persons C and D cross separately, they accumulate a total crossing time of Here we use A because we know that using A to cross both C and D separately is the most efficient.

But, the time has elapsed and person A and B are still on the starting side of the bridge and must cross. Second, we show that in order for C and D to cross together that they need to cross on the second pair-cross: i. Remember our assumption at the beginning states that we should minimize crossings and so we have five crossings - 3 pair-crossings and 2 single crossings.

Assume that C and D cross first. But then C or D must cross back to bring the torch to the other side, and so whoever solo-crossed must cross again. Hence, they will cross separately. Also, it is impossible for them to cross together last, since this implies that one of them must have crossed previously, otherwise there would be three persons total on the start side. So, since there are only three choices for the pair-crossings and C and D cannot cross first or last, they must cross together on the second, or middle, pair-crossing.

Putting all this together, A and B must cross first, since we know C and D cannot and we are minimizing crossings. Then, A must cross next, since we assume we should choose the fastest to make the solo-cross. Then we are at the second, or middle, pair-crossing so C and D must go. Then we choose to send the fastest back, which is B.

A and B are now on the start side and must cross for the last pair-crossing. Several variations exist, with cosmetic variations such as differently named people, or variation in the crossing times or time limit.

In a variation called The Midnight Trainfor example, person D needs 10 minutes instead of 8 to cross the bridge, and persons A, B, C and D, now called the four Gabrianni brothers, have 17 minutes to catch the midnight train. The puzzle is known to have appeared as early asin the book Super Strategies For Puzzles and Games. In this version of the puzzle, A, B, C and D take 5, 10, 20, and 25 minutes, respectively, to cross, and the time limit is 60 minutes.

In the case where there are an arbitrary number of people with arbitrary crossing times, and the capacity of the bridge remains equal to two people, the problem has been completely analyzed by graph-theoretic methods.

**Bridge and Torch Problem**

## thoughts on “Bridge and torch problem”