人工智能猴子摘香蕉问题的逻辑表示_猴子拿香蕉实验感悟

人工智能猴子摘香蕉问题的逻辑表示_猴子拿香蕉实验感悟猴子摘香蕉问题:一个房间里,天花板上挂有一串香蕉,有一只猴子可在房间里任意活动(到处走动,推移箱子,攀登箱子等)。设房间里还有一只可被猴子移动的箱子,且猴子登上箱子时才能摘到香蕉,问猴子在某一状态下(设猴子位置为A,箱子位置为B,香蕉位置在C),如何行动可摘取到香蕉。代码样例:#includestructState{ intmonkey;//-1:MonkeyatA

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

猴子摘香蕉问题:

一个房间里,天花板上挂有一串香蕉,有一只猴子可在房间里任意活动(到处走动,推移箱子,攀登箱子等)。设房间里还有一只可被猴子移动的箱子,且猴子登上箱子时才能摘到香蕉,问猴子在某一状态下(设猴子位置为A,箱子位置为B,香蕉位置在C),如何行动可摘取到香蕉。

代码样例:

#include<stdio.h>

struct State
{
	int monkey;  //-1:Monkey at A; 0:Monkey at B;  1:Monkey at C;
	int box;	//-1:box at A; 0:box at B; 1:box at C;
	int banana;  //banana at B ,banana = 0;
	int monbox;	//-1:monkey on the box
};

struct State States[150];
char* routesave[150];

//monkey goto , monkey go to other place
void monkeygoto(int b,int i)
{
	int a;
	a = b;
	if(a == -1)
	{
		routesave[i] = "Monkey go to A";
		States[i+1] = States[i];
		States[i+1].monkey = -1;
	}
	else if(a == 0)
	{
		routesave[i] = "Monkey go to C";
		States[i+1] = States[i];
		States[i+1].monkey = 0;
	}
	else if(a ==1)
	{
		routesave[i] = "Monkey go to B";
		States[i+1] = States[i];
		States[i+1].monkey = 1;
	}
	else
	{
		printf("Wrong!");
	}
}

void movebox(int a,int i)
{
	int B;
	B=a;
	if(B== -1)
	{
		routesave[i] = "monkey move box to A";
		States[i+1] = States[i];
		States[i+1].monkey = -1;
		States[i+1].box=-1;
	}
	else if(B==0)
	{
		routesave[i] = "monkey move box to C";
		States[i+1] = States[i];
		States[i+1].monkey = 0;
		States[i+1].box= 0;
	}
	else if(B==1)
	{
		routesave[i] = "monkey move box to B";
		States[i+1] = States[i];
		States[i+1].monkey = 1;
		States[i+1].box= 1;
	}
	else
	{
		printf("Wrong!");
	}
}

void climbonto(int i)
{
	routesave[i] = "monkey climb onto box";
	States[i+1] = States[i];
	States[i+1].monbox = 1;
}

void climbdown(int i)
{
	routesave[i] = "monkey climb down box";
	States[i+1] = States[i];
	States[i+1].monbox = -1;
}

void reach(int i)
{
	routesave[i] = "monkey reach the banana";
}

void showSolution(int i)
{
	int c;
	printf("%s \n","Result to problem:");
	for(c=0;c<i+1;c++)
	{
		printf("Step %d : %s \n",c+1,routesave[c]);
	}
	printf("\n");
}

