Pots POJ 3414

/*
*POJ 3414
*简单模板bfs
*编程应该为了方便理解,尽量提供接口
*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=1e2+10;
int VA,VB,VC;
bool inq[maxn][maxn];
vector<int>vec;
struct Node{
	int a,b,step;//a,b的值,以及步数 
	vector<int>ope;
	Node(){}
	Node(int _a,int _b,int _s){
		a=_a;
		b=_b;
		step=_s;
	}
};
void fill(int index,int &A,int &B){
	if(index==1) A=VA;
	else if(index==2) B=VB;
	else printf("Illegal operation!\n");
}
void drop(int index,int &A,int &B){
	if(index==1) A=0;
	else if(index==2) B=0;
	else printf("Illegal operation!\n");
}
void pour(int in1,int in2,int &A,int &B){
	if(in1==1&&in2==2){
		if(A+B>=VB){
			A=A+B-VB;
			B=VB;
		}
		else{
			B=A+B;
			A=0;
		}
	}
	else if(in1==2&&in2==1){
		if(A+B>VA){
			B=A+B-VA;
			A=VA;
		}
		else{
			A=A+B;
			B=0;
		}
	}
}
Node BFS(){
	memset(inq,false,sizeof(inq));
	queue<Node>que;
	que.push(Node(0,0,0));
	inq[0][0]=true;
	while(!que.empty()){
		Node now=que.front();
		que.pop();
		int a=now.a,b=now.b,step=now.step;
		if(a==VC||b==VC) return now;
		for(int i=0;i<6;i++){
			int tempa=a,tempb=b;
			if(i==0) fill(1,tempa,tempb);
			else if(i==1) fill(2,tempa,tempb);
			else if(i==2) drop(1,tempa,tempb);
			else if(i==3) drop(2,tempa,tempb);
			else if(i==4) pour(1,2,tempa,tempb);
			else if(i==5) pour(2,1,tempa,tempb);			
			if(inq[tempa][tempb]==false){
				Node next=Node(tempa,tempb,step+1);
				next.ope=now.ope;
				next.ope.push_back(i);
				que.push(next);
				inq[tempa][tempb]=true;
			}
		}
		
	}
	return Node(-1,-1,-1);
}
int main(){
	scanf("%d%d%d",&VA,&VB,&VC);
	Node ans=BFS();
	if(ans.a==-1) printf("impossible\n");
	else{
		printf("%d\n",ans.step);
		for(int i=0;i<ans.ope.size();i++){
			if(ans.ope[i]==0) printf("FILL(1)\n");
			else if(ans.ope[i]==1) printf("FILL(2)\n");
			else if(ans.ope[i]==2) printf("DROP(1)\n");
			else if(ans.ope[i]==3) printf("DROP(2)\n");
			else if(ans.ope[i]==4) printf("POUR(1,2)\n");
			else if(ans.ope[i]==5) printf("POUR(2,1)\n");
			
		}
	}
		
	return 0;
}
posted @ 2019-07-18 08:31  waaaafool  阅读(333)  评论(0编辑  收藏  举报