Java实现传教士与野人过河问题

本文介绍了使用Java解决传教士与野人过河问题的方法。通过定义问题状态、算符和递归函数,找到所有安全的渡河方案。代码展示了如何通过不同的渡河操作,确保在任何情况下,传教士不会被野人攻击。

1  问题定义

河的两岸有三个传教士和三个野人需要过河,目前只有一条能装下两个人的船,在河的任何一方或者船上,如果野人的人数大于传教士的人数,那么传教士就会被野人攻击,怎么找出一种安全的渡河方案呢?

2  算法分析

首先,先来看看问题的初始状态和目标状态,定义河的两岸分别为左岸和右岸,设定状态集合为(左岸传教士人数,右岸野人数,右岸传教士人数,右岸野人数,船的位置),船的位置:-1表示船在左岸,1表示船在右岸。

初始状态:(3,3,0,0,0,-1)

目标状态:(0,0,3,3,1)

然后,整个问题就抽象成了怎样从初始状态经中间的一系列状态达到目标状态。问题状态的改变是通过划船渡河来引发的,所以合理的渡河操作就成了通常所说的算符,根据题目要求,可以得出以下5个算符(按照渡船方向的不同,也可以理解为10个算符):

渡1野人、渡1传教士、渡1野人1传教士、渡2野人、渡2传教士

根据船的位置,向左移或向右移通过递归依次执行5种算符,判断是否找到所求,并排除不符合实际的状态,就可以找到所有可能的解,如图1所示为递归函数流程图。


3  源代码


package joepa.javaweb;


import java.util.ArrayList;
import java.util.List;


