1. Text problem 4.1.

2. Text problem 4.8.

3. Text problem 4.9. (Hint: there is a counterexample; think about graphs where the shortest path isn't well-defined.)

4. Text problem 4.11.

5. This is another contest problem, and it's moderately hard
(though not as hard as the longest-chain problem). Don't wait until
Halloween to get started!
You are given two hour glasses. They measure *M*
*N*
*T*
*M*
*N*

Initially (at time = 0), all of the sand is in the lower bowl in each of the hour glasses, and both hour glasses are flipped. Subsequently, one can flip one or both of the hour glasses according to the following rules.

- When only one of the hour glasses expires at a particular
instant, one has four choices of action:
1. flip the hour glass that expired;

2. flip the hour glass that did not expire;

3. flip both hour glasses;

4. do not flip either one, just let one hour glass sit idle until the other one expires.

- When both of the hour glasses happen to expire simultaneously, or if one hour glass has been sitting idle and the other one has just expired, one must flip at least one of the hour glasses.
- Any hour glass may be flipped only at an instant when either the same hour glass, or the other one, or both have just expired.

A particular time *T*
*T*

The input consists of a number of lines, each representing one
instance of the problem. Each line contains three positive integers
which represent the values of *M*
*N*
*T*
*M* < *N* <= 200
and *N* <= *T* <= 2000.
The input is terminated by a line containing three zeroes.

For each instance of the problem, print the shortest sequence of
flips which measures the target time *T*
*T*
`Impossible!`' on a single line. The output
for each instance of the problem should be followed by a line
consisting of ten hyphens.

4 17 21 4 17 22 8 13 23 0 0 0

0: 4,17 17: 4,17 ---------- 0: 4,17 4: 4 8: 4 12: 4 16: 4 17: 4 18: 4,17 ---------- 0: 8,13 8: 8 13: 8,13 18: 8,13 ----------

This is similar to the homework problem (Text Problem 3.8) where you had
to build a graph representing the contents of three containers and 11 pints
of water. Except here you have to solve the problem in the fewest number
of steps. This is a red flag that **BFS** is indicated instead of
just **DFS**.

To solve this problem, take an approach similar to what you did for Text Problem 3.8: build a graph where the root is the starting state (both hourglasses pouring sand). Then make an arc to all states that can be legally reached from there by an hourglass-flip.

It's best to build the graph *dynamically* instead of building it
in advance: just create the nodes as you run BFS. If you reach the target
time, you have to print the path in the graph that got you there. If you
pass the target time, you print "Impossible!". The best way to detect when
no solution is possible is to simply avoid enqueuing any nodes whose time
value exceeds the target time. If the queue comes up empty and you haven't
hit the target, you know there is no solution.

Hand in your code in hardcopy along with sample runs for these test cases:

4 17 21 4 17 22 8 13 23 171 177 1846 173 194 746 144 187 1186 157 198 378 33 127 1250 168 197 1487 176 187 1776 130 180 1998 177 195 694 172 188 1161 85 193 924 0 0 0

Feel free to consult with your fellow students on the answers you're getting to these test cases. Or ask Martin or me what we got for a given test case if you're feeling worried that something's wrong.