Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 14034 | Accepted: 5909 | Special Judge |
Description
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)
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
//这里char没有string好用
string op[7]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(2,1)","POUR(1,2)"};
int l,r;//队列的头尾
int a,b,c;
int vis[200][200],step[50000];//标记数组,操作数组
struct node
{
int x;//第一个杯子里的水量
int y;//第二个杯子里的水量
int opter;//操作编号
int pre;//上一步的下标
}xin[100000];
void solve(int x,int y,int opter)
{
if(vis[x][y])
return ;
vis[x][y]=1;//改变此种情况时的状态
xin[r].x=x;
xin[r].y=y;
xin[r].opter=opter;//记录操作编号
xin[r].pre=l;//记录上一步的下标
r++;//进队列
}
int print()
{
int ans=0;
while(l!=0)
{
step[ans++]=xin[l].opter;//记录操作编号
l=xin[l].pre;//递归思想
}
printf("%d\n",ans);//操作数
for(int i=ans-1;i>=0;i--)
{
cout<<op[step[i]]<<endl;//操作
}
return 0;
}
int bfs()
{
memset(vis,0,sizeof(vis));
xin[0].x=0;//初始化两个杯子都是空的状态
xin[0].y=0;
vis[0][0]=1;
l=0;
r=1;//进队列
int tx;//临时水量
int ty;
while(l!=r)
{
if(xin[l].x==c||xin[l].y==c)//结束的条件,恰好量出需要的水量
{
print();
break;
}
tx=a;
ty=xin[l].y;
solve(tx,ty,1);//第一种情况,倒满第一杯
tx=xin[l].x;
ty=b;
solve(tx,ty,2);//第二种情况,倒满第二杯
tx=0;
ty=xin[l].y;
solve(tx,ty,3);//第三种情况,清空第一杯
tx=xin[l].x;
ty=0;
solve(tx,ty,4);//第四种情况,清空第二杯
tx=xin[l].x+min(a-xin[l].x,xin[l].y);
ty=xin[l].y-min(a-xin[l].x,xin[l].y);
solve(tx,ty,5);//第五种情况,把第二杯倒入第一杯
tx=xin[l].x-min(b-xin[l].y,xin[l].x);
ty=xin[l].y+min(b-xin[l].y,xin[l].x);
solve(tx,ty,6);//第六种情况,把第一杯倒入第二杯
l++;
}
if(l>=r)//不能恰好量出需要的水量
printf("impossible\n");
return 0;
}
int main()
{
while(~scanf("%d",&a))
{
scanf("%d",&b);
scanf("%d",&c);
bfs();
}
return 0;
}