




免费预览已结束,剩余2页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
农夫过河问题一、实验目的掌握广度优先搜索策略,并用队列求解农夫过河问题二、实验内容问题描述:一农夫带着一只狼,一只羊和一颗白菜,身处河的南岸,他要把这些东西全部运到北岸,遗憾的是他只有一只小船,小船只能容下他和一件物品。这里只能是农夫来撑船,同时因为狼吃羊、羊吃白菜、所以农夫不能留下羊和狼或羊和白菜在河的一边,而自己离开;好在狼属肉食动物,不吃白菜。农夫怎么才能把所有的东西安全运过河呢?实验要求如下:(1) 设计物品位置的表示方法和安全判断算法;(2) 设计队列的存储结构并实现队列的基本操作(建立空队列、判空、入队、出队、取对头元素),也可以使用STL中的队列进行代码的编写;(3) 采用广度优先策略设计可行的过河算法;(4) 输出要求:按照顺序输出一种可行的过河方案;提示:可以使用STL中的队列进行代码编写。程序运行结果:二进制表示:1111 0110 1110 0010 1011 0001 1001, 0000三、农夫过河算法流程n Step1:初始状态0000入队n Step2:当队列不空且没有到达结束状态1111时,循环以下操作:n 队头状态出队n 按照农夫一个人走、农夫分别带上三个物品走,循环以下操作:n 农夫和物品如果在同一岸,则计算新的状态n 如果新状态是安全的并且是没有处理过的,则更新path ,并将新状态入队n 当状态为1111时,逆向输出path 数组附录一:STL中队列的使用注:队列,可直接用标准模板库(STL)中的队列。需要#includeSTL中的queue,里面的一些成员函数如下(具体可以查找msdn,搜索queue class):front:Returns a reference to the first element at the front of the queue.pop:Removes an element from the front of the queuepush:Adds an element to the back of the queueempty:Tests if the queue is empty三、实验代码FarmerRiver.H#ifndef FARMERRIVER_H#define FARMERRIVER_Hint FarmerOnRight(int status); /农夫,在北岸返回1,否则返回0int WorfOnRight(int status); /狼int CabbageOnRight(int status); /白菜int GoatOnRight(int status); /羊int IsSafe(int status); /判断状态是否安全,安全返回1,否则返回0void FarmerRiver();#endifSeqQueue.h#ifndef SEQQUEUE_H#define SEQQUEUE_Htypedef int DataType;struct Queueint Max;int f;int r;DataType *elem;typedef struct Queue *SeqQueue;SeqQueue SetNullQueue_seq(int m);int IsNullQueue_seq(SeqQueue squeue);void EnQueue_seq(SeqQueue squeue, DataType x);void DeQueue_seq(SeqQueue);DataType FrontQueue_seq(SeqQueue);#endifFarmerRiver.c#include #include #include SeqQueue.h#include FarmerRiver.hint FarmerOnRight(int status) /判断当前状态下农夫是否在北岸return (0!=(status & 0x08);int WorfOnRight(int status)return (0!=(status & 0x04);int CabbageOnRight(int status)return (0!=(status & 0x02);int GoatOnRight(int status)return (0!=(status & 0x01);int IsSafe(int status) /判断当前状态是否安全if (GoatOnRight(status)=CabbageOnRight(status) & (GoatOnRight(status)!=FarmerOnRight(status)return (0); /羊吃白菜if (GoatOnRight(status)=WorfOnRight(status) & (GoatOnRight(status)!=FarmerOnRight(status)return 0; /狼吃羊return 1; /其他状态是安全的void FarmerRiver() int i, movers, nowstatus, newstatus;int status16; /用于记录已考虑的状态路径SeqQueue moveTo;moveTo = SetNullQueue_seq(20); /创建空列队EnQueue_seq(moveTo, 0x00); /初始状态时所有物品在北岸,初始状态入队for (i=0; i16; i+) /数组status初始化为-1statusi = -1;status0 = 0;/队列非空且没有到达结束状态while (!IsNullQueue_seq(moveTo) & (status15=-1)nowstatus = FrontQueue_seq(moveTo); /取队头DeQueue_seq(moveTo);for (movers=1; movers=8; movers=0; nowstatus=statusnowstatus)printf(The nowstatus is: %dn, nowstatus);if (nowstatus = 0)return;elseprintf(No solution.n);Sequeue.c#include #include #include SeqQueue.hSeqQueue SetNullQueue_seq(int m)SeqQueue squeue;squeue = (SeqQueue)malloc(sizeof(struct Queue);if (squeue=NULL)printf(Alloc failuren);return NULL;squeue-elem = (int *)malloc(sizeof(DataType) * m);if (squeue-elem!=NULL)squeue-Max = m;squeue-f = 0;squeue-r = 0;return squeue;else free(squeue);int IsNullQueue_seq(SeqQueue squeue)return (squeue-f=squeue-r);void EnQueue_seq(SeqQueue squeue, DataType x) /入队if (squeue-r+1) % squeue-Max=squeue-f) /是否满printf(It is FULL Queue!);elsesqueue-elemsqueue-r = x;squeue-r = (squeue-r+1) % (squeue-Max);void DeQueue_seq(SeqQueue squeue) /出队if (IsNullQueue_seq(squeue)printf(It is empty queue!n);elsesqueue-f = (squeue-f+1) % (squeue-Max);DataType FrontQueue_seq(SeqQueue squeue) /求队列元素i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年度国际贸易结算与风险控制服务合同
- 2025版宿舍管理员绿色节能技术应用服务合同
- 2025版机场候机厅软装设计施工合同
- 2025年度吊装工程合同范本(含吊装设备维护与保养)
- 2025年度事业单位实习生实习合同
- 2025版绿色环保住宅区绿化施工与维护合同
- 2025版数据中心通风系统升级改造合同
- 2025年度男方外遇婚姻解除协议书范本
- 2025年度房产按揭贷款与装修贷优惠利率合同
- 2025年清洁服务人员安全培训及管理合同范本
- 《免除烦恼》课件
- 《非权力影响力》课件
- 2025年江西南昌市西湖城市建设投资发展集团有限公司招聘笔试参考题库附带答案详解
- 职业教育产教融合型数字化教材开发研究
- 文学传播学概论课件
- 第3单元主题活动三《创意玩具DIY》(课件)三年级上册综合实践活动
- 商务英语词汇大全
- 麻醉质量控制专家共识
- 反走私课件完整版本
- 2024-2025学年小学劳动一年级上册人教版《劳动教育》教学设计合集
- You Raise Me Up二部合唱简谱
评论
0/150
提交评论