猴子摘香蕉

发布时间:2015-11-25 14:45:17   来源:文档文库   
字号:

猴子摘香蕉

实验分析

1. 定义描述环境状态的谓词。

Monkey(a):猴子在a点,本题中取abc

Hold(x,t)x手中拿着t

Onbox(a)abox上。

Box(a)boxa点。

Banana(a):bananaa点。

2. 使用谓词、连结词、量词来表示环境状态。

问题的初始状态可表示为:

SoMonkey(a)˄Box(c) ˄Banana(b)

要达到的目标状态为:

S: Monkey(b) ˄Box(b) ˄Onbox(monkey) ˄Hold(monkey,banana)

谓词:

goto(a,b):猴子从a走到b处。

push(a,b):猴子把b推到a处。

climb(a):猴子爬上a

reach(a):猴子去拿a

猴子的路线是:

goto(a,c)push(c,box)climb(box)reach(banana)

在上述过程中,我们应该注意,当猴子执行某一个操作之前,需要检查当前状态是否可使所要求的条件得到满足,即证明当前状态是否蕴涵操作所要求的状态的过程。在行动过程中, 检查条件的满足性后才进行变量的代换。代入新条件后的新状态如果是目标状态,则问题解决;否则看是否满足下面的操作,如果不满足或即使满足却又回到了原来的状态,那么代入无效。

源代码

#include

struct State

{

int monkey; /*0:Monkey at A;1: Monkey at B;2:Monkey at C;*/

int box; /*0:box at A;1:box at B;2:box at C;*/

int monbox; /*1: monkey on the box;0: monkey are not on the box;*/

};

char* routesave[10];

void nextStep(struct State States,int i){

if (States.monkey == States.box){

if (States.monkey == 1) {

if (States.monbox == 1) {

printf("you've got the result:\n");

routesave[i]="Monkey get the banana!";

}else{

States.monbox = 1;

routesave[i]="Monkey climbs on the box!";

nextStep(States,i+1);

}

}else{

States.monkey = 1;

States.box = 1;

routesave[i]="Monkey pushes the box to B!";

nextStep(States,i+1);

}

}else{

States.monkey = States.box;

routesave[i]="Monkey goes to the box!";

nextStep(States,i+1);

}

}

int main(){

struct State States;

States.monkey=0;

States.box=2;

States.monbox=0;

nextStep(States,0);

for (int i=0; routesave[i] ; i++) {

printf("Step %d : %s \n",i+1,routesave[i]);

}

}

本文来源:https://www.2haoxitong.net/k/doc/f2462787482fb4daa58d4bc5.html

《猴子摘香蕉.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式