#includeiostream.h
站在用戶的角度思考問題,與客戶深入溝通,找到容城網(wǎng)站設(shè)計與容城網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站設(shè)計制作、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、主機域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋容城地區(qū)。
#includestdio.h
#defineMAXNUM 20
typedefstruct //順序隊列類型定義
{
int f, r; //f表示頭,r 表示尾
int q[MAXNUM];//順序隊
}SeqQueue,*PSeqQueue;
PSeqQueuecreateEmptyQueue_seq( ) //創(chuàng)建隊列
{
PSeqQueue paqu = new SeqQueue;
if(paqu == NULL)
cout"Out of space!"endl;
else
paqu-f=paqu-r=0;
return (paqu);
}
intisEmptyQueue_seq( PSeqQueue paqu ) //判斷paqu 所指是否是空隊列
{
return paqu-f==paqu-r;
}
voidenQueue_seq(PSeqQueue paqu,int x) //在隊列中插入一元素 x
{
if ((paqu-r+1)%MAXNUM==paqu-f)
cout"隊列已滿."endl;
else
{
paqu-q[paqu-r]=x;
paqu-r=(paqu-r+1)%MAXNUM;
}
}
void deQueue_seq(PSeqQueue paqu) //刪除隊列頭部元素
{
if( paqu-f==paqu-r)
cout"隊列為空"endl;
else
paqu-f=(paqu-f+1)%MAXNUM;
}
intfrontQueue_seq( PSeqQueue paqu ) //對非空隊列,求隊列頭部元素
{
return (paqu-q[paqu-f]);
}
intfarmer(int location) //判斷農(nóng)夫位置對0做與運算,還是原來的數(shù)字,用來判斷位置
{
return 0 != (location 0x08);
}
intwolf(int location) //判斷狼位置
{
return 0 != (location 0x04);
}
intcabbage(int location) //判斷白菜位置
{
return 0 != (location 0x02);
}
intgoat(int location) //判斷羊的位置
{
return 0 !=(location 0x01);
}
intsafe(int location) // 若狀態(tài)安全則返回 true
{
if ((goat(location) == cabbage(location)) (goat(location) != farmer(location)) )
return 0;
if ((goat(location) == wolf(location)) (goat(location) != farmer(location)))
return 0;
return 1; //其他狀態(tài)是安全的
}
void farmerProblem( )
{
int movers, i, location, newlocation;
int route[16]; //記錄已考慮的狀態(tài)路徑
int print[MAXNUM];
PSeqQueue moveTo;
moveTo = createEmptyQueue_seq( );//新的隊列判斷路徑
enQueue_seq(moveTo, 0x00); //初始狀態(tài)為0
for (i = 0; i 16; i++)
route[i] = -1; //-1表示沒有記錄過路徑
route[0]=0;
while(!isEmptyQueue_seq(moveTo)(route[15] == -1))//隊列不為空,路徑未滿時循環(huán)
{
location = frontQueue_seq(moveTo); //從隊頭出隊
deQueue_seq(moveTo);
for (movers = 1; movers = 8;movers= 1)
{
if ((0 != (location 0x08)) == (0 !=(location movers)))
{
newlocation = location^(0x08|movers);//或運算
if (safe(newlocation) (route[newlocation] == -1))//判斷是否安全,以及路徑是否可用
{
route[newlocation] = location;
enQueue_seq(moveTo, newlocation);
}
}
}
}
/*打印出路徑 */
if(route[15] != -1)
{
cout"過河步驟是 : "endl;
i=0;
for(location = 15; location = 0; location= route[location])
{
print[i]=location;
i++;
if (location == 0)
break;
}
int num=i-1;
int temp[20][4];
int j;
for(i=num;i=0;i--)
{
for(j=3;j=0;j--)
{
temp[num-i][j]=print[i]%2;
print[i]/=2;
temp[0][j]=0;
temp[num+1][j]=1;
}
}
for(i=1;i=num;i++)
{
cout"\t\t\tNO ."i"\t";
if(i%2==1)
{
if(temp[i][3]!=temp[i-1][3])
cout"農(nóng)夫帶羊過南岸";
if(temp[i][2]!=temp[i-1][2])
cout"農(nóng)夫帶白菜過南岸";
if(temp[i][1]!=temp[i-1][1])
cout"農(nóng)夫帶狼過南岸";
if(temp[i][3]==temp[i-1][3]temp[i][2]==temp[i-1][2]temp[i][1]==temp[i-1][1])
cout"農(nóng)夫自己過南岸";
}
else if(i%2==0)
{
if(temp[i][3]!=temp[i-1][3])
cout"農(nóng)夫帶羊回北岸";
if(temp[i][2]!=temp[i-1][2])
cout"農(nóng)夫帶白菜回北岸";
if(temp[i][1]!=temp[i-1][1])
cout"農(nóng)夫帶狼回北岸";
if(temp[i][3]==temp[i-1][3]temp[i][2]==temp[i-1][2]temp[i][1]==temp[i-1][1])
cout"農(nóng)夫自己回北岸";
}
coutendl;
}
}
else
cout"Nosolution."endl;
}
int main() /*主函數(shù)*/
{
farmerProblem();
return 0;
}
好繁瑣...一堆if else...你不覺得很麻煩嗎? 我給你提個思路. 你每個Goods object里面都設(shè)置一個天敵的list.
Goods?g?=?new?Goods("Sheep");
g.setEnemy(Arrays.asList(new?String[]{"wolf"?}));
Goods?g2?=?new?Goods("Cabbage");
g2.setEnemy(Arrays.asList(new?String[]{"Sheep"?}));
Goods?g3?=?new?Goods("Wolf");
g3.setEnemy(Arrays.asList(new?String[]{}));
這樣你的在check isFriendly的時候, 只要檢測2個物品的enemyList里面沒有自己就可以了.
return?!good1.getEnemyList().contains(good2.getName())??!good2.getEnemyList().contains(good1.getName());
先把羊帶過去
回去
把狼帶過去
把羊帶回去
把白菜帶過去
回去
把羊帶過去
這個是偶寫的 你可以參考下 寫的有點多 你自己優(yōu)化下吧 之前還不知道農(nóng)夫過河是啥意思 不過后來知道了 如果有問題的話可以馬上說的 你的50分偶要定咯!!(可以直接運行)
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)系是否友好 寫的麻煩了一點= =..
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;
}
}
當(dāng)前名稱:農(nóng)夫過河java代碼 農(nóng)夫過河問題共有幾種方案
轉(zhuǎn)載來源:http://jinyejixie.com/article42/ddiciec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、面包屑導(dǎo)航、域名注冊、用戶體驗、網(wǎng)站改版、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)