



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/ 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年度调度规程考试题库及答案
- 高铁隧道施工劳务合同(3篇)
- 高速工程施工合同(3篇)
- 高频焊檩条采购合同模板(3篇)
- n3级级护士手考试试题及答案
- 航空航天单位员工航天技术聘用合同
- 互联网餐厅厨师承包服务与管理协议
- 2025公务员真实面试题及答案
- 深圳市绿色环保产业门面租赁及市场推广协议
- 2025年统编版小升初语文阅读专项训练:点面结合(含答案)
- 羽毛球裁判员培训与实施
- 小学生养成良好学习习惯课件
- 人教版中职数学拓展模块一:3.2.1向量的加法课件(共21张课件)
- 宫外孕大出血护理
- 《乡土中国》非连续性文本阅读专练-2023届高考语文备考专题复习
- 2025年北京市水务局所属事业单位招聘工作人员101人笔试高频重点提升(共500题)附带答案详解
- 2025至2030年中国密炼机上辅机系统行业投资前景及策略咨询研究报告
- 《T CPSS 1013-2021-开关电源电子元器件降额技术规范》
- 初级关务水平测试题库500题(含答案)
- 四川省德阳市中江县2024-2025学年九年级上学期期中考试英语试题(无答案)
评论
0/150
提交评论