源链接:http://poj.org/problem?id=3414
题目大意就是给你两个空瓶子,它们的容量分别为a,b,然后给你三种操作Fill,drop,pour,问你是否能使其中一个瓶子的水的体积为c.如果能,输出最小步数和过程,否则输出impossible。
模拟+bfs即可。题目不难,就是代码略长,分别判断某中情况下能否进行某个操作即可。一共有六种操作:
FILL(1)--0
FILL(2)--1
POUR(1,2)--2
POUR(2,1)--3
DROP(1)--4
DROP(2)--5
有这六种情况,把它们从0编号到5,操作过程中记录下来编号,最后还原就好~
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<map>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
const double PI=acos(-1.0);
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
using namespace std;
#define ll __int64
int n,m;
int L,R,C;
#define Mod 1000000007
#define N 510
#define M 1000100
int dirx[4] = {0,0,1,-1};
int diry[4] = {1,-1,0,0};
int a,b,c;
struct Node{
int va,vb;
vector<int> op;
Node(){
op.clear();
}
};
int vis[N][N];
int flag = 0;
void printAns(Node x){ //print the procedure
int size = (int)x.op.size();
printf("%d\n",size);
for(int i=0;i<size;i++){
if(x.op[i] == 0){
printf("FILL(1)\n");
}else if(x.op[i] == 1){
printf("FILL(2)\n");
}else if(x.op[i] == 2){
printf("POUR(1,2)\n");
}else if(x.op[i] == 3){
printf("POUR(2,1)\n");
}else if(x.op[i] == 4){
printf("DROP(1)\n");
}else if(x.op[i] == 5){
printf("DROP(2)\n");
}
}
flag = 1;
return;
}
void bfs(){
queue<Node> q;
Node cur,next;
memset(vis,0,sizeof vis);
vis[0][0] = 1;
cur.va = 0;
cur.vb = 0;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
if(cur.va == c || cur.vb == c){
printAns(cur);
return;
}
if(cur.va<a){ //FILL(1)
next = cur;
next.va = a;
if(!vis[next.va][next.vb]){
vis[next.va][next.vb] = 1;
next.op.push_back(0);
if (next.va == c || next.vb == c) {
printAns(next);
return;
}
q.push(next);
}
}
if(cur.vb<b){ //FILL(2)
next = cur;
next.vb = b;
if(!vis[next.va][next.vb]){
vis[next.va][next.vb] = 1;
next.op.push_back(1);
if (next.va == c || next.vb == c) {
printAns(next);
return;
}
q.push(next);
}
}
if(cur.va !=0 && cur.vb<b){ //POUR(1,2)
next = cur;
next.va = Max(0,cur.va-(b-cur.vb));
next.vb = Min(b,cur.vb+cur.va);
if(!vis[next.va][next.vb]){
vis[next.va][next.vb] = 1;
next.op.push_back(2);
if (next.va == c || next.vb == c) {
printAns(next);
return;
}
q.push(next);
}
}
if(cur.va<a && cur.vb!=0){ //POUR(2,1)
next = cur;
next.va = Min(a,cur.va+cur.vb);
next.vb = Max(0,cur.vb-(a-cur.va));
if(!vis[next.va][next.vb]){
vis[next.va][next.vb] = 1;
next.op.push_back(3);
if (next.va == c || next.vb == c) {
printAns(next);
return;
}
q.push(next);
}
}
if(cur.va != 0){ //DROP(1)
next = cur;
next.va = 0;
if(!vis[next.va][next.vb]){
vis[next.va][next.vb] = 1;
next.op.push_back(4);
if (next.va == c || next.vb == c) {
printAns(next);
return;
}
q.push(next);
}
}
if(cur.vb != 0){ //DROP(2)
next = cur;
next.vb = 0;
if(!vis[next.va][next.vb]){
vis[next.va][next.vb] = 1;
next.op.push_back(5);
if (next.va == c || next.vb == c) {
printAns(next);
return;
}
q.push(next);
}
}
}
return;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
while(scanf("%d%d%d",&a,&b,&c)!=EOF){
bfs();
if(!flag)
printf("impossible\n");
}
return 0;
}