Pots
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 29637 | Accepted: 12366 | Special Judge |
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)
就是有两个罐子,你可以进行六种操作,来使得罐子里的水量进行变化,问你能否通过某一种变换来得到给定体积的水在任意一个罐子里。其实比较好想,bfs枚举就行,主要是拿这道题练一下 使用vector+递归 输出路径
因为代码里一个地方写错了而在规定时间里少AC了一道题,今天又拿出来补上了,纪念一下
#include<cstdio>
#include<string>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf=0x3f3f3f3f;
const int mm=111;
struct node{
int x;
int y;
int step;
int fa;
int op;
node(int a,int b,int c,int d,int e):x(a),y(b),step(c),fa(d),op(e){}
};
vector<node>path;
void print(int x){
switch(x){
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;
}
}
void show(int x){
if(x==-1)
return ;
show(path[x].fa);
print(path[x].op);
}
int book[mm][mm];//标记两个罐子中的情况是否出现过
int a,b,c;
int bfs(){
queue<node>q;
q.push(node(0,0,0,-1,0));
book[0][0]=1;
while(!q.empty()){
node pre=q.front();
q.pop();
if(pre.x==c||pre.y==c){
cout<<pre.step<<endl;
//cout<<path.size()<<endl;
show(pre.fa);
print(pre.op);
return 1;
}
path.push_back(pre);
for(int i=1;i<=6;i++){
node now=pre;
switch(i){
case 1:
now.x=a;//fill 1
break;
case 2:
now.y=b;//fill 2
break;
case 3:
now.x=0;//drop 1
break;
case 4:
now.y=0;//drop 2
break;
case 5: //pour 1,2
if(now.x<=b-now.y){
now.y+=now.x;
now.x=0;
}
else {
now.x-=(b-now.y);
now.y=b;
}
break;
case 6: //pour 2,1
if(now.y<=a-now.x){
now.x+=now.y;
now.y=0;
}
else {
now.y-=(a-now.x);
now.x=a;
}
break;
}
if(!book[now.x][now.y]){
book[now.x][now.y]=1;
now.step=pre.step+1;
now.fa=path.size()-1;
now.op=i; //存操作
q.push(now);
}
}
}
return -1;
}
int main()
{
scanf("%d%d%d",&a,&b,&c);
int res=bfs();
if(res==-1)
printf("impossible\n");
return 0;
}