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.
Input
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.
Output
For each set of input data, output the minimum number of steps required to obtain c litres, or 1 if this is impossible.
Example
2 5 2 3 2 3 4
Sample output:
2 1
hide comments
sherlock11:
20180516 10:21:37
bfs will work...........but carefully examine when answer will be "1"........otherwise enjoy "TLE" 

itzsowvik:
20180508 17:47:12
Isn't this problem is too much hard for beginners?


nadino:
20180430 10:42:18
https://gist.github.com/psayre23/c30a821239f4818b0709


gauravgb21:
20180425 17:06:32
solved it using DP! 

chetan4060:
20171228 12:38:51
adhoc problem


vengatesh15:
20171221 11:08:34
easy BFS 

chachaji_harsh:
20171204 17:35:45
how is it a DP problem 

mahilewets:
20170829 20:18:26
I think BFS is bad here


arthur1991:
20170326 07:38:10
i love qianqian 

osama2003:
20170125 00:31:58
1 .. nguenthanhloc 
Added by:  adrian 
Date:  20040531 
Time limit:  1s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All except: NODEJS PERL6 VB.NET 
Resource:  An ancient problem, formulated in these words by Mr Tadeusz Ratajczak 