POJ3414 Pots

                                                                                      Pots

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4011   Accepted: 1689   Special Judge

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 ≤ i ≤ 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 A, B, 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 #include <cstdlib>
  2 #include <iostream>
  3 #include <string.h>
  4 using namespace std;
  5 int a,b;
  6 int A,B,C;
  7 int tag=-1;
  8 bool visited[101][101];
  9 struct Node{
 10        int i;
 11        int father;
 12        int a,b;
 13        int num;
 14        };
 15 Node queue[2000000];//队列 
 16 int front,rear;
 17 
 18 int fill_1()
 19 {if(a==A)
 20  return 0;
 21  else
 22  {a=A;
 23   if(visited[a][b])
 24   return 0;
 25   visited[a][b]=true;
 26  return 1;
 27  }
 28  
 29 }
 30 
 31 int fill_2()
 32 {if(b==B)
 33  return 0;
 34  else 
 35  {b=B;
 36  if(visited[a][b])
 37   return 0;
 38   visited[a][b]=true;
 39   return 1;
 40   }
 41 }
 42 
 43 int drop_1()
 44 {if(a==0)
 45 return 0;
 46 else
 47 {a=0;
 48  if(visited[a][b])
 49   return 0;
 50   visited[a][b]=true;
 51   return 1;
 52  }
 53 }
 54 
 55 int drop_2()
 56 {if(b==0)
 57 return 0;
 58 else
 59 {b=0;
 60 if(visited[a][b])
 61   return 0;
 62   visited[a][b]=true;
 63   return 1;}
 64 }
 65 
 66 int pour_1to2()
 67 {if(a==0)
 68  return 0;
 69  else
 70  {if(a<(B-b))
 71  {b+=a;a=0;}
 72  else
 73  {a-=(B-b);b=B;}
 74  if(visited[a][b])
 75   return 0;
 76   visited[a][b]=true;
 77  return 1;
 78  }
 79 }
 80 
 81 int pour_2to1()
 82 { if(b==0)
 83  return 0;
 84  else
 85  {
 86      if(b<(A-a))
 87      {a+=b;b=0;}
 88      else
 89      {b-=(A-a);a=A;}
 90      if(visited[a][b])
 91      return 0;
 92      visited[a][b]=true;
 93      return 1;
 94  }
 95 }
 96 
 97 int (*p[6])()={fill_1,fill_2,drop_1,drop_2,pour_1to2,pour_2to1};
 98 
 99      
100        
101 int bfs()
102 {
103     
104     front=rear=0;
105     int i,k,j;
106     
107     Node node;
108     for(i=0;i<6;i++)
109     {a=b=0;
110      if(p[i]())
111      {queue[rear].i=i;
112      queue[rear].father=-1;
113      queue[rear].a=a;
114      queue[rear].b=b;
115      queue[rear].num=1;
116      rear++;
117      
118      if(a==C||b==C)
119      {tag=rear-1;return 1;}
120     }
121      
122      
123     }
124     
125     while(front<rear)
126     {node.i=queue[front].i;
127      node.a=queue[front].a;
128      node.b=queue[front].b;
129      node.num=queue[front].num;
130      for(i=0;i<6;i++)
131      {a=node.a;b=node.b;
132      if(p[i]())
133      {queue[rear].i=i;
134      queue[rear].father=front;
135      queue[rear].a=a;
136      queue[rear].b=b;
137      queue[rear].num=node.num+1;
138      rear++;
139      
140      if(a==C||b==C)
141      {tag=rear-1;return node.num+1;}
142      }
143      
144      }//for
145      front++;
146                      
147                      
148                      
149     }//while
150     return 0;
151     
152     
153 }
154 void output(int i)
155 {
156     if( queue[i].father==-1)
157      {switch(queue[i].i)
158      {case 0:
159            cout<<"FILL(1)"<<endl;
160            break;
161      case 1:cout<<"FILL(2)"<<endl;
162            break;
163      case 2:cout<<"DROP(1)"<<endl;
164            break;
165      case 3:cout<<"DROP(2)"<<endl;
166            break;
167      case 4:cout<<"POUR(1,2)"<<endl;
168            break;
169      case 5:cout<<"POUR(2,1)"<<endl;
170            break;
171      }
172      return ;
173     }
174     output(queue[i].father);
175     switch(queue[i].i)
176      {case 0:
177            cout<<"FILL(1)"<<endl;
178            break;
179      case 1:cout<<"FILL(2)"<<endl;
180            break;
181      case 2:cout<<"DROP(1)"<<endl;
182            break;
183      case 3:cout<<"DROP(2)"<<endl;
184            break;
185      case 4:cout<<"POUR(1,2)"<<endl;
186            break;
187      case 5:cout<<"POUR(2,1)"<<endl;
188            break;
189      }
190 }
191     
192 
193       
194 
195 int main(int argc, char *argv[])
196 {cin>>A>>B>>C;
197 int t;
198 
199  
200 
201  memset(visited,false,sizeof(visited));
202  t=bfs();
203  
204  if(t==0)
205  cout<<"impossible"<<endl;
206  else
207  {cout<<t<<endl;
208   output(tag);
209  }
210  
211     system("PAUSE");
212     return EXIT_SUCCESS;
213 }
posted @ 2012-05-31 10:01  cseriscser  阅读(440)  评论(0编辑  收藏  举报