完整word版C實(shí)現(xiàn)傳教士與野人過河問題實(shí)驗(yàn)報(bào)告_第1頁
完整word版C實(shí)現(xiàn)傳教士與野人過河問題實(shí)驗(yàn)報(bào)告_第2頁
完整word版C實(shí)現(xiàn)傳教士與野人過河問題實(shí)驗(yàn)報(bào)告_第3頁
完整word版C實(shí)現(xiàn)傳教士與野人過河問題實(shí)驗(yàn)報(bào)告_第4頁
完整word版C實(shí)現(xiàn)傳教士與野人過河問題實(shí)驗(yàn)報(bào)告_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、傳教士與野人過河問題實(shí)驗(yàn)報(bào)告1問題定義河的兩岸有三個(gè)傳教士和三個(gè)野人需要過河,目前只有一條能裝下兩個(gè)人的 船,在河的任何一方或者船上,如果野人的人數(shù)大于傳教士的人數(shù), 那么傳教士 就會(huì)被野人攻擊,怎么找出一種安全的渡河方案呢?2算法分析首先,先來看看問題的初始狀態(tài)和目標(biāo)狀態(tài),定義河的兩岸分別為左岸和右岸,設(shè)定狀態(tài)集合為(左岸傳教士人數(shù),右岸野人數(shù),右岸傳教士人數(shù),右岸野 人數(shù),船的位置),船的位置:-1表示船在左岸,1表示船在右岸。初始狀態(tài):(330,0,0 ,-1) 目標(biāo)狀態(tài):(0,0,3,3,1)然后,整個(gè)問題就抽象成了怎樣從初始狀態(tài)經(jīng)中間的一系列狀態(tài)達(dá)到目標(biāo)狀態(tài)。問題狀態(tài)的改變是通過劃船

2、渡河來引發(fā)的, 所以合理的渡河操作就成了通常 所說的算符,根據(jù)題目要求,可以得出以下 5個(gè)算符(按照渡船方向的不同,也 可以理解為10個(gè)算符): 渡1野人、渡1傳教士、渡1野人1傳教士、渡2野人、渡2傳教士根據(jù)船的位置,向左移或向右移通過遞歸依次執(zhí)行5種算符,判斷是否找到 所求,并排除不符合實(shí)際的狀態(tài),就可以找到所有可能的解,如圖1所示為遞歸 函數(shù)流程圖。數(shù)據(jù)結(jié)構(gòu)方面采用如下所示的結(jié)構(gòu)體存儲(chǔ)當(dāng)前傳教士、野人、船三者的狀態(tài)。churchL; /左岸傳教士數(shù)wiIdL; /左岸野人數(shù) churchR; /右岸傳教士數(shù)wildR; /右岸野人數(shù)boat; /船的位置,-1在左岸,1在右岸struct

3、 riversides intintintintint;3 編程實(shí)現(xiàn)程序使用C+實(shí)現(xiàn),具體代碼如下:#inelude #inelude #inelude using namespacestd;structriversidesintehurehL;intint/左岸傳教士數(shù) 左岸野人數(shù)/右岸傳教士數(shù)intintwildL; /ehurehR;wildR; /右岸野人數(shù)boat; /船的位置,-1在左岸,1在右岸;myeount = 0;/統(tǒng)計(jì)成功過河次數(shù)CvsWdfs( riverSidesveetor operationintintlasteurre ntState, veetor last

4、Parameters ,int ifboaeurrentStatety )if(lasteurrentState.ehurehR = 3 &lasteurre ntState.wildR = 3)myeou nt+;第傳教士野人|i = 0; i eout eout for ( int |myeount 次成功過河 endl;移動(dòng)方向 endl;operation .size。; i+)eout operation i endl;eout en dl;return 0;|/判斷過河操作否重復(fù),去除死循環(huán)for (int i = 0; i last Parameters .size() - 1;

5、 i+)if ( last Parameters i.wildL =.wildL& last Parameters i.ehurehL =lasteurre ntState.ehurehL)lasteurre ntStateif(lasteurre ntState.boat = last Parameters i.boat)return 0;|/檢驗(yàn)人數(shù)數(shù)據(jù)合法性(lastcurrentState.churchL 0 |lastcurrentState.wildL 0 |lastcurre ntState .churchR 0 |lastcurre ntState.wildR 0)return

6、 0; |傳教士是否被吃if/if0) 11 (lastcurrentState.churchL lastcurrentState .wildL& lastcurrentState.churchL !=lastcurrentState .churchR 右岸);operation .push_back( 20 |curre ntState.churchL = lastcurre ntState右岸- 左岸);.churchL - 2 *lastcurre ntState.boat;curre ntState.wiIdL =lastcurre ntState.wiIdL;curre ntStat

