Problem # 18 : Die Hard

Remember the movie "Die Hard with a Vengeance" (*ing Bruce Willis. Third in a series of Die Hard movies).

Well at one point in the movie he was given two containers one with a capacity of 5 litres and the other with 3 and an endless supply of water. To stop a bomb he needed exactly 4 litres of water.
This is what he did :-

Now your objective is to write a function DIEHARD(A B C n) which takes the following parameters :-
a) A = Capacity of can 1
b) B = Capacity of can 2
c) C = Amount required
d) n = Number of steps to do so
which will return -1 if it is possible to obtain C litres in one of the two containers in "n" steps and 0 if it is not possible to do so.

Note in the example of the movie above Bruce Willis had A=5 B=3 C=4 and took n=6 steps.

The things you can do with your containers  are :-

Also you can assume that n<=5.

Sample Output
        DIEHARD(3 2 2 1) should return -1
        DIEHARD(4 3 1 2) should return -1
        DIEHARD(4 4 1 5) should return  0
        DIEHARD(2 9 7 2) should return -1
        DIEHARD(2 9 5 5) should return -1

NOTE: I couldn't find a particularly efficient algorithm for the above program myself. (Output for some test data took upto 2 minutes!!) So if you are able to create an efficient algorithm in Qbasic or C++ or Visual Basic please e-mail it to me.

--Thanx

Previous Problem
Return to problems at The Vault
(Back to problems at The Vault )
Next Problem

LinkExchange
LinkExchange Member
1