1997 ACM South Central USA
Programming Contest

 

Problem #7: Jugs
 
Source File: jugs.{pas,cpp,c,ss}
Input File: jugs.dat
Output File: jugs.out
Comments

In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.

You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.

A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are

fill A
fill B
empty A
empty B
pour A B
pour B A
success

where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.

You may assume that the input you are given does have a solution.

Input Format

Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.

Required Output Format

Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

Sample Input

3 5 4
5 7 3

Sample Output

fill B
pour B A
empty A
pour B A
fill B
pour B A
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success
 
Judge's Input

3 5 4
5 7 3
11 13 9
27 59 17
19 37 1
67 91 19
1 1 1

Judge's Output

fill B
pour B A
empty A
pour B A
fill B
pour B A
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success
fill A
pour A B
fill A
pour A B
fill A
pour A B
empty B
pour A B
fill A
pour A B
fill A
pour A B
empty B
pour A B
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
fill A
pour A B
empty B
pour A B
success
fill B
success

Judge's Solution

#include <stdio.h>
#include <stdlib.h>

#define min(a,b) (((a)<(b)) ? (a) : (b) )

typedef struct queueItem
{
  unsigned long element;
  struct queueItem *next;
} queueItem;

typedef struct Queue
{
  queueItem *first, *last;
} Queue;

void queueinit(Queue* q)
{
  queueItem *dummy = (queueItem*) malloc(sizeof(*dummy));
  q->first = q->last = dummy;
  dummy->next = dummy;
}

void enqueue(Queue* q, unsigned long elem)
{
  queueItem *nu = (queueItem*) malloc(sizeof(*nu));
  nu->element = elem;
  nu->next = q->last->next;
  q->last = q->last->next = nu;
}

unsigned long dequeue(Queue* q)
{
  queueItem *old = q->first->next;
  unsigned long elem = old->element;
  q->first->next = old->next;
  if (q->last == old)
    {
      q->last = q->first;
      q->first->next = q->first;
    }
  free((char*) old);
  return elem;
}

void queueelim(Queue* q)
{
  queueItem* curr = q->first->next;
  while (curr != q->first)
    {
      queueItem* old = curr;
      curr = curr->next;
      free((char*) old);
    }
  free((char*) curr);
}

#define FILL_A       0
#define FILL_B       1
#define EMPTY_A      2
#define EMPTY_B      3
#define POUR_A_B     4
#define POUR_B_A     5

#define UNVISITED (unsigned long)(-1)

unsigned long *initialize(unsigned a, unsigned b)
{
  unsigned long sz = (a+1)*(b+1);
  unsigned long* p = (unsigned long*) malloc(sz*sizeof(*p));
  while (sz--)
    p[sz] = UNVISITED;
  return p;
}

int compute_soln(unsigned a, unsigned b, unsigned n, unsigned long *parent)
{
  Queue q;
  unsigned long stat;
  if (n == 0)
    return 0;

  queueinit(&q);
  enqueue(&q,0);
  parent[0] = 0;
  while (1)
    {
      unsigned long jugStat = dequeue(&q);
      int action;
      for (action = FILL_A; action <= POUR_B_A; ++action)
        {
          unsigned long cb = jugStat % (b+1);
          unsigned long ca = jugStat / (b+1);
          switch (action)
            {
            case FILL_A:
              ca = a;
              break;
            case FILL_B:
              cb = b;
              break;
            case EMPTY_A:
              ca = 0;
              break;
            case EMPTY_B:
              cb = 0;
              break;
            case POUR_A_B:
              {
                unsigned sum = ca + cb;
                cb = min(sum,b);
                ca = sum - cb;
              } break;
            case POUR_B_A:
              {
                unsigned sum = ca + cb;
                ca = min(sum,a);
                cb = sum - ca;
              } break;
            }
          stat = ca * (b+1) + cb;
          if (parent[stat] == UNVISITED)
            {
              parent[stat] = jugStat;
              if (stat%(b+1) == n)
                {
                  queueelim(&q);
                  return stat;
                }
              enqueue(&q, stat);
            }
        }
    }
}

void print_soln(FILE *outfp, unsigned long stat,
                unsigned a, unsigned b,
                unsigned long *parent)
{
  if (stat == 0)
    return;
  print_soln(outfp, parent[stat], a, b, parent);

  {
    unsigned long jugStat = parent[stat];
    unsigned long cb = jugStat % (b+1);
    unsigned long ca = jugStat / (b+1);
    unsigned long nb = stat % (b+1);
    unsigned long na = stat / (b+1);

    if (na + nb == ca + cb)
      {
        if (na > ca)
          fprintf(outfp, "pour B A\n");
        else
          fprintf(outfp, "pour A B\n");
      }
    else if (na != ca)
      {
        if (na == 0)
          fprintf(outfp, "empty A\n");
        else
          fprintf(outfp, "fill A\n");
      }
    else if (nb != cb)
      {
        if (nb == 0)
          fprintf(outfp, "empty B\n");
        else
          fprintf(outfp, "fill B\n");
      }
  }
}

int main(void)
{
  FILE *infp = fopen("jugs.dat", "r");
  FILE *outfp= fopen("jugs.out", "w");
 
  while (!feof(infp))
    {
      unsigned long* parent;
      unsigned long stat;
      unsigned a,b,n;
      fscanf(infp, "%d %d %d\n", &a, &b, &n);
      parent = initialize(a,b);
      stat = compute_soln(a,b,n,parent);
      print_soln(outfp, stat, a, b, parent);
      fprintf(outfp, "success\n");
      free((char*) parent);
    }
  fclose(infp);
  fclose(outfp);
  return(0);
}

Comment

The solution above views the set of jug content pairs as vertices in a graph, with an edge from one pair/vertex to another if and only if one of the pouring acts gets you from one configuration to the other.  In that perspective, the algorithm above is just a breadth-first search of the graph.  A depth-first search is fine too, and perhaps simpler with recursion, but this breadth-first solution gives the shortest sequence of jug operations to achieve a result.