Yes, the algorithm is always optimal. The reason is that once you've decided which jug to fill first, there's nothing you can do that makes sense at any point other than what the algorithm says.
Suppose we have just poured from A to B. Then, because we stopped pouring, it must be the case that either A is empty or B is full. (Or both are true, in which case we have achieved nothing we couldn't do just by filling up B from the beginning).
If A is empty, then pouring back from B to A immediately will just put us in the state we just came from. Either emptying or filling B will erase all of our hard work, so the only sane thing to do is to fill A from the tap, and then continue pouring from A to B.
If B is full, then pouring back from B to A immediately would still be pointless (at least from the point of view of getting a shortest solution) because we could have done that instead of pouring from A to B in the first place. Either emptying or filling A would lose our work so far, so the only thing to do is to empty out B, and then continue pouring from A to B.
Thus, in an optimal sequence every pour goes in the same direction: either always from A to B or from B to A. You can compute how long it will take to reach $c$ in each case by using the extended Euclidean algorithm (as Joffan describes) rather than actually doing the simulation one pour at a time, though.
I'm adding this answer borrowing @saulspatz suggestions If somebody like me is also struggling with these kinds of questions.
So I'll try to do step-by-step solution:
Initially we have this diagram and it can be seen that there is a constant to be added to a number $x$, thus we label to the orange shade square that variable.
From the first column it is known that the resulting sum is $x+15$
See figure from below:
$\hspace{4cm}$
Therefore in the first row we have to fill out the blank space with something so that by summed with $x$ we also get that previous $x+15$.
Since I cannot do it mentally I'll add an additional variable named $a$ as an aid for doing this momentary computation. But it can be omitted if desired.
so:
$$x+3+a=x+15$$
$$a=15-3$$
$$a=12$$
$\hspace{3cm}$
See figures 1 and figure 2.
To get the diagonal we know already that we have:
$$12+10=22$$
To get $x+15$ I'll use again that auxiliary variable to fill out the blank space in terms of $\textrm{x+something}$
$$a+22=x+15$$
$$a=x-7$$
$\hspace{2cm}$
This is indicated in figure 3 and figure 4.
We continue the process by filling up the remaining spaces:
For the second row, third column blank space I used again that auxiliary variable.
$$x-7+5+a=x+15$$
$$a=17$$
$\hspace{2cm}$
For this you can see it how it was filled in figure 5 and figure 6.
For the third row, second column the blank space is filled as follows:
$$3+x-7+a=x+15$$
$$a=19$$
$\hspace{2cm}$
This is seen in figure 7 and figure 8
Finally all that is left to do is fill out the last blank space, third row, third column:
$$12+17+a=x+15$$
$$a=x+3-17$$
$$a=x-14$$
$\hspace{2cm}$
And this is seen in figure 9 and figure 10.
At this point we can calculate the sum by equating the diagonals from both directions, from northwest to southeast with the diagonal from northeast to southwest.
See figure 11
$\hspace{5cm}$
$$x+x-7+x-14=12+10+x-7$$
$$2x-14=22$$
$$2x=36$$
$$x=18$$
Therefore by replacing in the figure we get to:
Since all the sums check:
$$18+3+12=15+18=33$$
$$5+11+17=16+17=33$$
$$10+19+4=29+4=33$$
$$18+5+10=23+10=33$$
$$3+11+19=14+19=33$$
$$12+17+4=12+21=33$$
Then both diagonals
$$18+11+4=29+4=33$$
$$12+11+10=12+21=33$$
So the number must be $18$
$\hspace{4cm}$
See figure 12 for details.
So that's it. This process as mentioned in the comments seems a good strategy when the problem is stated this way, hope it can help others as well.
Best Answer
I would approach this as a problem in finding paths through a graph.
Let the vertices of the graph be the pairs $\def\gn#1#2{\left\langle {#1}, {#2}\right\rangle}\gn ab$ where $0\le a\le 4$ and $0\le b \le 7$. Directed edges on the graph from $\gn ab$ are of three types:
We can trim the graph to omit all the nodes that are not reachable from the initial position $\gn 00$, and all the edges incident to those nodes. For example, there is no way to reach position $\gn 11$, which has 1 gallon in each bucket. (To see this, just ask what the quantities could have been just prior to the last move.)
The edges are directed, and we can do a breadth-first enumeration of the nodes reachable from the initial position $\gn00$, and delete all the "back edges", which are edges that point from a node back to a previously-visited node. These correspond to suboptimal solutions for example, suppose we know a sequence of moves $S$ that solves the problem from position $\gn 40$. Then a complete solution starting from $\gn 00$ is: fill bucket 4, then do $S$. Another complete solution starting from $\gn 00$ is: Fill bucket 7, then fill bucket 4, then empty bucket 7, then do $S$. Eliminating the "back edge" from $\gn47$ to $\gn40$ removes this solution from the graph.
We then get something like this:
The blue node $\gn 00$ at the far left is where we start. The green nodes are nodes of interest where we have measured exactly $n$ gallons into bucket 7, for various $n$. The solution you want, $\gn05$, is the rightmost green node on the bottom path. We see that there is exactly one solution with minimal moves, corresponding to the one path from the blue node $\gn00$ to this green node.
Your alternative solution through $\gn43, \gn30, \gn46, \gn42, \gn 20, \gn45$ is the other path through the graph, along the top row of nodes, and then against the arrows after reaching $\gn20$ on the far right.
If you want to find all the ways to measure 5 gallons, you can leave in the back edges. Then the graph is much more complicated:
Node $\gn 50$ is the red node in the middle of the diagram. I left in the back edges, except the ones leading back to the starting position $\gn 00$. There are now an infinite number of solutions, all of them silly variations on the unique shortest solution, which you can see snaking its way along the middle. For example, after you get to $\gn 44$ you can empty bucket 4 and refill it again as many times as you want before continuing onward to $\gn 17$. Later once you get to $\gn 41$, you can return to $\gn 40$, essentially starting over.