void nextStep(int i)
{
	int c;
	int j;
	if(i>=100)
	{
		printf("step has reached 100,Wrong!");
		return;
	}
	for(c=0;c<i;c++)
	{
		if(States[c].monkey == States[i].monkey && States[c].box == States[i].box && States[c].banana == States[i].banana && States[c].monbox == States[i].monbox)
		{
			return;
		}
	}
	if(States[i].monbox == 1 && States[i].monkey == 0 && States[i].banana == 0 && States[i].box == 0)
	{
		showSolution(i);
		printf("end");
		while(1)
			getchar();
		return;
	}
	j = j + 1;
	if(States[i].monkey==0)
	{
		if(States[i].box == 0)
		{
			if(States[i].monbox == -1)
			{
				climbonto(i);
				reach(i+1);
				nextStep(j);
			}
			else
			{
				reach(i+1);
				nextStep(j);
			}
		}
		else if(States[i].box == 1)
		{
			monkeygoto(1,i);
			nextStep(j);
			movebox(0,i);
			nextStep(j);
			climbonto(i);
			reach(i+1);
			nextStep(j);
		}
		else	//box = -1
		{
			monkeygoto(-1,i);
			nextStep(j);
			movebox(0,i);
			nextStep(j);
			climbonto(i);
			reach(i+1);
			nextStep(j);
		}
	}
	if(States[i].monkey== -1)
	{
		if(States[i].box ==  -1)
		{
			if(States[i].monbox == -1)
			{
				movebox(0,i);	
				nextStep(j);
				climbonto(i);
				reach(i+1);
				nextStep(j);
			}
			else
			{
				climbdown(i);
				nextStep(j);
				movebox(0,i);
				nextStep(j);
				climbonto(i);

				reach(i+1);
				nextStep(j);
			}
		}
		else if(States[i].box == 0)
		{
			monkeygoto(0,i);
			nextStep(j);
			
			climbonto(i);
			reach(i+1);
			nextStep(j);
		}
		else	
		{
			monkeygoto(1,i);
			nextStep(j);
			movebox(0,i);
			nextStep(j);
			climbonto(i);
			reach(i+1);
			nextStep(j);
		}
	}

	if(States[i].monkey== 1)
	{
		if(States[i].box ==  1)
		{
			if(States[i].monbox == -1)
			{
				movebox(0,i);	
				nextStep(j);
				climbonto(i);
				reach(i+1);
				nextStep(j);
			}
			else
			{
				climbdown(i);
				nextStep(j);
				movebox(0,i);
				nextStep(j);
				climbonto(i);

				reach(i+1);
				nextStep(j);
			}
		}
		else if(States[i].box == -1)
		{
			monkeygoto(-1,i);
			nextStep(j);
			movebox(0,i);
			nextStep(j);
			movebox(0,i);
			nextStep(j);
			climbonto(i);
			reach(i+1);
			nextStep(j);
		}
		else	
		{
			monkeygoto(0,i);
			nextStep(j);
			movebox(0,i);
			nextStep(j);
			climbonto(i);
			reach(i+1);
			nextStep(j);
		}
	}


}

int main()
{
	int q,p,k;
	printf(" -1:A, 1:B ,0:C,  enter the location of monkey , box, banana:\n"); 
	scanf("%d%d%d",&q,&p,&k);
	States[0].monkey = q;
	States[0].box = p;
	States[0].banana = k;
	States[0].monbox = -1;
	nextStep(0);
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/189294.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 反射入门_入门教程

    反射入门_入门教程反射package com.atguigu.java;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;public class Person { private String name; public int age; public Person() { } public void setName(S

  • 漏洞扫描工具汇总「建议收藏」

    漏洞扫描工具汇总「建议收藏」漏洞扫描器可以快速帮助我们发现漏洞,如SQL注入漏洞、CSRF、缓冲区溢出等。下面就介绍几种常用的漏洞扫描工具。Fortify代码审计工具FortifySCA(FortifyStaticCodeAnalyzer),一款软件代码安全测试工具,提供静态源码扫描能力,包含了五大引擎分析系统:语义、结构、数据流、控制流、配置流。分析的过程中与特有的软件安全漏洞规则集进行全面的匹配、查找,从而将源代码中存在的安全漏洞扫描出来,并生成报告。BurpSuiteAWVSAppScanDependen

  • Windows下 文件夹加密

    Windows下 文件夹加密此加密方法會使这个特殊的文件夹用正常情况不可打開也不可删除,可以起到保护重要资料的作用。  在Windows中“\”符号是路径的分隔符,比如“C:\Windows\System.exe”的意思是C分区的Windows文件夹中的System.exe文件。如果文件名中有“\”符号会怎么样呢?假如“sexinsex\”是一个文件夹的名字,这个文件夹位于“F:\”,它的路径就是“F:\sexinse…

  • sqlserver datetime与smalldateTime

    sqlserver datetime与smalldateTimedatetime 从1753年1月1日到9999年12月31日的日期和时间数据,精确度为百分之三秒(等于3.33毫秒或0.00333秒)。–A.测试datetime精度问题DECLARE@tTABLE(datechar(21))INSERT@tSELECT’1900-1-100:00:00.000’INSERT@t

  • 线程的用户态和内核态_缺页发生在用户态还是内核态

    线程的用户态和内核态_缺页发生在用户态还是内核态(1)用户态和内核态的概念?—>内核态:CPU可以访问内存所有数据,包括外围设备,例如硬盘,网卡.CPU也可以将自己从一个程序切换到另一个程序—>用户态:只能受限的访问内存,且不允许访问外围设备.占用CPU的能力被剥夺,CPU资源可以被其他程序获取(2)为什么需要用户态和内核态?—>由于需要限制不同的程序之间的访问能力,防止他们获取别的程序的内存数据,…

  • QStringList与QString互转

    QStringList与QString互转QStringListfonts;fonts&lt;&lt;"Arial"&lt;&lt;"Helvetica"&lt;&lt;"Times"&lt;&lt;"Courier";QStringstr=fonts.join(",");QStringstr="name1,path1;name2,p

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号