
1) State (‘X’, ‘Y’) represents that the first vessel has ‘X’ liters of water and the second vessel has ‘Y’ liters of water.
2) If it is impossible to measure ‘W’ amount of water using the given two vessels then return -1.
1) You can empty a vessel i.e. move from (‘X’, ‘Y’) to (0, ‘Y’) or (‘X’, 0).
2) Fill a vessel (‘X’, ‘Y’) to its maximum capacity i.e. (‘M’, ‘Y’) or ( ‘X’, ‘N’).
3) Pouring water from one vessel to another until one of the vessels is either empty or full i.emove from (‘X’, ‘Y’) to (min(‘X’ + ‘Y’, ‘M’ ), max(0, ‘X’ + ‘Y’ - ‘M’) or (max(0, ‘X’ + ‘Y’ - ‘N’), min(‘X’ + ‘Y’ , ‘N’).
The first line of input contains an integer ‘T’ which denotes the number of test cases.
The first and the only line of each test case contains three space-separated integers ‘M’, ‘N’, and ‘W’ where ‘M’ represents the capacity of water the first vessel can store, ‘N’ represents the capacity of water the first vessel can store and ‘W’ represents the amount of water you have to measure.
For each test case, return the minimum number of steps required to measure the ‘W’ amount of water.
Print the output of each test case in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 100
1 <= ‘M’, ‘N’ <= 5000
1 <= ‘W’ <= max(‘M’, ‘N’)
Time Limit: 1 sec
As we know we can perform one of the three operations at a time, so we can use the breadth-first search algorithm to find the minimum number of steps required to measure ‘W’ liters of water.
The steps are as follows: