The Generalized Jug Problem - a different question 
“If you have a ﬁve-liter
  and a three-liter bottle and plenty of water, how can you get four liters of
  water in the ﬁve-liter bottle?”

We (all) know this problem, and its natural generalization, 

“Given a
  p-liter and q-liter jug what amounts of water can be obtained?”

My question is different. I would like to know if there's a way to obtain the minimum number of operation to fill one between p and p jug with c liters. Only allowed operation are:


*

*Overfill a n-liter jug with n-liter

*Empty a jug

*Pour from a jug to another while the first one is empty or the other is full.


Thanks for your attention. 
 A: I have been thinking about this problem a lot lately. Given a $p$-liter and a $q$-liter with $p < q$, there is an algorithm involving $p$ and $q$ which outputs a $c$ for each set of operations inside of the algorithm.
For an example, consider a $4$-liter jug and a $9$-liter jug, where we need $6$ liters.


*

*Fill up the smaller jug. (In this case, the $4$-liter jug.)

*Slowly pour the smaller jug into the larger jug until either the larger jug is full or the smaller jug is empty.

*If the larger jug is full, empty it out and put the remaining contents of the smaller jug into the larger jug.

*Measure and record $c$, the number of liters in the larger jug. (In this case: $4$ liters in the $9$-liter jug.)

*Repeat until the desired $c$ is acquired.


Running this algorithm for this example outputs the following values for $c$: $4, 8, 3, 7, 2, 6, 1, 5, 0$. Those with a keen eye will see that these are the elements of $Z9$, the group of integers modulo 9.
In general, this algorithm will eventually produce all elements of $<p>$ inside $Zq$, the group of integers modulo $q$.
For the layperson (who knows about modular arithmetic), the possible amounts of water which can be obtained are $1 * p$ $mod$ $q$, $2 * p$ $mod$ $q$, $3 * p$ $mod$ $q$, ..., $q * p$ $mod$ $q$.
The number of operations required to find $c$ is always less than $4*q$. (This is because we have four steps to check each integer up to q, maximally).
A: By hand, this is most likely not possible. However, if you use a computer, you can use depth first search to look at all the possible first moves, possible second moves, possible third moves. At some point, you would have found the minimum number of operations required. The downfall to this method is that it has an exponential run-time.
