upsidedown

water jug

Jul 25th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.19 KB | None | 0 0
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4.  
  5. class Queue
  6. {
  7.             public:
  8.                         Queue()
  9.                         : head(NULL), tail(NULL)
  10.                         {
  11.                         }
  12.                         void enqueue(int i)
  13.                         {
  14.                                     if (head == NULL)
  15.                                                 head = tail = new Node(i, NULL);
  16.                                     else
  17.                                                 tail = tail->next = new Node(i, NULL);
  18.                         }
  19.                         int dequeue()
  20.                         {
  21.                                     Node* old = head;
  22.                                     head = head->next;
  23.                                     int i = old->i;
  24.                                     delete old;
  25.                                     return i;
  26.                         }
  27.                         int isEmpty()
  28.                         {
  29.                                     return (head == NULL);
  30.                         }
  31.                         ~Queue()
  32.                         {
  33.                                     while (!isEmpty())
  34.                                     dequeue();
  35.                         }
  36.             private:
  37.                         struct Node
  38.                         {
  39.                                     int i;
  40.                                     Node* next;
  41.  
  42.                                     Node(int iP, Node* nextP)
  43.                                     : i(iP), next(nextP)
  44.                                     {
  45.                                     }
  46.                         } *head, *tail;
  47. } iQueue;
  48.  
  49. const int MAX = 100;
  50. const int MAX_I = (MAX + 1) * (MAX + 1);
  51.  
  52. int N, M, k, n, m;
  53.  
  54. int distance[MAX_I];
  55. int prev[MAX_I];
  56.  
  57. int nmToI(int n, int m)
  58. {
  59.             return n * (M + 1) + m;
  60. }
  61.  
  62. int iToN(int i)
  63. {
  64.             return i / (M + 1);
  65. }
  66.  
  67. int iToM(int i)
  68. {
  69.             return i % (M + 1);
  70. }
  71.  
  72. void trace(int i)
  73. {
  74.             if (i > 0)
  75.                         trace(prev[i]);
  76.             cout <<"    "<<iToN(i) << "   |   " <<iToM(i) << "\n";
  77. }
  78.  
  79. void test(int n, int m, int n1, int m1)
  80. {
  81.             if (n1 < 0 || n1 > N || m1 < 0 || m1 > M)
  82.                         return;
  83.  
  84.             int i1 = nmToI(n1, m1);
  85.  
  86.             if (distance[i1] != 0)
  87.                         return;
  88.  
  89.             int i = nmToI(n, m);
  90.             distance[i1] = distance[i] + 1;
  91.             prev[i1] = i;
  92.             iQueue.enqueue(i1);
  93. }
  94.  
  95. int solve()
  96. {
  97.             n = m = 0;
  98.             distance[0] = 1;
  99.             iQueue.enqueue(0);
  100.  
  101.             while (!iQueue.isEmpty())
  102.             {
  103.                         int i = iQueue.dequeue();
  104.                         int n = iToN(i);
  105.                         int m = iToM(i);
  106.  
  107.                         if (n == k || m == k || n + m == k)
  108.                         return i;
  109.  
  110.             // empty out a jug
  111.                         test(n, m, 0, m);
  112.                         test(n, m, n, 0);
  113.  
  114.             // fill a jug
  115.                         test(n, m, N, m);
  116.                         test(n, m, n, M);
  117.  
  118.             // pour one to another until source is empty
  119.                         test(n, m, 0, n + m);
  120.                         test(n, m, n + m, 0);
  121.  
  122.             // pour one to another until destination is full
  123.                         test(n, m, n - M + m, M);
  124.                         test(n, m, N, m - N + n);
  125.             }
  126.             return -1;
  127. }
  128.  
  129. void main()
  130. {
  131.            
  132.             cout<<"Please enter the number of gallons in first jug:  ";
  133.             cin>>N;
  134.             cout<<"Please enter the number of gallons in second jug:  ";
  135.             cin>>M;
  136.             cout<<"Please enter the vol. of water to be left finally: ";
  137.             cin>>k;
  138.  
  139.             int i = solve();
  140.  
  141.             cout<<"  JUG 1  "<<"  JUG 2 \n";
  142.             cout<<"----------------\n";
  143.             if (i == -1)
  144.                         cout << 0 << "\n";
  145.             else
  146.             {
  147.                         cout << distance[i] << "\n";
  148.                         trace(i);
  149.             }
  150.             cout << -1 << "\n";
  151.             getch();
  152. }
Advertisement
Add Comment
Please, Sign In to add comment