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)
题目链接:http://poj.org/problem?id=3414
解法类型:BFS
解题思路:广度搜索,优先队列,分别处理每种情况。用一个标记数组,防止重复的情况再次搜索。最后就是步骤的打印,利用递归思想很容易解决的。
算法实现:
STATUS:C++_AC_0MS_220K
#include<stdio.h>
#include<memory.h>
const int MAXN=110;
int BFS();
void print(int fa);
int vis[MAXN][MAXN],q[MAXN*MAXN*MAXN][5];
int A,B,C,fa;
int main()
{
// freopen("in.txt","r",stdin);
int ok;
while(scanf("%d%d%d",&A,&B,&C)!=EOF)
{
memset(vis,0,sizeof(vis));
ok=BFS();
if(ok){
printf("%d\n",q[fa][4]);print(fa);
}
else printf("impossible\n");
}
return 0;
}
int BFS()
{
int front=0,rear=0,a,b,na,nb,nfront,tot;
q[rear][0]=0,q[rear][1]=0,q[rear][2]=0,q[rear++][4]=0; //q[?][0|1|2|3|4|5]分别储存A中的水,B中的水,操作,父节点,距离.
vis[0][0]=1;
while(front<rear) //队列非空
{
a=q[front][0],b=q[front][1],tot=q[front][4]; //出队列
nfront=front,front++;
if(a==C||b==C){//search succeed
fa=nfront;
return 1;
}
na=A,nb=b; //FILL(1)
if(!vis[na][nb]){
vis[na][nb]=1;
q[rear][0]=na,q[rear][1]=nb;
q[rear][2]=1,q[rear][3]=nfront;
q[rear++][4]=tot+1;
}
na=a,nb=B; //FILL(2)
if(!vis[na][nb]){
vis[na][nb]=1;
q[rear][0]=na,q[rear][1]=nb;
q[rear][2]=2,q[rear][3]=nfront;
q[rear++][4]=tot+1;
}
na=0,nb=b; //DROP(1)
if(!vis[na][nb]){
vis[na][nb]=1;
q[rear][0]=na,q[rear][1]=nb;
q[rear][2]=3,q[rear][3]=nfront;
q[rear++][4]=tot+1;
}
na=a,nb=0; //DROP(2)
if(!vis[na][nb]){
vis[na][nb]=1;
q[rear][0]=na,q[rear][1]=nb;
q[rear][2]=4,q[rear][3]=nfront;
q[rear++][4]=tot+1;
}
int is_full; //POUR(1,2)
is_full=(a+b)/B;
is_full?(na=a+b-B,nb=B):(na=0,nb=a+b);
if(!vis[na][nb]){
vis[na][nb]=1;
q[rear][0]=na,q[rear][1]=nb;
q[rear][2]=5,q[rear][3]=nfront;
q[rear++][4]=tot+1;
}
is_full=(a+b)/A; //POUR(2,1)
is_full?(na=A,nb=a+b-A):(na=a+b,nb=0);
if(!vis[na][nb]){
vis[na][nb]=1;
q[rear][0]=na,q[rear][1]=nb;
q[rear][2]=6,q[rear][3]=nfront;
q[rear++][4]=tot+1;
}
}
return 0; //search fail
}
void print(int fa) //递归打印步骤
{
if(fa==0)return;
print(q[fa][3]);
if(q[fa][2]<=2)printf("FILL(%d)\n",q[fa][2]); //下面分别打印
else if(q[fa][2]<=4)printf("DROP(%d)\n",q[fa][2]/2);
else {
if(q[fa][2]==5)printf("POUR(1,2)\n");
else printf("POUR(2,1)\n");
}
return;
}