猴子摘香蕉
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
猴子摘香蕉
实验分析
1.定义描述环境状态的谓词。
Monkey(a):猴子在a点,本题中取a,b,c。
Hold(x,t):x手中拿着t。
Onbox(a):a在box上。
Box(a):box在a点。
Banana(a):banana在a点。
2.使用谓词、连结词、量词来表示环境状态。
问题的初始状态可表示为:
S o:Monkey(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 <stdio.h>
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]);
}
}。