



免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/ farmerProblem.c/ 用队列解决农夫过河问题#include #include typedef int DataType;/顺序队列:类型和界面函数声明struct SeqQueue / 顺序队列类型定义 int MAXNUM; / 队列中最大元素个数 int f, r; DataType *q;typedef struct SeqQueue *PSeqQueue; / 顺序队列类型的指针类型PSeqQueue createEmptyQueue_seq(int m) /创建一个空队列 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);/个体状态判断函数int farmer(int location) /判断农夫的位置 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);/安全状态的判断函数int safe(int location) / 若状态安全则返回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); / 其他状态是安全的main() int i, movers, location, newlocation; int route16; /用于记录已考虑的状态路径 PSeqQueue moveTo; /用于记录可以安全到达的中间状态 moveTo = createEmptyQueue_seq(20); /创建空队列 enQueue_seq(moveTo, 0x00); /初始状态进队列 for (i = 0; i 16; i+) routei = - 1; /准备数组route初值 route0 = 0; while (!isEmptyQueue_seq(moveTo) & (route15 = - 1) location = frontQueue_seq(moveTo); /取队头状态为当前状态 deQueue_seq(moveTo); for (movers = 1; movers = 8; movers = 0; location = routelocation)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025网签版贷款合同范本
- 2025国际计算机硬件采购与技术支持合同范本
- 南通融通控股集团有限公司招聘笔试题库2025
- 李开复成功理念与价值观测试卷附答案
- 2025茶叶购销合同简易模板
- 2025年全国营销师三级真题及答案
- 2025年度讲师在线课程制作与销售合同
- 2025版绿色能源项目执行经理聘用合同
- 制动电控基础知识考试题及答案
- 食品卫生考试题库及答案
- 一年级幼小衔接开学第一课系列:《会问好》教学课件
- 1.2 规划初中生活(课件)-2024-2025学年七年级道德与法治上册 (统编版2024)
- 乒乓球聘用教练合同模板
- 叙事护理课件
- DB11-T+2260-2024中成药单位产品能源消耗限额
- NB-T+10131-2019水电工程水库区工程地质勘察规程
- 民办非企业单位年度调查报告书
- 消化道出血护理新进展
- 【浅析机械自动化技术的发展现状及发展趋势8900字(论文)】
- MOOC 大学计算机-思维与应用-周口师范学院 中国大学慕课答案
- (2024年)TWI培训课件完整版
评论
0/150
提交评论