【题目描述】
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
给你两个容器,分别能装下A和B升水,并且可以进行以下操作:
1.FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2);
2.DROP(i) 将第i个容器中的水倒完
3.POUR(i,j) 将第i个容器里的水倒入第j个容器(这次操作结束后产生两种结果,一是第j个容器倒满并且第i个容器依旧有剩余,二是第i个容器里的水全部倒入j中,第i个容器为空)
现在要求你写一个程序,来找出能使其中任何一个容器里的水恰好有C升,找出最少操作数并给出操作过程
【输入】
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).
只有一行,包含3个数A,B,C(1<=A,B<=100,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’.
第一行包含一个数表示最小操作数K
随后K行每行给出一次具体操作,如果有多个答案符合最小操作数,输出他们中的任意一种操作,如果你不能使两个容器中的任意一个满足恰好C升的话,输出“impossible”
【样例输入】
3 5 4
【样例输出】
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
题目链接:https://cn.vjudge.net/problem/POJ-3414
模拟6种情况进行bfs,用t数组记录bfs的全部过程,用*pre还原路径
代码如下(写的比较垃圾,用数组整合6种情况至少能减少100行):
#include <iostream>
#include <cstring>
#include <queue>
#include <utility>
#include <stack>
#include <algorithm>
using namespace std;
static const int MAXN=100;
int vis[MAXN+10][MAXN+10];
int a,b,c,ans;
struct Node{
pair<int,int> c;
int step;
int flag;
Node *pre;
};
queue<Node> Q;
stack<int> S;
void bfs(pair<int,int> n)
{
Node t[1000];
Q.push(Node{make_pair(0,0),0,0,NULL});
vis[n.first][n.second]=1;
int cnt=-1;
while(!Q.empty())
{
cnt++;
t[cnt]=Q.front();
Q.pop();
Node next;//fill 1
next.c.first=a;
next.c.second=t[cnt].c.second;
next.flag=1;
if(!vis[next.c.first][next.c.second])
{
vis[next.c.first][next.c.second]=1;
next.pre=&t[cnt];
next.step=t[cnt].step+1;
if(next.c.first==c || next.c.second==c)
{
cout<<next.step<<endl;
while(next.pre)
{
S.push(next.flag);
next=*next.pre;
}
while(!S.empty())
{
int op=S.top();
S.pop();
switch(op)
{
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;
}
Q.push(next);
}
next.c.first=t[cnt].c.first;//fill 2
next.c.second=b;
next.flag=2;
if(!vis[next.c.first][next.c.second])
{
vis[next.c.first][next.c.second]=1;
next.pre=&t[cnt];
next.step=t[cnt].step+1;
if(next.c.first==c || next.c.second==c)
{
cout<<next.step<<endl;
while(next.pre)
{
S.push(next.flag);
next=*next.pre;
}
while(!S.empty())
{
int op=S.top();
S.pop();
switch(op)
{
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;
}
Q.push(next);
}
next.c.first=0;//drop 1
next.c.second=t[cnt].c.second;
next.flag=3;
if(!vis[next.c.first][next.c.second])
{
vis[next.c.first][next.c.second]=1;
next.pre=&t[cnt];
next.step=t[cnt].step+1;
if(next.c.first==c || next.c.second==c)
{
cout<<next.step<<endl;
while(next.pre)
{
S.push(next.flag);
next=*next.pre;
}
while(!S.empty())
{
int op=S.top();
S.pop();
switch(op)
{
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;
}
Q.push(next);
}
next.c.first=t[cnt].c.first;//drop 2
next.c.second=0;
next.flag=4;
if(!vis[next.c.first][next.c.second])
{
vis[next.c.first][next.c.second]=1;
next.pre=&t[cnt];
next.step=t[cnt].step+1;
if(next.c.first==c || next.c.second==c)
{
cout<<next.step<<endl;
while(next.pre)
{
S.push(next.flag);
next=*next.pre;
}
while(!S.empty())
{
int op=S.top();
S.pop();
switch(op)
{
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;
}
Q.push(next);
}
next.c.first=t[cnt].c.first-min(t[cnt].c.first,b-t[cnt].c.second);//pour 1 2
next.c.second=t[cnt].c.second+min(t[cnt].c.first,b-t[cnt].c.second);
next.flag=5;
if(!vis[next.c.first][next.c.second])
{
vis[next.c.first][next.c.second]=1;
next.pre=&t[cnt];
next.step=t[cnt].step+1;
if(next.c.first==c || next.c.second==c)
{
cout<<next.step<<endl;
while(next.pre)
{
S.push(next.flag);
next=*next.pre;
}
while(!S.empty())
{
int op=S.top();
S.pop();
switch(op)
{
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;
}
Q.push(next);
}
next.c.first=t[cnt].c.first+min(t[cnt].c.second,a-t[cnt].c.first);//pour 2 1
next.c.second=t[cnt].c.second-min(t[cnt].c.second,a-t[cnt].c.first);
next.flag=6;
if(!vis[next.c.first][next.c.second])
{
vis[next.c.first][next.c.second]=1;
next.pre=&t[cnt];
next.step=t[cnt].step+1;
if(next.c.first==c || next.c.second==c)
{
cout<<next.step<<endl;
while(next.pre)
{
S.push(next.flag);
next=*next.pre;
}
while(!S.empty())
{
int op=S.top();
S.pop();
switch(op)
{
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;
}
Q.push(next);
}
}
cout<<"impossible"<<endl;
return;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0),cout.tie(0);
cin>>a>>b>>c;
bfs(make_pair(0,0));
return 0;
}