题目
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
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)
题意,两个杯子里有水a,b,有六种操作:将a中到满水,b中倒满水,a倒空,b倒空,a到bb到a直到一个杯子满或者空,达到任意一个水杯到达c。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<cstdio>
#include<vector>
#include<set>
#include<cstring>
#include<cstdlib>
#include <time.h>
#include<stack>
using namespace std;
const int maxn=1e3+10;
int a,b,c,vis[maxn][maxn];
struct node
{
int a,b,st,f;
node(int aa=0,int bb=0,int s=0,int ff=0){
a=aa;
b=bb;
st=s;
f=ff;
}
};
node pre[maxn][maxn];
string s[6]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
void output(int ta,int tb){
if(ta==0&&tb==0)
return;
node tmp=pre[ta][tb];
output(tmp.a,tmp.b);
printf("%s\n",s[tmp.f].c_str());
}
void BFS(){
queue<node>qu;
qu.push(node(0,0));
vis[0][0]=1;
while(!qu.empty()){
node now=qu.front();
qu.pop();
if(now.a==c||now.b==c){
printf("%d\n",now.st);
output(now.a,now.b);
return;
}
int ta,tb;
if(a-now.a>0){
ta=a,tb=now.b;
if(vis[ta][tb]==0){
vis[ta][tb]=1;
qu.push(node(ta,tb,now.st+1,0));
pre[ta][tb]=now;
pre[ta][tb].f=0;
}
}
if(b-now.b>0){
ta=now.a,tb=b;
if(vis[ta][tb]==0){
vis[ta][tb]=1;
qu.push(node(ta,tb,now.st+1,1));
pre[ta][tb]=now;
pre[ta][tb].f=1;
}
}
if(now.a!=0){
ta=0,tb=now.b;
if(vis[ta][tb]==0){
vis[ta][tb]=1;
qu.push(node(ta,tb,now.st+1,2));
pre[ta][tb]=now;
pre[ta][tb].f=2;
}
}
if(now.b!=0){
ta=now.a,tb=0;
if(vis[ta][tb]==0){
vis[ta][tb]=1;
qu.push(node(ta,tb,now.st+1,3));
pre[ta][tb]=now;
pre[ta][tb].f=3;
}
}
if(now.a!=0&&now.b<b){
if(now.a>b-now.b){
ta=now.a-(b-now.b);
tb=b;
}
else{
ta=0;
tb=now.b+now.a;
}
if(vis[ta][tb]==0){
vis[ta][tb]=1;
qu.push(node(ta,tb,now.st+1,4));
pre[ta][tb]=now;
pre[ta][tb].f=4;
}
}
if(now.b!=0&&now.a<a){
if(now.b>a-now.a){
ta=a;
tb=now.b-(a-now.a);
}
else{
ta=now.a+now.b;
tb=0;
}
if(vis[ta][tb]==0){
vis[ta][tb]=1;
qu.push(node(ta,tb,now.st+1,5));
pre[ta][tb]=now;
pre[ta][tb].f=5;
}
}
}
puts("impossible");
}
int main(){
scanf("%d%d%d",&a,&b,&c);
BFS();
system("pause");
return 0;
}