




已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
/ farmerProblem.c/ 用隊列解決農(nóng)夫過河問題#include #include typedef int DataType;/順序隊列:類型和界面函數(shù)聲明struct SeqQueue / 順序隊列類型定義 int MAXNUM; / 隊列中最大元素個數(shù) int f, r; DataType *q;typedef struct SeqQueue *PSeqQueue; / 順序隊列類型的指針類型PSeqQueue createEmptyQueue_seq(int m) /創(chuàng)建一個空隊列 PSeqQueue queue = (PSeqQueue)malloc(sizeof(struct SeqQueue); if (queue != NULL) queue-q = (DataType*)malloc(sizeof(DataType) *m); if (queue-q) queue-MAXNUM = m; queue-f = 0; queue-r = 0; return (queue); else free(queue); printf(Out of space!n); / 存儲分配失敗 return NULL;int isEmptyQueue_seq(PSeqQueue queue) /判斷隊列是否為空 return (queue-f = queue-r);void enQueue_seq(PSeqQueue queue, DataType x)/ 在隊尾插入元素x if (queue-r + 1) % queue-MAXNUM = queue-f) printf(Full queue.n); else queue-qqueue-r = x; queue-r = (queue-r + 1) % queue-MAXNUM; void deQueue_seq(PSeqQueue queue)/ 刪除隊列頭部元素 if (queue-f = queue-r) printf(Empty Queue.n); else queue-f = (queue-f + 1) % queue-MAXNUM;DataType frontQueue_seq(PSeqQueue queue) if (queue-f = queue-r) printf(Empty Queue.n); else return (queue-qqueue-f);/個體狀態(tài)判斷函數(shù)int farmer(int location) /判斷農(nóng)夫的位置 return (0 != (location &0x08);int wolf(int location) /判斷狼的位置 return (0 != (location &0x04);int cabbage(int location) /判斷白菜的位置 return (0 != (location &0x02);int goat(int location) /判斷羊的位置 return (0 != (location &0x01);/安全狀態(tài)的判斷函數(shù)int safe(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)是安全的main() int i, movers, location, newlocation; int route16; /用于記錄已考慮的狀態(tài)路徑 PSeqQueue moveTo; /用于記錄可以安全到達(dá)的中間狀態(tài) moveTo = createEmptyQueue_seq(20); /創(chuàng)建空隊列 enQueue_seq(moveTo, 0x00); /初始狀態(tài)進(jìn)隊列 for (i = 0; i 16; i+) routei = - 1; /準(zhǔn)備數(shù)組route初值 route0 = 0; while (!isEmptyQueue_seq(moveTo) & (route15 = - 1) location = frontQueue_seq(moveTo); /取隊頭狀態(tài)為當(dāng)前狀態(tài) deQueue_seq(moveTo); for (movers = 1; movers = 8; movers = 0; location = routelocatio
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 用工管理面試題及答案
- 門診內(nèi)科出科總結(jié)
- 中國教育的目的
- 月字旁寫字課課件
- 2025年中國男士牛仔褲行業(yè)市場全景分析及前景機(jī)遇研判報告
- 綜合能源服務(wù)培訓(xùn)
- 怎樣做好日常培訓(xùn)
- EHS基礎(chǔ)知識培訓(xùn)
- 花山巖畫的群體性活動元素融入舞蹈課堂教學(xué)的實踐與探究
- 特殊關(guān)鍵工序培訓(xùn)
- 近七年寧夏中考化學(xué)真題及答案2024
- Braden 壓力性損傷評分表詳解
- 徐圩港區(qū)疏港航道整治工程報告書
- 動火作業(yè)安全規(guī)范
- XX公司事故隱患內(nèi)部報告獎勵制度1
- 《石油化工工程建設(shè)費用定額》2025
- 鸚鵡熱護(hù)理疑難病例討論
- 企業(yè)會計面試題及答案
- 連云港事業(yè)單位筆試真題2024
- 影視制作基地裝修施工合同
- 河北省唐山市重點達(dá)標(biāo)名校2025屆中考聯(lián)考生物試卷含解析
評論
0/150
提交評論