POJ - 3414 Pots
题目
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)
题解
这里考到路径记录了,我一般都喜欢用一个字符串记录下来,开在结构体里面就好。
这题用字符串疯狂WA或者RE,无奈开了数组记录。
/*************************************************************************
> Problem: POJ - 3414 Pots
> Author: ALizen_
> Mail: hhu_zyh@qq.com
> Blog: https://blog.csdn.net/Jame_19?spm=1001.2101.3001.5343
*************************************************************************/
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
#define sf(x) scanf("%d",&x)
#define pf(x) printf("%d\n",x)
#define slf(x) scanf("%lld",&x)
#define plf(x) printf("%lld\n",x)
#define MEM(a,b) memset((a),(b),sizeof(a))
#define ref(i,a,b) for(re i = a ; i >= b ; -- i)
#define fer(i,a,b) for(re i = a ; i <= b ; ++ i)
using namespace std;
typedef long long LL;
int a,b,c;
int book[2050][2050];
struct node{
int n,m,s;
int path[300];
};
node now,start,next;
void printt(node t){
cout<<t.s<<endl;
for(int i=0;i<t.s;i++){
int h=t.path[i];
if(h==1)cout<<"FILL(1)"<<endl;
if(h==2)cout<<"DROP(1)"<<endl;
if(h==3)cout<<"POUR(2,1)"<<endl;
if(h==4)cout<<"FILL(2)"<<endl;
if(h==5)cout<<"DROP(2)"<<endl;
if(h==6)cout<<"POUR(1,2)"<<endl;
}
}
void bfs(){
queue<node> q;
q.push(start);
while(q.size()){
now=q.front();
q.pop();
for(int i=1;i<=6;i++){
next=now;
if(i==1){
next.n=a;
next.m=now.m;
}
if(i==2){
next.n=0;
next.m=now.m;
}
if(i==3){
if(now.m+now.n>=a){
next.n=a;
next.m=now.m+now.n-a;
}
else{
next.n=now.m+now.n;
next.m=0;
}
}
if(i==4){
next.n=now.n;
next.m=b;
}
if(i==5){
next.n=now.n;
next.m=0;
}
if(i==6){
if(now.m+now.n>=b){
next.n=now.m+now.n-b;
next.m=b;
}
else{
next.n=0;
next.m=now.n+now.m;
}
}
next.path[now.s]=i;
next.s=now.s+1;
if(!book[next.n][next.m]){
q.push(next);
book[next.n][next.m]=1;
if(next.n==c||next.m==c){
printt(next);
return;
}
}
}
}
cout<<"impossible"<<endl;
}
int main(){
memset(book,0,sizeof(book));
cin>>a>>b>>c;
start.n=start.m=0;
start.s=0;
bfs();
return 0;
}