zoukankan      html  css  js  c++  java
  • 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!
    ");
    }
    void drop(int index,int &A,int &B){
    	if(index==1) A=0;
    	else if(index==2) B=0;
    	else printf("Illegal operation!
    ");
    }
    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
    ");
    	else{
    		printf("%d
    ",ans.step);
    		for(int i=0;i<ans.ope.size();i++){
    			if(ans.ope[i]==0) printf("FILL(1)
    ");
    			else if(ans.ope[i]==1) printf("FILL(2)
    ");
    			else if(ans.ope[i]==2) printf("DROP(1)
    ");
    			else if(ans.ope[i]==3) printf("DROP(2)
    ");
    			else if(ans.ope[i]==4) printf("POUR(1,2)
    ");
    			else if(ans.ope[i]==5) printf("POUR(2,1)
    ");
    			
    		}
    	}
    		
    	return 0;
    }
    
  • 相关阅读:
    angular4中引入quill editor
    rocketmq client for c#
    c#扩展出MapReduce方法
    用JS触发UpdatePanel里的TextBox的change事件局部刷新失败
    水晶报表中DisplayReverseSign的设置
    (转) SQL Server Merge的用法
    关于Java8 Stream的简单实用记录
    出现 java.math.BigInteger cannot be cast to java.lang.Long解决办法
    scala与spark配置浅谈
    HBase后续随笔
  • 原文地址:https://www.cnblogs.com/waaaafool/p/11204869.html
Copyright © 2011-2022 走看看