欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

主仆過河問題Java代碼,主仆過河問題java代碼是什么

農(nóng)夫過河問題(java)

這個是偶寫的 你可以參考下 寫的有點(diǎn)多 你自己優(yōu)化下吧 之前還不知道農(nóng)夫過河是啥意思 不過后來知道了 如果有問題的話可以馬上說的 你的50分偶要定咯!!(可以直接運(yùn)行)

創(chuàng)新互聯(lián)從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元青浦做網(wǎng)站,已為上家服務(wù),為青浦各地企業(yè)和個人服務(wù),聯(lián)系電話: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";

// 判斷兩個貨物之間關(guān)系是否友好 寫的麻煩了一點(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;

}

}

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

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;

// 默認(rèn)構(gòu)造方法

public Goods(String goodsName) {

this.goodsName = goodsName;

}

// 獲得貨物名稱

public String getGoodsName() {

return goodsName;

}

}

用java實現(xiàn)野人傳教士過河問題

//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編寫三個線程實現(xiàn)狼羊白菜過河問題

開三個線程,一個代表狼,一個代表羊,一個代表白菜。

一艘船。兩個位置。

河有兩邊,

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

即他們不能在船不在此岸邊的時候同時存在。

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

再開一個線程。。。。OYE~

船判斷能不能離岸,不能離就泄空。能就到對岸就把這兩個位置上的泄到對岸,船也到對岸。

然后狼,羊,白菜的線程繼續(xù)去搶船的位置。

船線程繼續(xù)判能不能離岸。(船上的位置剩余0或1或2時只要2岸不出現(xiàn)互斥,都可以離岸)

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

如此往復(fù)

直到有一天。。。3個都到對岸了。。OK了。。。

誰會要求寫出這樣沒有邏輯的純靠運(yùn)氣的程序啊。。。

現(xiàn)在的學(xué)校真操蛋。。。。

本文題目:主仆過河問題Java代碼,主仆過河問題java代碼是什么
本文鏈接:http://chinadenli.net/article40/hchoeo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站ChatGPT網(wǎng)站收錄App設(shè)計服務(wù)器托管品牌網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營