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
0.29 sec using bfs+map and 0.00 using eea,, nice question, worth the time;
Used Unordered_map and fast I/o with BFS ,... and I couldnt get below 0.08s ... But when I just space optimized it a bit ... Got AC in 0.00s :D Hip hip hurray
@nguyenthanhloc: 5 2 7=-1
pls tell me: out put of test case:
Resource for solving in O(a+b) time :-
can 0 be any of the inputs a,b or c?
BFS tip : Use STL map instead of arrays.
A bit frustrated with this one, the solution I arrived at (C++) works for all the test cases I can find and seems to be the correct logic according to a few different posts on stackoverflow, but I keep getting WA.
AC with Diophantine equation and a bit logic :)