public class River {


public static void main(String[] args) {
List<RiverSides> lastParameters = new ArrayList<RiverSides>();
List<String> operation = new ArrayList<String>();


RiverSides currentState = new RiverSides(3, 3, 0, 0, 1);


lastParameters.add(currentState);


cvsWdfs(currentState, lastParameters, operation, 0);


lastParameters.remove(lastParameters.size() - 1);


System.out.println("执行完毕");


}


private static int mycount = 0;// 统计成功过河次数


public static int cvsWdfs(RiverSides lastState,
List<RiverSides> lastParameters, List<String> operation,
int ifboacurrentStatety) {


if (lastState.getChurchR() == 3 && lastState.getWildR() == 3) {
mycount++;
System.out.println("第" + mycount + "次成功过河");
System.out.println("传教士 野人 |  移动方向");
for (int i = 0; i < operation.size(); i++) {
System.out.println(operation.get(i));
}
return 0;
}
// 判断过河操作否重复,去除死循环
for (int i = 0; i < lastParameters.size() - 1; i++) {
if (lastParameters.get(i).getWildL() == lastState.getWildL()
&& lastParameters.get(i).getChurchL() == lastState
.getChurchL()) {


if (lastState.getBoat() == lastParameters.get(i).getBoat())
return 0;
}
}
// 检验人数数据合法性
if (lastState.getChurchL() < 0 || lastState.getWildL() < 0
|| lastState.getChurchR() < 0 || lastState.getWildR() < 0)
return 0;
// 传教士是否被吃
if ((lastState.getChurchL() < lastState.getWildL() && lastState .getChurchL() != 0)
|| (lastState.getChurchR() < lastState.getWildR() && lastState.getChurchR() != 0))
return 0;
// 递归执行五类过河操作,boat=-1船在左岸,boat=1船在右岸,传入boat为上一次船位置
// 下次应当取反
RiverSides currentState = new RiverSides();


// 两个传教士过河
if (lastState.getBoat() == 1)
operation.add("  2     0   | 左岸->右岸");
else
operation.add("  2     0   | 右岸->左岸");
currentState.setChurchL(lastState.getChurchL() - 2
* lastState.getBoat());
currentState.setWildL(lastState.getWildL());
currentState.setChurchR(lastState.getChurchR() + 2
* lastState.getBoat());
currentState.setWildR(lastState.getWildR());
currentState.setBoat(-lastState.getBoat());
lastParameters.add(currentState);
cvsWdfs(currentState, lastParameters, operation, 0);
operation.remove(operation.size() - 1);
lastParameters.remove(lastParameters.size() - 1);


// 两个野人过河
if (lastState.getBoat() == 1)
operation.add("  0     2   | 左岸->右岸");
else
operation.add("  0     2   | 右岸->左岸");
currentState.setChurchL(lastState.getChurchL());
currentState.setWildL(lastState.getWildL() - 2 * lastState.getBoat());
currentState.setChurchR(lastState.getChurchR());
currentState.setWildR(lastState.getWildR() + 2 * lastState.getBoat());
currentState.setBoat(-lastState.getBoat());
lastParameters.add(currentState);
cvsWdfs(currentState, lastParameters, operation, 0);
operation.remove(operation.size() - 1);
lastParameters.remove(lastParameters.size() - 1);


// 一个野人,一个传教士
if (lastState.getBoat() == 1)
operation.add("  1     1   | 左岸->右岸");
else
operation.add("  1     1   | 右岸->左岸");
currentState.setChurchL(lastState.getChurchL() - 1
* lastState.getBoat());
currentState.setWildL(lastState.getWildL() - 1 * lastState.getBoat());
currentState.setChurchR(lastState.getChurchR() + 1
* lastState.getBoat());
currentState.setWildR(lastState.getWildR() + 1 * lastState.getBoat());
currentState.setBoat(-lastState.getBoat());
lastParameters.add(currentState);
cvsWdfs(currentState, lastParameters, operation, 0);
operation.remove(operation.size() - 1);
lastParameters.remove(lastParameters.size() - 1);


// 一个传教士过河
if (lastState.getBoat() == 1)
operation.add("  1     0   | 左岸->右岸");
else
operation.add("  1     0   | 右岸->左岸");
currentState.setChurchL(lastState.getChurchL() - 1
* lastState.getBoat());
currentState.setWildL(lastState.getWildL());
currentState.setChurchR(lastState.getChurchR() + 1
* lastState.getBoat());
currentState.setWildR(lastState.getWildR());
currentState.setBoat(-lastState.getBoat());
lastParameters.add(currentState);
cvsWdfs(currentState, lastParameters, operation, 0);
operation.remove(operation.size() - 1);
lastParameters.remove(lastParameters.size() - 1);


// 一个野人过河
if (lastState.getBoat() == 1)
operation.add("  0     1   | 左岸->右岸");
else
operation.add("  0     1   | 右岸->左岸");
currentState.setChurchL(lastState.getChurchL());
currentState.setWildL(lastState.getWildL() - 1 * lastState.getBoat());
currentState.setChurchR(lastState.getChurchR());
currentState.setWildR(lastState.getWildR() + 1 * lastState.getBoat());
currentState.setBoat(-lastState.getBoat());
lastParameters.add(currentState);
cvsWdfs(currentState, lastParameters, operation, 0);
operation.remove(operation.size() - 1);
lastParameters.remove(lastParameters.size() - 1);
return 0;
}


}



//定义过河状态类
class RiverSides {
private int churchL;
private int wildL;
private int churchR;
private int wildR;
private int boat;

public RiverSides() {
}


public int getChurchL() {
return churchL;
}


public void setChurchL(int churchL) {
this.churchL = churchL;
}


public int getWildL() {
return wildL;
}


public void setWildL(int wildL) {
this.wildL = wildL;
}


public int getChurchR() {
return churchR;
}


public void setChurchR(int churchR) {
this.churchR = churchR;
}


public int getWildR() {
return wildR;
}


public void setWildR(int wildR) {
this.wildR = wildR;
}


public int getBoat() {
return boat;
}


public void setBoat(int boat) {
this.boat = boat;
}


public RiverSides(int churchL, int wildL, int churchR, int wildR, int boat) {
super();
this.churchL = churchL;
this.wildL = wildL;
this.churchR = churchR;
this.wildR = wildR;
this.boat = boat;
}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值