Jugs(回溯法)

ZOJ Problem Set - 1005
Jugs

Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

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

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.

Output

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

思路:用二元组(a,b)记录每一次操作后A和B的状态(gallon数),一个状态元组不能重复出现,不然会不断循环。

AC Code:
  1 #include <iostream>
  2 #include <map>
  3 #include <cstdio>
  4 
  5 using namespace std;
  6 
  7 const int SZ = 10000;
  8 int op[SZ];
  9 //op is for "operation". 1:fill A,2:fill B,3:empty A,4:empty B,5:pour A B,6:pour B A,0:success
 10 int ca, cb, n;
 11 map<pair<int, int>, bool> tag;
 12 
 13 
 14 bool DFS(int a, int b, int k)  //a和b分别是A和B的gallon数,k是op的下标
 15 {
 16     pair<int, int> p = make_pair(a, b);
 17     if(tag[p]) return false;
 18     tag[p] = true;
 19     if(b == n)
 20     {
 21         op[k] = 0;
 22         return true;
 23     }
 24     if(a < ca) //fill A
 25     {
 26         op[k] = 1;
 27         if(DFS(ca, b, k + 1)) return true;
 28     }
 29     if(b < cb)  //fill B
 30     {
 31         op[k] = 2;
 32         if(DFS(a, cb, k + 1)) return true;
 33     }
 34     if(a)  //empty A
 35     {
 36         op[k] = 3;
 37         if(DFS(0, b, k + 1)) return true;
 38     }
 39     if(b)  //empty B
 40     {
 41         op[k] = 4;
 42         if(DFS(a, 0, k + 1)) return true;
 43     }
 44     if(a && b < cb)  //pour A B
 45     {
 46         op[k] = 5;
 47         int ra, rb;
 48         if(a > cb - b)
 49         {
 50             rb = cb;
 51             ra = a - (cb - b);
 52         }
 53         else
 54         {
 55             ra = 0;
 56             rb = b + a;
 57         }
 58         if(DFS(ra, rb, k + 1)) return true;
 59     }
 60     if(b && a < ca)  //pour B A
 61     {
 62         op[k] = 6;
 63         int ra, rb;
 64         if(b > ca - a)
 65         {
 66             ra = ca;
 67             rb = b - (ca - a);
 68         }
 69         else
 70         {
 71             rb = 0;
 72             ra = b + a;
 73         }
 74         if(DFS(ra, rb, k + 1)) return true;
 75     }
 76     return false;
 77 }
 78 
 79 int main()
 80 {
 81     while(scanf("%d %d %d", &ca, &cb, &n) != EOF)
 82     {
 83         tag.clear();
 84         bool flag = DFS(0, 0, 0);
 85         for(int i = 0; op[i]; i++)
 86         {
 87             switch(op[i])
 88             {
 89             case 1:
 90                 puts("fill A");
 91                 break;
 92             case 2:
 93                 puts("fill B");
 94                 break;
 95             case 3:
 96                 puts("empty A");
 97                 break;
 98             case 4:
 99                 puts("empty B");
100                 break;
101             case 5:
102                 puts("pour A B");
103                 break;
104             case 6:
105                 puts("pour B A");
106                 break;
107             default:
108                 break;
109             }
110         }
111         puts("success");
112     }
113     return 0;
114 }
 
 

原文地址:https://www.cnblogs.com/cszlg/p/3213508.html