主仆过河问题Java代码,主仆过河问题java代码是什么

农夫过河问题(java)

这个是偶写的 你可以参考下 写的有点多 你自己优化下吧 之前还不知道农夫过河是啥意思 不过后来知道了 如果有问题的话可以马上说的 你的50分偶要定咯!!(可以直接运行)

创新互联从2013年成立,是专业互联网技术服务公司,拥有项目网站建设、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元青浦做网站,已为上家服务,为青浦各地企业和个人服务,联系电话:18982081108

import java.util.Iterator;

import java.util.LinkedList;

public class AcrossTheRiver {

// 定义三个String对象

public static final String rabbitName = "Rabbit";

public static final String wolfName = "Wolf";

public static final String cabbageName = "Cabbage";

// 判断两个货物之间关系是否友好 写的麻烦了一点= =..

public static boolean isFriendly(Goods goods1, Goods goods2) {

if (goods1 != null) {

if (goods1.getGoodsName().trim().equals(rabbitName)) {

if (goods2 == null) {

return true;

} else {

return false;

}

} else if (goods1.getGoodsName().trim().equals(wolfName)) {

if (goods2 == null || goods2.getGoodsName().trim().equals(cabbageName)) {

return true;

} else {

return false;

}

} else if (goods1.getGoodsName().trim().equals(cabbageName)) {

if (goods2 == null || goods2.getGoodsName().trim().equals(wolfName)) {

return true;

} else {

return false;

}

} else {

return false;

}

} else {

return true;

}

}

// 我就直接写在主方法里了

public static void main(String[] args) {

boolean isSuccess = false;

LinkedListGoods beforeCrossing = new LinkedListGoods();

LinkedListGoods afterCrossing = new LinkedListGoods();

beforeCrossing.add(new Goods(rabbitName));

beforeCrossing.add(new Goods(cabbageName));

beforeCrossing.add(new Goods(wolfName));

while (!isSuccess) {

Goods goods1 = beforeCrossing.getFirst();

System.out.println(goods1.getGoodsName() + " 被取走了");

beforeCrossing.removeFirst();

if (beforeCrossing.isEmpty()) {

afterCrossing.addLast(goods1);

isSuccess = true;

System.out.println("全部移动完毕!");

} else {

IteratorGoods it = beforeCrossing.iterator();

Goods[] beforeCro = new Goods[2];

for (int i = 0; it.hasNext(); i++) {

beforeCro[i] = it.next();

System.out.println(beforeCro[i].getGoodsName() + " 留了下来");

}

if (isFriendly(beforeCro[0], beforeCro[1])) {

if (afterCrossing.isEmpty()) {

afterCrossing.addLast(goods1);

System.out.println(goods1.getGoodsName() + " 被成功的放到了对岸");

} else {

Goods goods2 = afterCrossing.getFirst();

if (isFriendly(goods1, goods2)) {

afterCrossing.addLast(goods1);

System.out.println(goods1.getGoodsName() + " 被成功的放到了对岸");

} else {

beforeCrossing.addLast(goods2);

afterCrossing.removeFirst();

System.out.println(goods1.getGoodsName() + " 与 "

+ goods2.getGoodsName() + "并不和睦 于是把 " + goods2.getGoodsName()

+ "带了回来 并将 " + goods1.getGoodsName() + " 留了下来");

}

}

} else {

beforeCrossing.addLast(goods1);

System.out.println("很可惜 留下来的两个东西并不和睦 于是 " + goods1.getGoodsName()

+ " 又被放了回去");

}

}

}

}

}

// 货物类

class Goods {

// 货物名称

private String goodsName;

// 默认构造方法

public Goods(String goodsName) {

this.goodsName = goodsName;

}

// 获得货物名称

public String getGoodsName() {

return goodsName;

}

}

用java实现野人传教士过河问题

//CrossRiverQuestion.java

import java.util.ArrayList;

import java.util.List;

