传道士野人过河

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;
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值