7、e.churchR =lastcurre ntState.churchR + 2 *lastcurre ntState.boat;curre ntState.wildR =lastcurre ntState.wildR;curre ntState.boat =-lastcurre ntState.boat;last Parameters .p ush_back(curre ntState);CvsWdfs(curre ntState,last Parameters,operation , 0);operation .pop_back();last Parameters .po p_back()

8、;/兩個(gè)野人過河if ( lastcurrentState.boat = 1)operation .push_back( 02 |左岸- 右岸);elseoperation .push_back( 02 |lastcurre ntStatecurre ntState.churchL =右岸- 左岸);.churchL;curre ntState.wildL =lastcurre ntState.wildL - 2 *lastcurre ntState.boat;curre ntState.churchR =lastcurre ntState.churchR;curre ntState.wild

9、R =lastcurre ntState.wildR + 2 *lastcurre ntState.boat;curre ntState.boat =-lastcurre ntState.boat;last Parameters .p ush_back(curre ntState);operation , 0);CvsWdfs(curre ntState,last Parameters ,last Parameters .po p_back();operation .pop_back();/ 一個(gè)野人,一個(gè)傳教士if ( lastcurrentState.boat = 1)左岸- 右岸);op

10、eration .push_back( 11 |else右岸- 左岸);operation .push_back( 11 |curre ntState.churchL =lastcurre ntState.churchL - 1 * lastcurre ntState .boat;curre ntState.wildL =lastcurre ntState.wildL - 1 *lastcurre ntState.boat;curre ntState.churchR =lastcurre ntState.churchR + 1 *lastcurre ntState.boat;curre ntS

11、tate.wildR =lastcurre ntState.wildR + 1 * lastcurre ntState .boat;curre ntState.boat =-lastcurre ntState.boat;last Parameters .p ush_back(curre ntState);CvsWdfs(curre ntState,last Parameters,operation , 0);operation .pop_back();last Parameters .po p_back();/ 一個(gè)傳教士過河if ( lastcurrentState.boat = 1)ope

12、ration .push_back( 10 |左岸- 右岸);elseoperation .push back( 10 |右岸- 左岸);curre ntState.churchL =lastcurre ntState.churchL - 1 * lastcurre ntState.boat;curre ntState.wildL =lastcurre ntState.wildL;curre ntState.churchR =lastcurre ntState.churchR + 1 *lastcurre ntState.boat;curre ntState.wildR =lastcurre

13、ntState.wildR;curre ntState.boat =-lastcurre ntState.boat;last Parameters .p ush_back(curre ntState);CvsWdfs(curre ntState,last Parameters ,operation , 0);operation .pop_back();last Parameters .po p_back();/ 一個(gè)野人過河if ( lastcurrentState.boat = 1)operation .push_back( 01 |左岸- 右岸);elseoperation .push_b

14、ack( 01 |右岸- 左岸);curre ntState.churchL =lastcurre ntState.churchL;curre ntState.wildL =lastcurre ntState.wildL - 1 *lastcurre ntState.boat;curre ntState.churchR =lastcurre ntState.churchR;curre ntState.wildR =lastcurre ntState.wildR + 1 *lastcurre ntState.boat;curre ntState.boat =-lastcurre ntState.

15、boat;last Parameters .p ush_back(curre ntState);,operation , 0);CvsWdfs(curre ntState, last Parametersoperation .pop_back();last Parameters .po p_back(); return 0; | int mai n0/分別用來計(jì)算左岸和右岸的傳教士int churchL = 3, wildL = 3, churchR = 0, wildR = 0;和野人vector last Parameters; /保存每一步移動(dòng)操作的兩岸傳教士、野人人數(shù) vector o

16、peration; /保存當(dāng)前操作的描述/初始化左岸參數(shù),可以認(rèn)為是從右岸移動(dòng)至左岸的操作/boat=-1表示船在左岸,boat=1表示船在右岸riverSides curre ntState;curre ntState.churchL = 3;curre ntState.wildL = 3;curre ntState.churchR = 0;curre ntState.wildR = 0;curre ntState.boat = 1;last Parameters .p ush_back(curre ntState);CvsWdfs(curre ntState, last Parameters, op erati on, 0);last Parameters .po p_back();system(pause); return 0;4程序結(jié)果最終得到如圖2、3所示的四種過河方式。t D:替習(xí)赍料個(gè)人C#工程療碼傳教士與野人過J氓De bu 9傳就士與野人遡可,EXE貳羣右左右左右左右左右 / 2右左右左右左右左右左 可 述人功野21210101201

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論