poj 3414 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)

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <string.h>
  4 #include <queue>
  5 #include <string>
  6 using namespace std;
  7 int A,B,C;
  8 bool flag[101][101];
  9 char str[6][10]={"FILL(1)","FILL(2)","POUR(2,1)","POUR(1,2)","DROP(1)","DROP(2)"};
 10 struct node
 11 {
 12     int num1,num2;
 13     string a;
 14     int step;
 15 };
 16 void bfs()
 17 {
 18     node p,temp;
 19     int i;
 20     p.num1=0;
 21     p.num2=0;
 22     p.step=0;
 23     p.a="";
 24     queue<node>q;
 25     while(!q.empty())
 26     q.pop();
 27     q.push(p);
 28     while(!q.empty())
 29     {
 30         p=q.front();
 31         q.pop();
 32         if(flag[p.num1][p.num2])
 33         continue;
 34         else
 35         {
 36             flag[p.num1][p.num2]=true;
 37         }
 38         if(p.num1==C || p.num2==C)
 39         {
 40             printf("%d\n",p.step);
 41             int j;
 42             for(j=0;j<p.step;j++)
 43             {
 44                 printf("%s\n",str[(p.a[j])-'0']);
 45             }
 46             return;
 47         }
 48         for(i=0;i<4;i++)
 49         {
 50             temp=p;
 51             temp.step=p.step+1;
 52             if(i==0)
 53             {
 54                 if(temp.num1==0)
 55                 {
 56                     temp.num1=A;
 57                     temp.num2=p.num2;
 58                     temp.a=p.a+'0';
 59                     q.push(temp);
 60                 }
 61                 if(temp.num2==0)
 62                 {
 63                     temp.num2=B;
 64                     temp.num1=p.num1;
 65                     temp.a=p.a+'1';
 66                     q.push(temp);
 67                 }
 68             }
 69             if(i==1)
 70             {
 71                 if(temp.num1!=0)
 72                 {
 73                     temp.num1=0;
 74                     temp.num2=p.num2;
 75                     temp.a=p.a+'4';
 76                     q.push(temp);
 77                 }
 78                 if(temp.num2!=0)
 79                 {
 80                     temp.num2=0;
 81                     temp.num1=p.num1;
 82                     temp.a=p.a+'5';
 83                     q.push(temp);
 84                 }
 85             }
 86             if(i==2)
 87             {
 88                 if(temp.num1!=0 && temp.num2!=B)
 89                 {
 90                     int dd=B-temp.num2;
 91                     if(temp.num1>=dd)
 92                     {
 93                         temp.num2=B;
 94                         temp.num1-=dd;
 95                     }
 96                     else
 97                     {
 98                         temp.num2+=temp.num1;
 99                         temp.num1=0;
100                     }
101                     temp.a=p.a+'3';
102                     q.push(temp);
103                 }
104             }
105             if(i==3)
106             {
107                 if(temp.num2!=0 && temp.num1!=A)
108                 {
109                     int dd=A-temp.num1;
110                     if(temp.num2>=dd)
111                     {
112                         temp.num1=A;
113                         temp.num2-=dd;
114                     }
115                     else
116                     {
117                         temp.num1+=temp.num2;
118                         temp.num2=0;
119                     }
120                     temp.a=p.a+'2';
121                     q.push(temp);
122                 }
123             }
124         }
125     }
126     printf("impossible\n");
127 }
128 int main()
129 {
130     while(~scanf("%d%d%d",&A,&B,&C))
131     {
132         memset(flag,false,sizeof(flag));
133         bfs();
134     }
135     return 0;
136 }
View Code

BFS的思想;

题目大意:

给你两个空瓶子,只有三种操作

一、把一个瓶子灌满

二、把一个瓶子清空

三、把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满)

说实话,要是没有把这个题目放到bfs里面的话,真心不知道。之前直接是用模拟来实现,可是无法保证是最少操作数。

不过要是理解了bfs的思想来解这道题还是没什么压力的。

posted @ 2013-06-02 20:38  欧阳生朵  阅读(299)  评论(0编辑  收藏  举报