码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3414(KB1-H BFS)

时间:2017-02-25 00:04:17      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:minimal   http   pre   string   ems   ast   set   push   search   

Pots

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

Input

On the first and only line are the numbers AB, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

Output

The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

Sample Input

3 5 4

Sample Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

Source

Northeastern Europe 2002, Western Subregion
 
  1 //2017-02-24
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <algorithm>
  6 
  7 using namespace std;
  8 
  9 struct node
 10 {
 11     int a, b, step, pre, op;
 12 }q[1000];
 13 
 14 int pots[3], POT[3], book[105][105];
 15 string option[10] = {"", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"};
 16 
 17 void fill(int i)
 18 {
 19     pots[i] = POT[i];
 20 }
 21 
 22 void drop(int i)
 23 {
 24     pots[i] = 0;
 25 }
 26 
 27 void pour(int i, int j)
 28 {
 29     if(pots[i] > POT[j]-pots[j]){
 30         pots[i] -= (POT[j]-pots[j]);
 31         pots[j] = POT[j];
 32     }else{
 33         pots[j] += pots[i];
 34         pots[i] = 0;
 35     }
 36 }
 37 
 38 void init(int a, int b)
 39 {
 40     pots[1] = a;
 41     pots[2] = b;
 42 }
 43 
 44 void push(int pos, int a, int b, int step, int pre, int op)
 45 {
 46     q[pos].a = a;
 47     q[pos].b = b;
 48     q[pos].step = step;
 49     q[pos].pre = pre;
 50     q[pos].op = op;
 51 }
 52 
 53 void print(int pos)
 54 {
 55     if(q[pos].pre == -1)return;
 56     print(q[pos].pre);
 57     cout<<option[q[pos].op]<<endl;
 58 }
 59 
 60 int min(int a, int b)
 61 {
 62     return a < b ? a : b;
 63 }
 64 
 65 int main()
 66 {
 67     int C, a, b, step;
 68     while(cin>>POT[1]>>POT[2]>>C)
 69     {
 70         int head = 0, tail = 1;
 71         q[0].a = 0;
 72         q[0].b = 0;
 73         q[0].step = 0;
 74         q[0].pre = -1;
 75         memset(book, 0, sizeof(book));
 76         book[0][0] = 1;
 77         while(head < tail)
 78         {
 79             a = q[head].a;
 80             b = q[head].b;
 81             step = q[head].step;
 82 
 83             if(a==C || b==C){
 84                 cout<<step<<endl;
 85                 print(head);
 86                 break;
 87             }
 88 
 89             init(a, b);
 90             fill(1);
 91             if(!book[pots[1]][pots[2]]){
 92                 book[pots[1]][pots[2]] = 1;
 93                 push(tail, pots[1], pots[2], step+1, head, 1);
 94                 tail++;
 95             }
 96 
 97             init(a, b);
 98             fill(2);
 99             if(!book[pots[1]][pots[2]]){
100                 book[pots[1]][pots[2]] = 1;
101                 push(tail, pots[1], pots[2], step+1, head, 2);
102                 tail++;
103             }
104 
105             if(a>0){
106                 init(a, b);
107                 drop(1);
108                 if(!book[pots[1]][pots[2]]){
109                     book[pots[1]][pots[2]] = 1;
110                     push(tail, pots[1], pots[2], step+1, head, 3);
111                     tail++;
112                 }
113             }
114 
115             if(b>0){
116                 init(a, b);
117                 drop(2);
118                 if(!book[pots[1]][pots[2]]){
119                     book[pots[1]][pots[2]] = 1;
120                     push(tail, pots[1], pots[2], step+1, head, 4);
121                     tail++;
122                 }
123             }
124 
125             init(a, b);
126             pour(1, 2);
127             if(!book[pots[1]][pots[2]]){
128                 book[pots[1]][pots[2]] = 1;
129                 push(tail, pots[1], pots[2], step+1, head, 5);
130                 tail++;
131             }
132 
133             init(a, b);
134             pour(2, 1);
135             if(!book[pots[1]][pots[2]]){
136                 book[pots[1]][pots[2]] = 1;
137                 push(tail, pots[1], pots[2], step+1, head, 6);
138                 tail++;
139             }
140 
141             head++;
142         }
143         if(head>=tail)cout<<"impossible"<<endl;
144     }
145     return 0;
146 }

 

POJ3414(KB1-H BFS)

标签:minimal   http   pre   string   ems   ast   set   push   search   

原文地址:http://www.cnblogs.com/Penn000/p/6440666.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!