3

I'm trying to solve a bridge and torch like problem with dynamic programming. More about this problem can be found on wikipedia (http://en.wikipedia.org/wiki/Bridge_and_torch_problem). The story goes like this:

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 one minute, B in two minutes, C in five minutes, and D in eight 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 in 15 minutes or less?

Now I've managed to solve the problem using a graph, but I don't see how I can solve this type of problem using dynamic programming. How do you split up the problem in to subproblems? And how do the solutions of the subproblems lead to the optimal solution of the whole problem? What are the stages and states?

Does somebody know how to solve this using DP? And maybe tell me how to solve this puzzle with Java?

Lesso
  • 31
  • 1
  • 2
  • Maybe [this](http://doc.gold.ac.uk/~mas01sd/classes/jpf_release/test/gov/nasa/jpf/mc/Crossing.java) will help you. – Alexey Odintsov Aug 20 '14 at 07:59
  • Or this http://stackoverflow.com/questions/1144207/bridge-crossing-puzzle – Revive Aug 20 '14 at 08:00
  • Thank you for the replies, already checked out the links, but I don't yet see what the DP approach is. What are the subproblems, stages and states... – Lesso Aug 20 '14 at 08:21
  • We need state `dp[2^4][2]`. `dp[i][j]` means the minimum time to achieve the state that every person's position(this side or that side) and the position of torch denoted by `i` and `j` from initial stage. For example, dp[7][0] means the minimum time to achieve the (7 is 0111(2), that means, `ABC` on that side and `0` means the torch is on this side) stage from initial stage. By this, we calculate `dp[15][1]`(`ABCD` on that side and torch is on that side, too.) then we get the answer. – Sayakiss Sep 02 '14 at 08:51

1 Answers1

0

I have written a code in python for the torch and bridge puzzle, you can use the same logic in other languages.

src=[1,2,5,8]

src=sorted(src)


def minimum_time(src):
    
    count=0
    if len(src)==3:
        count=sum(src)
    else:
        count=src[1]*(len(src)-2)+src[0]*(len(src)//3)
        for i in range(len(src)-1,-1,-2):
            #print(src[i])
            count+=src[i]
    return(count)

print(minimum_time(src))
Faizal
  • 1