# (图的DFS遍历)分酒问题

9 0 0 0

0

6 0 0 3

-1

7 2 0 0

2

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

String last = input.nextLine();

Set<String> init = new HashSet<String>();
Set<String> hist = new HashSet<String>();
int r = bfs(hist,init,last);
System.out.println(r);
}

//hist 记录历史状态
//init 现在这一层所有状态
public static int bfs(Set<String> hist,Set<String> init,String goStr){
//这一层有我想要的结果
if(hist.contains(goStr))return 0;

//下一层的倒酒姿势
Set<String> form = new HashSet<String>();

for (Iterator<String> iterator = init.iterator(); iterator.hasNext();) {
String next = iterator.next();
Set<String> newForm = move(next);
}

//消除重复倒酒姿势
form.removeAll(hist);

//如果消除重复倒酒姿势后大小 为 0 说明没有可以用的姿势了 -1 代表失败的意思，找不到结果
if(form.isEmpty())return -1;

//载入历史倒酒姿势

//递归调用 bfs 扩散开树的下一层开始宽度优先遍历
int r = bfs(hist,form,goStr);

//出现失败情况
if(r<0)return -1;

//否则 倒酒次数加1 返回回去

return 1+r;
}

public static Set<String> move(String init){
int[] cap = {9,7,4,2};
int[] data = new int[4];
String[] ss = init.split(" ");

Set<String> set = new HashSet<String>();
for(int i=0;i<ss.length;i++){
data[i] = Integer.parseInt(ss[i]);
}

for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(i==j)continue;
//A酒杯没酒了
if(data[i]==0)continue;
//B酒杯满了
if(data[j]==cap[j])continue;

//A 全倒进 B 的总酒量
int sumWine = data[i] + data[j];

//待会即将被改变的状态
int[] temp = Arrays.copyOf(data,data.length);

//B酒杯装完全部A
if(sumWine<=cap[j]){

temp[i] =0;
temp[j] = sumWine;

}else{
//B酒杯不可以装满全部A
temp[i] = sumWine - cap[j];
temp[j] = cap[j];
}

String res = "";
for(int k=0;k<4;k++){
res += " "+temp[k];
}
}

}
return set;

}

}