public class CrossRiverQuestion {

public static void main(String[] args) {

CrossRiverQuestion q = new CrossRiverQuestion(5, 4);

q.solveQuestion();

}

private int peoNum;

private int savageNum;

private ListNode resultList = new ArrayListNode();

public ListNode solveQuestion() {

Node n = new Node(peoNum,savageNum,0,0,0,new ArrayListInteger(),0,0);

boolean dfsResult = dfs(n);

if(dfsResult) {

resultList.add(0,n);

for(Node node : resultList) {

System.out.println("左岸传教士:"+node.getLeftPeo()+"左岸野人: "+node.getLeftSavage()+" 右岸传教士: "+node.getRightPeo()+"右岸野人:"+node.getRightSavage()+"船上传教士:"+node.getOnBoatPeoNum()+"船上野人:"+node.getOnBoatSavageNum());

}

return resultList;

}

return null;

}

public CrossRiverQuestion(int peoNum, int savageNum) {

super();

this.peoNum = peoNum;

this.savageNum = savageNum;

}

private boolean dfs(Node n) {

if(n.hasVisited()) return false;

n.addCheckSum();

if(n.getLeftPeo()==0n.getLeftSavage()==0) return true;

if(n.getLeftPeo()0||n.getRightPeo()0||n.getLeftSavage()0||n.getRightSavage()0) {

return false;

}

if(n.getLeftPeo()n.getLeftSavage()n.getLeftPeo()0) return false;

if(n.getRightPeo()n.getRightSavage()n.getRightPeo()0) return false;

if(n.getCURR_STATE()==n.getStateBoatLeft()) {

Node n1 = new Node(n.getLeftPeo()-1,n.getLeftSavage()-1,n.getRightPeo()+1,n.getRightSavage()+1,n.getStateBoatRight(),n.getNodesCheckSum(),1,1);

if(dfs(n1)) {

resultList.add(0,n1);

return true;

}

Node n4 = new Node(n.getLeftPeo()-2,n.getLeftSavage(),n.getRightPeo()+2,n.getRightSavage(),n.getStateBoatRight(),n.getNodesCheckSum(),2,0);

if(dfs(n4)) {

resultList.add(0,n4);

return true;

}

Node n5 = new Node(n.getLeftPeo(),n.getLeftSavage()-2,n.getRightPeo(),n.getRightSavage()+2,n.getStateBoatRight(),n.getNodesCheckSum(),0,2);

if(dfs(n5))  {

resultList.add(0,n5);

return true;

}

else {

Node n6 = new Node(n.getLeftPeo(),n.getLeftSavage()+1,n.getRightPeo(),n.getRightSavage()-1,n.getStateBoatLeft(),n.getNodesCheckSum(),0,1);

if(dfs(n6)) {

resultList.add(0,n6);

return true;

}

Node n7 = new Node(n.getLeftPeo()+1,n.getLeftSavage(),n.getRightPeo()-1,n.getRightSavage(),n.getStateBoatLeft(),n.getNodesCheckSum(),1,0);

if(dfs(n7)) {

resultList.add(0,n7);

return true;

}

Node n1 = new Node(n.getLeftPeo()+1,n.getLeftSavage()+1,n.getRightPeo()-1,n.getRightSavage()-1,n.getStateBoatLeft(),n.getNodesCheckSum(),1,1);

if(dfs(n1)) {

resultList.add(0,n1);

return true;

}

Node n4 = new Node(n.getLeftPeo()+2,n.getLeftSavage(),n.getRightPeo()-2,n.getRightSavage(),n.getStateBoatLeft(),n.getNodesCheckSum(),2,0);

if(dfs(n4)) {

resultList.add(0,n4);

return true;

}

Node n5 = new Node(n.getLeftPeo(),n.getLeftSavage()+2,n.getRightPeo(),n.getRightSavage()-2,n.getStateBoatLeft(),n.getNodesCheckSum(),0,2);

if(dfs(n5))  {

resultList.add(0,n5);

return true;

}

}

return false;

}

public ListNode getResultList() {

return resultList;

}

}

Node.java

import java.util.ArrayList;

import java.util.List;

public class Node {

private ListInteger nodesCheckSum = new ArrayListInteger();

private int leftPeo;

private int rightPeo;

private int leftSavage;

private int rightSavage;

private int CURR_STATE = 0;

private int onBoatPeoNum = 0;

private int onBoatSavageNum = 0;

private final int STATE_BOAT_LEFT = 0;

private final int STATE_BOAT_RIGHT = 1;

public Node(int leftPeo, int leftSavage, int rightPeo, int rightSavage, int state, List checkSumList, int onBoatPeoNum, int onBoatSavageNum) {

this.CURR_STATE = state;

this.leftPeo = leftPeo;

this.leftSavage = leftSavage;

this.rightPeo = rightPeo;

this.rightSavage = rightSavage;

this.nodesCheckSum.addAll(checkSumList);

this.onBoatPeoNum = onBoatPeoNum;

this.onBoatSavageNum = onBoatSavageNum;

}

public int getLeftPeo() {

return leftPeo;

}

public void setLeftPeo(int leftPeo) {

this.leftPeo = leftPeo;

}

public int getRightPeo() {

return rightPeo;

}

public void setRightPeo(int rightPeo) {

this.rightPeo = rightPeo;

}

public int getLeftSavage() {

return leftSavage;

}

public void setLeftSavage(int leftSavage) {

this.leftSavage = leftSavage;

}

public int getRightSavage() {

return rightSavage;

}

public void setRightSavage(int rightSavage) {

this.rightSavage = rightSavage;

}

@Override

public String toString() {

return leftPeo+","+leftSavage+","+rightPeo+","+rightSavage+","+CURR_STATE;

}

public int getCURR_STATE() {

return CURR_STATE;

}

public void setCURR_STATE(int cURR_STATE) {

CURR_STATE = cURR_STATE;

}

public int getStateBoatLeft() {

return STATE_BOAT_LEFT;

}

public int getStateBoatRight() {

return STATE_BOAT_RIGHT;

}

public int calcCheckSum() {

return 1*getCURR_STATE()+10*getLeftPeo()+100*getLeftSavage()+1000*getRightPeo()+10000*getRightSavage();

}

public void addCheckSum() {

int checkSum = calcCheckSum();

nodesCheckSum.add(checkSum);

}

public boolean hasVisited() {

int sum = calcCheckSum();

for (Integer checkSum : nodesCheckSum) {

if(checkSum==sum) return true;

}

return false;

}

public ListInteger getNodesCheckSum() {

return nodesCheckSum;

}

public int getOnBoatPeoNum() {

return onBoatPeoNum;

}

public void setOnBoatPeoNum(int onBoatPeoNum) {

this.onBoatPeoNum = onBoatPeoNum;

}

public int getOnBoatSavageNum() {

return onBoatSavageNum;

}

public void setOnBoatSavageNum(int onBoatSavageNum) {

this.onBoatSavageNum = onBoatSavageNum;

}

}

c程序,主仆过河问题。

#include <stdio.h>#define MAX 100typedef enum BOOL{ FALSE = 0, TRUE = 1 }BOOL;typedef union Items{struct {char boy : 1;char girl : 1;char father : 1;char mother : 1;char police : 1;char thief : 1;char : 0;};char c;}*pItems, Items;struct{Items item[MAX];int boat[MAX];int length;}stack; int Boat[2];Items ItemMask[12];char* msg[4] = { "comes back single." "comes back together.", "pasts the river single.", "past the river together." };char* msgn[12] = { "father", "mother", "police", "thief", "police and the thief", "police and the father","police and the mother", "police and the boy", "police and the girl", "father and the boy", "mother and the girl", "father and the mother"};BOOL IsLegal ( Items item ){Items t1, t2, t3;Items t4, t5, t6;t1.c = 0, t2.c = 0, t3.c = 0;t4.c = 0, t5.c = 0, t6.c = 0;t1.girl = 1, t1.father = 1, t1.mother = 0;t4.girl = 1, t4.father = 1, t4.mother = 1;t2.boy = 1, t2.father = 0, t2.mother = 1;t5.boy = 1, t5.father = 1, t5.mother = 1;t3.thief = 1, t3.police = 0;t6.thief = 1, t6.police = 1;if (( t4.c & item.c ) == t1.c ) {return FALSE; }if (( t5.c & item.c ) == t2.c ) {return FALSE;}if ((( item.c & t6.c ) == t3.c ) && (( item.c ^ t3.c ) != 0)) {return FALSE;} return TRUE;}BOOL IsInStack ( Items item, int boat ){int i = 0;for ......余下全文>>

如何用Java编写三个线程实现狼羊白菜过河问题

开三个线程,一个代表狼,一个代表羊,一个代表白菜。

一艘船。两个位置。

河有两边,

狼跟羊互斥,羊跟白菜互斥。

即他们不能在船不在此岸边的时候同时存在。

狼,羊,白菜的线程去抢船的位置。(船在此岸)(2个位置,去抢吧,抢到了就占个座。。。。)

再开一个线程。。。。OYE~

船判断能不能离岸,不能离就泄空。能就到对岸就把这两个位置上的泄到对岸,船也到对岸。

然后狼,羊,白菜的线程继续去抢船的位置。

船线程继续判能不能离岸。(船上的位置剩余0或1或2时只要2岸不出现互斥,都可以离岸)

能就走,不能就泄空。。。。

如此往复

直到有一天。。。3个都到对岸了。。OK了。。。

谁会要求写出这样没有逻辑的纯靠运气的程序啊。。。

现在的学校真操蛋。。。。


名称栏目:主仆过河问题Java代码,主仆过河问题java代码是什么
文章路径:http://scyanting.com/article/hchoeo.html