poj 3414 Pots(bfs+输出路径)

时间:2022-04-19 16:07:57

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)

题意:给你两个体积为a,b的空水瓶 问你能不能通过以上6种操作 装到c体积的水

思路:倒水问题+在结构体里开一个数组记录路径

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int v[],c;
bool vis[][];
string way[]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
struct node{
int a[];
int cnt;
int path[];
};
node t;
void pour(int from,int to){
int temp=t.a[from]+t.a[to];
if(temp>=v[to]) t.a[to]=v[to];
else t.a[to]=temp;
t.a[from]=temp-t.a[to];
}
void fill(int i){
t.a[i]=v[i];
}
void drop(int i){
t.a[i]=;
}
void bfs(){
queue<node> q;
t.a[]=t.a[]=t.cnt=;
q.push(t);
while(!q.empty()){
node temp=q.front();
q.pop();
if(temp.a[]==c||temp.a[]==c){
cout<<temp.cnt<<endl;
for(int i=;i<=temp.cnt;i++)
cout<<way[temp.path[i]]<<endl;
return ;
}
for(int i=;i<;i++){
if(i==){
t=temp;
fill();
t.path[t.cnt+]=;
}else if(i==){
t=temp;
fill();
t.path[t.cnt+]=;
}else if(i==){
t=temp;
drop();
t.path[t.cnt+]=;
}else if(i==){
t=temp;
drop();
t.path[t.cnt+]=;
}else if(i==){
t=temp;
pour(,);
t.path[t.cnt+]=;
}else if(i==){
t=temp;
pour(,);
t.path[t.cnt+]=;
}
if(vis[t.a[]][t.a[]]) continue;
vis[t.a[]][t.a[]]=;
t.cnt++;
q.push(t);
}
}
cout<<"impossible"<<endl;
}
int main(){
ios::sync_with_stdio(false);
while(cin>>v[]>>v[]>>c){
memset(vis,,sizeof(vis));
bfs();
}
return ;
}