设为首页
收藏本站
用户名
Email
自动登录
找回密码
密码
登录
注册
快捷导航
平台
Portal
论坛
BBS
文库
项目
群组
Group
我的博客
Space
搜索
搜索
热搜:
shell
linux
mysql
本版
用户
LinuxSir.cn,穿越时空的Linuxsir!
»
论坛
›
Linux 发行版讨论区 —— LinuxSir.cn
›
Debian Linux
›
农夫过河问题?
1
2
/ 2 页
返回列表
楼主:
sipingal
农夫过河问题?
[复制链接]
chaisave
chaisave
当前离线
积分
2402
IP卡
狗仔卡
发表于 2004-9-23 22:24:30
|
显示全部楼层
我觉得用 scheme 的 amb 表达式做的话,会简单一些。
主要就是用回溯来试探,最后比较出一个最优解。
回复
支持
反对
使用道具
举报
显身卡
sipingal
sipingal
当前离线
积分
692
IP卡
狗仔卡
楼主
|
发表于 2004-9-24 10:43:24
|
显示全部楼层
这里有个C的范例(用队列算法)
哪位能否稍加说明
/* 用队列解决农夫过河问题的算法*/
#include<stdio.h>
#include<stdlib.h>
#define MAXNUM 20
typedef int DataType;
struct SeqQueue { /* 顺序队列类型定义 */
int f, r;
DataType q[MAXNUM];
};
typedef struct SeqQueue *PSeqQueue; /* 顺序队列类型的指针类型 */
PSeqQueue createEmptyQueue_seq( void ) {
PSeqQueue paqu = (PSeqQueue)malloc(sizeof(struct SeqQueue));
if (paqu == NULL)
printf("Out of space!! \n");
else
paqu->f = paqu->r = 0;
return (paqu);
}
int isEmptyQueue_seq( PSeqQueue paqu ) {
return paqu->f == paqu->r;
}
/* 在队列中插入一元素x */
void enQueue_seq( PSeqQueue paqu, DataType x ) {
if ( (paqu->r + 1) % MAXNUM == paqu->f )
printf( "Full queue.\n" );
else {
paqu->q[paqu->r] = x;
paqu->r = (paqu->r + 1) % MAXNUM;
}
}
/* 删除队列头部元素 */
void deQueue_seq( PSeqQueue paqu ) {
if( paqu->f == paqu->r )
printf( "Empty Queue.\n" );
else
paqu->f = (paqu->f + 1) % MAXNUM;
}
/* 对非空队列,求队列头部元素 */
DataType frontQueue_seq( PSeqQueue paqu ) {
return (paqu->q[paqu->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);
}
/* 若状态安全则返回true */
int safe(int location) {
/* 羊吃白菜 */
if ((goat(location) == cabbage(location)) &&
(goat(location) != farmer(location)) )
return 0;
/* 狼吃羊 */
if ((goat(location) == wolf(location)) &&
(goat(location) != farmer(location)))
return 0;
return 1; /* 其他状态是安全的 */
}
void farmerProblem( ) {
int movers, i, location, newlocation;
int route[16]; /*记录已考虑的状态路径*/
PSeqQueue moveTo;
/*准备初值*/
moveTo = createEmptyQueue_seq( );
enQueue_seq(moveTo, 0x00);
for (i = 0; i < 16; i++) route[i] = -1;
route[0]=0;
/*开始移动*/
while (!isEmptyQueue_seq(moveTo)&&(route[15] == -1)) {
/*得到现在的状态*/
location = frontQueue_seq(moveTo);
deQueue_seq(moveTo);
for (movers = 1; movers <= 8; movers <<= 1) {
/* 农夫总是在移动,随农夫移动的也只能是在农夫同侧的东西 */
if ((0 != (location & 0x08)) == (0 != (location & movers))) {
newlocation = location^(0x08|movers);
if (safe(newlocation) && (route[newlocation] == -1)) {
route[newlocation] = location;
enQueue_seq(moveTo, newlocation);
}
}
}
}
/* 打印出路径 */
if(route[15] != -1) {
printf("The reverse path is : \n");
for(location = 15; location >= 0; location = route[location]) {
printf("The location is : %d\n",location);
if (location == 0) return;
}
}
else
printf("No solution.\n");
}
int main() {
farmerProblem( );
return 0;
}
复制代码
回复
支持
反对
使用道具
举报
显身卡
1
2
/ 2 页
返回列表
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
注册
本版积分规则
发表回复
回帖后跳转到最后一页
浏览过的版块
开源软件专题讨论
Redhat/Fedora/CentOS Linux
Gentoo Linux
Copyright © 2002-2023
LinuxSir.cn
(http://www.linuxsir.cn/) 版权所有 All Rights Reserved.
Powered by
RedflagLinux!
技术支持:
中科红旗
|
京ICP备19024520号
快速回复
返回顶部
返回列表