package com.ctl;
import java.util.ArrayList;
import java.util.List;
public class 传道士过桥 {
public static boolean flagA_B2 = false;// A-->B 有BB(-1-1) AA(1,1)两种情况
public static boolean flagB_A2 = false;// B-->A 有B (-1) ,AB(1,-1)两种情况
public static boolean b = false;
public static StringBuilder sb=new StringBuilder();
static int count = 0;
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
List<String> listA = new ArrayList<String>();
List<String> listB = new ArrayList<String>();
List<String> temp = new ArrayList<String>();
listA.add("-1");
listA.add("-1");
listA.add("-1");
listA.add("1");
listA.add("1");
listA.add("1");
while (listA.size()>0) {
Thread.sleep(100);
flagA_B2 = false;// A-->B 有BB(-1-1) AA(1,1)两种情况
flagB_A2 = false;// B-->A 有B (-1) ,AB(1,-1)两种情况
b = false;
if (mount(listA, "-1") >= 2) {
listA.remove("-1");
listA.remove("-1");
listB.add("-1");
listB.add("-1");
temp.add("-1");
temp.add("-1");
if ((add(listA) < 0 && mount(listA, "1") >= 1)
|| (add(listB) < 0 && mount(listB, "1") >= 1)) {
listB.remove("-1");
listB.remove("-1");
listA.add("-1");
listA.add("-1");
temp.clear();
flagA_B2 = true;
}
} else if (flagA_B2 || mount(listA, "-1") < 2) {
listA.remove("1");
listA.remove("1");
listB.add("1");
listB.add("1");
temp.add("-1");
temp.add("-1");
}
if (flagA_B2) {
listA.remove("1");
listA.remove("1");
listB.add("1");
listB.add("1");
temp.add("-1");
temp.add("-1");
}
count++;
int size=listA.size();
sb=new StringBuilder();
for(int i=0;i<=8-size;i++){
sb.append(" ");
}
if(count>=10)
System.out.print(count);
else
System.out.print("0"+count);
System.out.printf("%1s",listA);
System.out.print(sb.toString()+"移动");
System.out.print("\t\t"+listB);
System.out.println();
sb=null;
temp.clear();
if (listA.size() == 0)
break;
if (mount(listB, "-1") >= 1) {
listB.remove("-1");
listA.add("-1");
temp.add("-1");
if ((add(listA) < 0 && mount(listA, "1") >= 1)
|| (add(listB) < 0 && mount(listB, "1") >= 1)) {
listA.remove("-1");
listB.add("-1");
temp.clear();
flagB_A2 = true;
}
}
if (flagB_A2) {
listB.remove("-1");
listB.remove("1");
listA.add("-1");
listA.add("1");
temp.add("-1");
temp.add("1");
}
// System.out.println(listB);
count++;
size=listA.size();
sb=new StringBuilder();
for(int i=0;i<=8-size;i++){
sb.append(" ");
}
if(count>=10)
System.out.print(count);
else
System.out.print("0"+count);
System.out.printf("%1s",listA);
System.out.print(sb.toString()+"移动");
System.out.print("\t\t"+listB);
System.out.println();
sb=null;
// System.out
// .println(count + " A-->" + listA
// + "\t\t移动"+temp+"\t\t\tB:" + listB);
temp.clear();
if (listA.size() == 0)
break;
}
}
public static int add(List<String> list) {// list所有元素的和
int result = 0;
int size = list.size();
for (int i = 0; i < size; i++) {
result += Integer.parseInt((String) list.get(i));
}
return result;
}
public static int mount(List<String> list, String a) {// list中某个元素的个数
int size = list.size();
int sum = 0;
for (int i = 0; i < size; i++) {
if (list.get(i).equals(a))
++sum;
}
return sum;
}
}
传道士野人过河
最新推荐文章于 2021-02-12 11:57:44 发布