POUR1 - Pouring water
Given two vessels, one of which can accommodate a litres of water and the other - b litres of water, determine the number of steps required to obtain exactly c litres of water in one of the vessels.
At the beginning both vessels are empty. The following operations are counted as 'steps':
- emptying a vessel,
- filling a vessel,
- pouring water from one vessel to the other, without spilling, until one of the vessels is either full or empty.
An integer t, 1<=t<=100, denoting the number of testcases, followed by t sets of input data, each consisting of three positive integers a, b, c, not larger than 40000, given in separate lines.
For each set of input data, output the minimum number of steps required to obtain c litres, or -1 if this is impossible.
2 5 2 3 2 3 4
Any strong test cases or commonly overlooked impossible cases?
The four needs to be in one cup, not split between the two.
y cant we get 4 from 2 and 3
In the following, a-b (Current volume occupied in a&b litre containers respectively)
i think the answer for a=12 b=11 and c=6 is zero.if anyone thinks otherwise,please post ur answer
can the answer be odd number?
I can't believe it that when I used BFS, I got the TLE,but when I change from bfs to simulate, it's just spent 10ms,what amazing...
check out the forum guys. We have good solutions for this problem and hence a lot to learn from this problem :)Last edit: 2010-05-06 21:19:32
BFS and a bit of math to prune impossible cases will suffice :)