zoukankan      html  css  js  c++  java
  • Poj 3414 Pots

    Description

    You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

    1. FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
    2. DROP(i)      empty the pot i to the drain;
    3. 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’.


    分析:用BFS,在怎么存储被访问过的状态上想了很久,最后还是用了个List存下来了。另外,在判断条件上花了点时间,不够细心。

    package Search;
    
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Scanner;
    
    class state{
    	int a;
    	int b;
    	String str;
    	public state(int a,int b){
    		this.a=a;
    		this.b=b;
    	}
    }
    
    
    public class Poj_3414 {
    	
    	static int A,B,C;
    	static LinkedList<state> queue=new LinkedList<state>();
    	static List<String> list=new ArrayList<String>();
    	static String BFS(){
    		
    		state start=new state(0,0);
    		start.str="";
    		queue.add(start);
    		list.add(""+0+0);
    		
    		while(!queue.isEmpty()){
    			state next=queue.poll();
    			
    			state a1=new state(A,next.b);
    			a1.str=next.str+'
    '+"FILL(1)";
    			if(a1.a == C || a1.b == C){
    				return a1.str;
    			}
    			if(!list.contains(""+a1.a+a1.b)){
    				queue.add(a1);
    				list.add(""+a1.a+a1.b);
    			}
    			
    			state a2=new state(next.a,B);
    			a2.str=next.str+'
    '+"FILL(2)";
    			if(a2.a == C || a2.b == C){
    				return a2.str;
    			}
    			if(!list.contains(""+a2.a+a2.b)){
    				queue.add(a2);
    				list.add(""+a2.a+a2.b);
    			}
    			
    			state a3=new state(0,next.b);
    			a3.str=next.str+'
    '+"DROP(1)";
    			if(a3.a == C || a3.b == C){
    				return a3.str;
    			}
    			if(!list.contains(""+a3.a+a3.b)){
    				queue.add(a3);
    				list.add(""+a3.a+a3.b);
    			}
    			
    			state a4=new state(next.a,0);
    			a4.str=next.str+'
    '+"DROP(2)";
    			
    			if(a4.a == C || a4.b == C){
    				return a4.str;
    			}
    			if(!list.contains(""+a4.a+a4.b)){
    				queue.add(a4);
    				list.add(""+a4.a+a4.b);
    			}
    			state a5;
    			if(next.b >= 0 && next.a != A){
    				if(next.b >= A - next.a){
    					a5=new state(A,next.b-(A-next.a));
    				}else{
    					a5=new state(next.a+next.b,0);
    				}
    				a5.str=next.str+'
    '+"POUR(2,1)";
    				if(a5.a == C || a5.b == C){
    					return a5.str;
    				}
    				if(!list.contains(""+a5.a+a5.b)){
    					queue.add(a5);
    					list.add(""+a5.a+a5.b);
    				}
    			}
    			
    			state a6;
    			if(next.a >= 0 && next.b != B){
    				if(next.a >= B - next.b){
    					a6=new state(next.a -(B - next.b),B);
    				}else{
    					a6=new state(0,next.a+next.b);
    				}
    				a6.str=next.str+'
    '+"POUR(1,2)";
    				if(a6.a == C || a6.b == C){
    					return a6.str;
    				}
    				
    				if(!list.contains(""+a6.a+a6.b)){
    					queue.add(a6);
    					list.add(""+a6.a+a6.b);
    				}
    			}
    		}
    		return null;
    	}
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		A=sc.nextInt();
    		B=sc.nextInt();
    		C=sc.nextInt();
    		String ans=BFS();
    		if(ans != null){
    			String ss=ans.trim();
    			String s[]=ss.split("
    ");
    			System.out.println(s.length);
    			System.out.println(ss);
    		}else{
    			System.out.println("impossible");
    		}
    		
    	} 
    }
    
    


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    最近这段时间我,想在2008 的基础上,写2011 有的工具 不知道会样,这次又机会研究ploy
    Screen 可以查找屏幕pos系类的函数
    笔记1
    Ubuntu下如何安装 tar.bz2 文件
    安装ubuntu遇到“BusyBox”问题
    android luancher 如何添加快捷方式
    转 Android 源代码结构
    修改apk图标
    Linux Ubuntu 下如何安装 .SH文件
    解放你的电源键!!不用刷机不用装软件!超简单修改搜索锁屏、HOME键唤醒~~~~~
  • 原文地址:https://www.cnblogs.com/AndyDai/p/4734095.html
Copyright © 2011-2022 走看看