日期:2014-05-20  浏览次数:20816 次

100分求解人鬼过河flash小游戏的正确算法
求解人鬼过河flash小游戏的正确算法

或者把我写的源码改好

package game;

import java.util.*;

public class RiverGame {
//人
private final String PERSON = "1";
//鬼
private final String GHOST = "2";
//开始的岸上的人和鬼集合
private List<String> a;
//船上的人和鬼集合
private List<String> b;
//到达的岸上的人和鬼集合
private List<String> c;

public RiverGame() {
init();
}

private void init(){
a = new ArrayList<String>();
for (int i = 0; i < 3; i++) {
a.add(PERSON);
a.add(GHOST);
}
b = new ArrayList<String>();
c = new ArrayList<String>();
}

//获取某一个岸或船上人或鬼的数量
public int getCount(List<String> arg0, String arg1) {
int count = 0;
for (String s : arg0) {
if (arg1.equals(s)) {
count++;
}
}
return count;
}
//打印List的方法
private String show(List<String> s){
StringBuffer ss=new StringBuffer();
for(String s1:s){
ss.append(s1);
}
return ss.toString();
}

//从岸到船
public void transToShip(List<String> arg0, List<String> arg1,List<String> reacher, int direction) {

// 传两人
if ((getCount(arg0, PERSON) - 2 >= getCount(arg0, GHOST))&& (getCount(arg0, PERSON) >= 2) && (arg1.size() == 0)) {
for (int i = 0; i < 2; i++) {
arg0.remove(PERSON);
arg1.add(PERSON);
}
System.out.print("传两人 ");
if (b.size() >= 1) {
crossRiver(arg1, reacher, direction);
}else{
for (int i = 0; i < 2; i++) {
arg0.add(PERSON);
arg1.remove(PERSON);
}
}
}
// 传两鬼
if ((getCount(arg0, PERSON) >= getCount(arg0, GHOST))&& (getCount(arg0, GHOST) >= 2) && (arg1.size() == 0)) {
for (int i = 0; i < 2; i++) {
arg0.remove(GHOST);
arg1.add(GHOST);
}
System.out.print("传两鬼 ");
if (b.size() >= 1) {
crossRiver(arg1, reacher, direction);
}else{
for (int i = 0; i < 2; i++) {
arg0.add(GHOST);
arg1.remove(GHOST);
}
}
}
// 传一人一鬼
if ((getCount(arg0, PERSON) >= getCount(arg0, GHOST))&& (getCount(arg0, GHOST) >= 1) && (arg1.size() == 0)) {
arg0.remove(GHOST);
arg0.remove(PERSON);
arg1.add(GHOST);
arg1.add(PERSON);
System.out.print("传一人一鬼 ");
if (b.size() >= 1) {
crossRiver(arg1, reacher, direction);
}else{
arg0.add(GHOST);
arg0.add(PERSON);
arg1.remove(GHOST);
arg1.remove(PERSON);
}
}
// 传一人
if ((getCount(arg0, PERSON) - 1 >= getCount(arg0, GHOST))&& (getCount(arg0, PERSON) >= 1) && (arg1.size() <= 1)) {
arg0.remove(PERSON);
arg1.add(PERSON);
System.out.print("传一人 ");
if (b.size() >= 1) {
crossRiver(arg1, reacher, direction);
}else{
arg0.add(PERSON);
arg1.remove(PERSON);
}
}
// 传一鬼
if ((getCount(arg0, PERSON) >= getCount(arg0, GHOST))&& (getCount(arg0, GHOST) >= 1) && (arg1.size() <= 1)) {
arg0.remove(GHOST);
arg1.add(GHOST);
System.out.print("传一鬼 ");
if (b.size() >= 1) {
crossRiver(arg1, reacher, direction);
}else{
arg0.add(GHOST);
arg1.remove(GHOST);
}
}
}

//从船到岸
public void transToBank(List<String> arg0, List<String> arg1,List<String> reacher, int direction) {

// 传两人
if (arg1.size() <= 4 && (getCount(arg0, PERSON) == 2)&& (arg0.size() == 2)) {
for (int i = 0; i < 2; i++) {