每一行表示一个操作过程中的油量状态。
递归实现
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; import java.util.Scanner; import java.util.Set; /** * * @author 秦哲 * */ public class FenJiu { // private static int w1 = 12, // w2 = 8, // w3 = 5, // v1 = 12, // v2 = 0, // v3 = 0, // end = 6;// // private static int son1v1, son1v2, son1v3, // son2v1, son2v2, son2v3, // son3v1, son3v2, son3v3, // son4v1, son4v2, son4v3, // son5v1, son5v2, son5v3, // son6v1, son6v2, son6v3; private static int w1,w2,w3,v1,v2,v3,end; private static ArrayList al; //private static HashSet hs; private static Set<String> hs = new HashSet<String>(); public static void main(String[] args) { Shuru(); al = new ArrayList(); hs = new HashSet(); //hs.add("4 3 5"); Panduan(v1, v2, v3); int n = al.size(); if(n == 0){ System.out.println("不可能"); } else{ for(int i=n-1; i>=0; i--) System.out.println(al.get(i)); } //System.out.println(hs); /*for(int i=flag; i>=0; i--){ System.out.println(al.get(i)); } if(flag == -1){ //if(al.size() == 0) System.out.println("No way!"); } else{ }*/ } public static void Shuru(){ System.out.println("请依次输入3个容器的容量,开始的状态,和要求的目标油量,用逗号隔开,回车结束:"); String str = null; try { str = new BufferedReader(new InputStreamReader(System.in)).readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Scanner sc = new Scanner(str); sc.useDelimiter(","); w1 = sc.nextInt(); //System.out.println(w1); w2 = sc.nextInt(); //System.out.println(w2); w3 = sc.nextInt(); //System.out.println(w3); v1 = sc.nextInt(); //System.out.println(v1); v2 = sc.nextInt(); //System.out.println(v2); v3 = sc.nextInt(); //System.out.println(v3); end = sc.nextInt(); //System.out.println(end); sc.close(); } public static boolean Panduan(int vv1, int vv2, int vv3) { int son1v1, son1v2, son1v3, son2v1, son2v2, son2v3, son3v1, son3v2, son3v3, son4v1, son4v2, son4v3, son5v1, son5v2, son5v3, son6v1, son6v2, son6v3; String step = vv1 + " " + vv2 + " " + vv3; //System.out.println(step); //al.add(step); if(isEnd(vv1, vv2, vv3)){ al.add(step); //flag++; return true; } if(hs.add(step)){ //1倒入2 if(vv1 > (w2-vv2)){ son1v1 = vv1 - (w2 - vv2); son1v2 = w2; son1v3 = vv3; } else{ son1v1 = 0; son1v2 = vv1 + vv2; son1v3 = vv3; } //1倒入3 if(vv1 > (w3-vv3)){ son2v1 = vv1 - (w3 - vv3); son2v2 = vv2; son2v3 = w3; } else{ son2v1 = 0; son2v2 = vv2; son2v3 = vv1 + vv3; } //2倒入1 if(vv2 > (w1-vv1)){ son3v1 = w1; son3v2 = vv2 - (w1 - vv1); son3v3 = vv3; } else{ son3v1 = vv1 + vv2; son3v2 = 0; son3v3 = vv3; } //2倒入3 if(vv2 > (w3-vv3)){ son4v1 = vv1; son4v2 = vv2 - (w3 - vv3); son4v3 = w3; } else{ son4v1 = vv1; son4v2 = 0; son4v3 = vv2 + vv3; } //3倒入1 if(vv3 > (w1-vv1)){ son5v1 = w1; son5v2 = vv2; son5v3 = vv3 - (w1 - vv1); } else{ son5v1 = vv1 + vv3; son5v2 = vv2; son5v3 = 0; } //3倒入2 if(vv3 > (w2-vv2)){ son6v1 = vv1; son6v2 = w2; son6v3 = vv3 - (w2 - vv2); } else{ son6v1 = vv1; son6v2 = vv2 + vv3; son6v3 = 0; } // System.out.println(son1v1+" "+son1v2+" "+son1v3); // System.out.println(son2v1+" "+son2v2+" "+son2v3); // System.out.println(son3v1+" "+son3v2+" "+son3v3); // System.out.println(son4v1+" "+son4v2+" "+son4v3); // System.out.println(son5v1+" "+son5v2+" "+son5v3); // System.out.println(son6v1+" "+son6v2+" "+son6v3); if(Panduan(son1v1, son1v2, son1v3) || Panduan(son2v1, son2v2, son2v3) || Panduan(son3v1, son3v2, son3v3) || Panduan(son4v1, son4v2, son4v3) || Panduan(son5v1, son5v2, son5v3) || Panduan(son6v1, son6v2, son6v3)){ al.add(step); //flag++; return true; } else{ return false; } } else{ return false; } } public static boolean isEnd(int vvv1, int vvv2, int vvv3){ if(vvv1 == end || vvv2 == end || vvv3 == end) { return true; } else { return false; } } }