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 jis 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)
思路:这题类似非常可乐,只需要模拟所有操作就可以了,代码虽然长但基本都是复制黏贴的,最后通过记录数组递归地输出路径。
坑点:刚开始数组开了105 * 105 一直Runtime Error,代码感觉找不到错误,后来数组改成150 * 150,然后过了。。懵。按理说105*105也够用了啊。总之,以后遇见RE时注意看看数组大小,在初始化时尽量把数组开大点!
#include<iostream>
#include<queue>
using namespace std;
struct node{
int a,b,step;
};
int a,b,c;
int vis[150][150];
node pre_path[150][150];
void print(int a,int b){
if(a == 0 && b == 0){
return;
}
print(pre_path[a][b].a,pre_path[a][b].b);
if(pre_path[a][b].step == 1){
cout<<"FILL(1)"<<endl;
}
if(pre_path[a][b].step == 2){
cout<<"FILL(2)"<<endl;
}
if(pre_path[a][b].step == 3){
cout<<"DROP(1)"<<endl;
}
if(pre_path[a][b].step == 4){
cout<<"DROP(2)"<<endl;
}
if(pre_path[a][b].step == 5){
cout<<"POUR(1,2)"<<endl;
}
if(pre_path[a][b].step == 6){
cout<<"POUR(2,1)"<<endl;
}
}
int bfs(){
queue<node>q;
node now,next;
now.a = 0,now.b = 0,now.step = 0;
q.push(now);
while(q.size()){
now = q.front();
q.pop();
if(now.a == c || now.b == c){
cout<<now.step<<endl;
print(now.a,now.b);
return 1;
}
node pre; //记录路径
pre.a = now.a,pre.b = now.b;
//FILL
if(now.a < a && !vis[a][now.b]){ //倒满a
next.a = a,next.b = now.b,next.step = now.step + 1;
q.push(next);
vis[next.a][next.b] = 1;
pre.step = 1; //记录操作类型
pre_path[next.a][next.b] = pre;
}
if(now.b < b && !vis[now.a][b]){ //倒满b
next.a = now.a,next.b = b,next.step = now.step + 1;
q.push(next);
vis[next.a][next.b] = 1;
pre.step = 2; //记录操作类型
pre_path[next.a][next.b] = pre;
}
//DROP
if(now.a > 0 && !vis[0][now.b]){
next.a = 0,next.b = now.b,next.step = now.step + 1;
q.push(next);
vis[next.a][next.b] = 1;
pre.step = 3;
pre_path[next.a][next.b] = pre;
}
if(now.b > 0 && !vis[now.a][0]){
next.a = now.a,next.b = 0,next.step = now.step + 1;
q.push(next);
vis[next.a][next.b] = 1;
pre.step = 4;
pre_path[next.a][next.b] = pre;
}
//POUR
//a --> b
if(now.a && now.b < b){ //a不空且b未满
pre.step = 5;
if(now.a >= b - now.b && !vis[now.a - (b - now.b)][b]){ //a可以倒满b
next.a = now.a - (b - now.b),next.b = b,next.step = now.step + 1;
q.push(next);
vis[next.a][next.b] = 1;
pre_path[next.a][next.b] = pre;
}
else if(!vis[0][now.b + now.a]){ //a不可以倒满b
next.a = 0,next.b = now.b + now.a,next.step = now.step + 1;
q.push(next);
vis[next.a][next.b] = 1;
pre_path[next.a][next.b] = pre;
}
}
//b --> a
if(now.b && now.a < a){ //b不空且a未满
pre.step = 6;
if(now.b >= a - now.a && !vis[a][now.b - (a - now.a)]){ //b可以倒满a
next.a = a,next.b = now.b - (a - now.a),next.step = now.step + 1;
q.push(next);
vis[next.a][next.b] = 1;
pre_path[next.a][next.b] = pre;
}
else if(!vis[now.a + now.b][0]){ //b不可以倒满a
next.a = now.a + now.b,next.b = 0,next.step = now.step + 1;
q.push(next);
vis[next.a][next.b] = 1;
pre_path[next.a][next.b] = pre;
}
}
}
return 0;
}
int main()
{
cin>>a>>b>>c;
int ans = bfs();
if(!ans) cout<<"impossible"<<endl;
return 0;
}