這個(gè)是偶寫(xiě)的 你可以參考下 寫(xiě)的有點(diǎn)多 你自己優(yōu)化下吧 之前還不知道農(nóng)夫過(guò)河是啥意思 不過(guò)后來(lái)知道了 如果有問(wèn)題的話可以馬上說(shuō)的 你的50分偶要定咯??!(可以直接運(yùn)行)
創(chuàng)新互聯(lián)從2013年成立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元青浦做網(wǎng)站,已為上家服務(wù),為青浦各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
import java.util.Iterator;
import java.util.LinkedList;
public class AcrossTheRiver {
// 定義三個(gè)String對(duì)象
public static final String rabbitName = "Rabbit";
public static final String wolfName = "Wolf";
public static final String cabbageName = "Cabbage";
// 判斷兩個(gè)貨物之間關(guān)系是否友好 寫(xiě)的麻煩了一點(diǎn)= =..
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;
}
}
// 我就直接寫(xiě)在主方法里了
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("全部移動(dòng)完畢!");
} 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() + " 留了下來(lái)");
}
if (isFriendly(beforeCro[0], beforeCro[1])) {
if (afterCrossing.isEmpty()) {
afterCrossing.addLast(goods1);
System.out.println(goods1.getGoodsName() + " 被成功的放到了對(duì)岸");
} else {
Goods goods2 = afterCrossing.getFirst();
if (isFriendly(goods1, goods2)) {
afterCrossing.addLast(goods1);
System.out.println(goods1.getGoodsName() + " 被成功的放到了對(duì)岸");
} else {
beforeCrossing.addLast(goods2);
afterCrossing.removeFirst();
System.out.println(goods1.getGoodsName() + " 與 "
+ goods2.getGoodsName() + "并不和睦 于是把 " + goods2.getGoodsName()
+ "帶了回來(lái) 并將 " + goods1.getGoodsName() + " 留了下來(lái)");
}
}
} else {
beforeCrossing.addLast(goods1);
System.out.println("很可惜 留下來(lái)的兩個(gè)東西并不和睦 于是 " + goods1.getGoodsName()
+ " 又被放了回去");
}
}
}
}
}
// 貨物類(lèi)
class Goods {
// 貨物名稱(chēng)
private String goodsName;
// 默認(rèn)構(gòu)造方法
public Goods(String goodsName) {
this.goodsName = goodsName;
}
// 獲得貨物名稱(chēng)
public String getGoodsName() {
return goodsName;
}
}
//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;
}
}
#include?。t;stdio.h>#define MAX 100typedef enum BOOL{ FALSE?。健?, TRUE?。健??。鼴OOL;typedef union Items{struct {char boy?。骸?;char girl?。骸?;char father : 1;char mother?。骸?;char police?。骸?;char thief?。骸?;char?。骸?;};char c;}*pItems, Items;struct{Items item[MAX];int boat[MAX];int length;}stack; int Boat[2];Items ItemMask[12];char* msg[4]?。健。。uot;comes back single."?。uot;comes back together.",?。uot;pasts the river single.",?。uot;past the river together." };char* msgn[12]?。健。。uot;father",?。uot;mother",?。uot;police", "thief",?。uot;police and the thief",?。uot;police and the father","police and the mother", "police and the boy",?。uot;police and the girl",?。uot;father and the boy",?。uot;mother and the girl", "father and the mother"};BOOL IsLegal?。ātems item?。鸌tems t1, t2, t3;Items t4, t5, t6;t1.c?。健?, t2.c?。健?, t3.c?。健?;t4.c?。健?, t5.c = 0, t6.c?。健?;t1.girl?。健?, t1.father?。健?, t1.mother = 0;t4.girl?。健?, t4.father = 1, t4.mother?。健?;t2.boy?。健?, t2.father?。健?, t2.mother = 1;t5.boy?。健?, t5.father?。健?, t5.mother?。健?;t3.thief = 1, t3.police = 0;t6.thief?。健?, t6.police?。健?;if?。ǎā4.c & item.c?。。剑健1.c?。。鹯eturn FALSE;?。齣f (( t5.c?。mp; item.c?。。剑健2.c?。。鹯eturn FALSE;}if?。ǎǎātem.c?。mp; t6.c?。。剑健3.c?。。mp;&?。ǎātem.c?。蕖3.c?。。。健?)) {return FALSE;} return TRUE;}BOOL IsInStack?。ātems item, int boat?。鹖nt i?。健?;for?。嘞氯模荆?/p>
開(kāi)三個(gè)線程,一個(gè)代表狼,一個(gè)代表羊,一個(gè)代表白菜。
一艘船。兩個(gè)位置。
河有兩邊,
狼跟羊互斥,羊跟白菜互斥。
即他們不能在船不在此岸邊的時(shí)候同時(shí)存在。
狼,羊,白菜的線程去搶船的位置。(船在此岸)(2個(gè)位置,去搶吧,搶到了就占個(gè)座。。。。)
再開(kāi)一個(gè)線程。。。。OYE~
船判斷能不能離岸,不能離就泄空。能就到對(duì)岸就把這兩個(gè)位置上的泄到對(duì)岸,船也到對(duì)岸。
然后狼,羊,白菜的線程繼續(xù)去搶船的位置。
船線程繼續(xù)判能不能離岸。(船上的位置剩余0或1或2時(shí)只要2岸不出現(xiàn)互斥,都可以離岸)
能就走,不能就泄空。。。。
如此往復(fù)
直到有一天。。。3個(gè)都到對(duì)岸了。。OK了。。。
誰(shuí)會(huì)要求寫(xiě)出這樣沒(méi)有邏輯的純靠運(yùn)氣的程序啊。。。
現(xiàn)在的學(xué)校真操蛋。。。。
本文題目:主仆過(guò)河問(wèn)題Java代碼,主仆過(guò)河問(wèn)題java代碼是什么
本文鏈接:http://muchs.cn/article40/hchoeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、ChatGPT、網(wǎng)站收錄、App設(shè)計(jì)、服務(wù)器托管、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)