Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 15785 | Accepted: 6661 | 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)
Source
题目意思:
解题思路:
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 110
struct Node
{
int a,b;//分别表示两个瓶子
int step;//步数
int op;//操作
Node *former;//前一个操作
} q[MAXN*MAXN];
int A,B,C;//输入的原始值
int vis[MAXN][MAXN];//vis[A][B]=true,表示a瓶装A升且b瓶装B升的状态是否访问过
bool flag=false;//能否出现要求的水量
void OutPut(Node *temp)//输出操作
{
int s[MAXN*MAXN],cnt=0;
while(temp->former!=NULL)
{
s[cnt++]=temp->op;//存储操作的序号
temp=temp->former;//指向下一个操作
}
for(int i=cnt-1; i>=0; --i)
{
switch(s[i])
{
case 1:
cout<<"FILL(1)"<<endl;
break;
case 2:
cout<<"FILL(2)"<<endl;
break;
case 3:
cout<<"DROP(1)"<<endl;
break;
case 4:
cout<<"DROP(2)"<<endl;
break;
case 5:
cout<<"POUR(1,2)"<<endl;
break;
case 6:
cout<<"POUR(2,1)"<<endl;
break;
}
}
return;
}
void BFS()
{
memset(vis,false,sizeof(vis));
vis[0][0]=true;
q[0].a=q[0].b=q[0].step=q[0].op=0;
q[0].former = NULL;
int head=0,tail=1;
while(head<tail)
{
Node *temp =&q[head++];
int a=temp->a;
int b=temp->b;
int step=temp->step;
if(a==C||b==C)
{
flag=true;
cout<<step<<endl;
OutPut(temp);
return;
}
for(int i=0; i<6; ++i)
{
if(i==0)//FILL(1)
{
if(a<A&&!vis[A][b])
{
vis[A][b]=true;
q[tail].a=A;
q[tail].b=b;
q[tail].step=step+1;
q[tail].former=temp;
q[tail++].op=1;
}
}
else if(i==1)//FILL(2)
{
if(b<B&&!vis[a][B])
{
vis[a][B]=true;
q[tail].a=a;
q[tail].b=B;
q[tail].step=step+1;
q[tail].former=temp;
q[tail++].op=2;
}
}
else if(i==2)//DROP(1)
{
if(a>0&&!vis[0][b])
{
vis[0][b]=true;
q[tail].a=0;
q[tail].b=b;
q[tail].step=step+1;
q[tail].former=temp;
q[tail++].op=3;
}
}
else if(i==3)//DROP(2)
{
if(b>0&&!vis[a][0])
{
vis[a][B]=true;
q[tail].a=a;
q[tail].b=0;
q[tail].step=step+1;
q[tail].former=temp;
q[tail++].op=4;
}
}
else if(i==4)//POUR(1,2)
{
if(a>B-b&&b<B&&!vis[a-(B-b)][B])//the pot 2 is full (and there may be some water left in the pot 1)
{
vis[a-(B-b)][B]=true;
q[tail].a=a-(B-b);
q[tail].b=B;
q[tail].step=step+1;
q[tail].former=temp;
q[tail++].op=5;
}
else if(a>0&&a+b<B&&!vis[0][a+b])//the pot 1 is empty (and all its contents have been moved to the pot 2)
{
vis[0][a+b]=true;
q[tail].a=0;
q[tail].b=a+b;
q[tail].step=step+1;
q[tail].former=temp;
q[tail++].op=5;
}
}
else if(i==5)//POUR(2,1)
{
if(b>A-a&&a<A&&!vis[A][b-(A-a)])//the pot 1 is full (and there may be some water left in the pot 2)
{
vis[A][b-(A-a)]=true;
q[tail].a=A;
q[tail].b=b-(A-a);
q[tail].step=step+1;
q[tail].former=temp;
q[tail++].op=6;
}
else if(b>0&&a+b<A&&!vis[a+b][0])//the pot 2 is empty (and all its contents have been moved to the pot 1)
{
vis[a+b][0]=true;
q[tail].a=a+b;
q[tail].b=0;
q[tail].step=step+1;
q[tail].former=temp;
q[tail++].op=6;
}
}
}
}
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("F:/cb/read.txt","r",stdin);
//freopen("F:/cb/out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cin>>A>>B>>C;
BFS();
if(!flag) puts("impossible");
return 0;
}
/*
3 5 4
*/