描述
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.
输入
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).
输出
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’.
样例输入
3 5 4
样例输出
6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)
Notes
这算是一道bfs搜索题吧,我们把A,B容器里面的水定义为一个状态,由(0,0)开始通过不同的操作,得到不同的状态。然后依次放到队列里面,直到找到满足题目要求的状态,期间需要记录已经到达的状态,下一次再遇到的时候,就不再处理了, 由一个状态到另一个状态的时候需要记录它的前驱状态。这样最后的是时候才能找到最短的操作序列。
AC Code
#include<iostream>
#include<cmath>
#include<cstring>
//#include<vector>
//#include<list>
//#include<stack>
//#include<queue>
//#include<map>
//#include<set>
//#include<cstdio>
//#include<cstdlib>
//#include<algorithm>
using namespace std;
const int MAXN = 1e4 + 10;
int front=-1, rear=-1, ans=-1;
int A, B, C;
bool visited[MAXN][MAXN];
struct Node{
int a, b; //表示水量
int step; //走了几步
int type; //通过哪种方式得到这一步
int prior; //从哪一步过来的
}q[MAXN];
void bfs(){
Node f={0, 0, 0, 0, -1};
q[++rear]=f;
visited[0][0]=1;
while(rear!=front){
Node cur=q[++front], next;
for(int i=1; i<=6; ++i){
switch(i){
case 1: //fill(1)
next.a=A; next.b=cur.b; next.type=1;
break;
case 2: //fill(2)
next.a=cur.a; next.b=B; next.type=2;
break;
case 3: //drop(1)
next.a=0; next.b=cur.b; next.type=3;
break;
case 4: //drop(2)
next.a=cur.a; next.b=0; next.type=4;
break;
case 5: //pour(1,2)
if(cur.a > B-cur.b){
next.a=cur.a-(B-cur.b);
next.b=B;
} else {
next.a=0;
next.b=cur.a+cur.b;
}
next.type=5;
break;
case 6: //pour(2,1)
if(cur.b > A-cur.a){
next.a=A;
next.b=cur.b-(A-cur.a);
} else {
next.a=cur.a+cur.b;
next.b=0;
}
next.type=6;
break;
}
if(visited[next.a][next.b]) continue;
visited[next.a][next.b]=1;
next.step=cur.step+1;
next.prior=front;
q[++rear]=next;
if(next.a==C || next.b==C) {
ans=rear;
return;
}
}
}
}
int operation[MAXN];
void print(int ans){
int p=q[ans].prior, s=q[ans].step;
printf("%d\n", s);
operation[s--]=q[ans].type;
while(p!=-1){
operation[s--]=q[p].type;
p=q[p].prior;
}
for(int i=1; i<=q[ans].step; ++i){
switch(operation[i]){
case 1: printf("FILL(1)\n"); break;
case 2: printf("FILL(2)\n"); break;
case 3: printf("DROP(1)\n"); break;
case 4: printf("DROP(1)\n"); break;
case 5: printf("POUR(1,2)\n"); break;
case 6: printf("POUR(2,1)\n"); break;
}
}
}
int main(){
//freopen("C:\\Users\\Ambition\\Desktop\\in.txt","r",stdin);
memset(visited, 0, sizeof(visited));
scanf("%d %d %d", &A, &B, &C);
bfs();
if(ans==-1) printf("impossible\n");
else print(ans);
return 0;
}