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

人工智能猴子摘香蕉问题的逻辑表示_猴子拿香蕉实验感悟猴子摘香蕉问题:一个房间里,天花板上挂有一串香蕉,有一只猴子可在房间里任意活动(到处走动,推移箱子,攀登箱子等)。设房间里还有一只可被猴子移动的箱子,且猴子登上箱子时才能摘到香蕉,问猴子在某一状态下(设猴子位置为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);
}

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 计算机基础复习题库

    计算机基础复习题库写在最前面,本文中题库为搜寻整理所得。一、单选题练习1.完整的计算机系统由( C )组成。A.运算器、控制器、存储器、输入设备和输出设备B.主机和外部设备C.硬件系统和软件系统D.主机箱、显示器、键盘、鼠标、打印机2.以下软件中,( D )不是操作系统软件。A.WindowsxpB.unixC.linux  D.microsoftoffice3.用一个字节最多能编出(D)不同的码。A.8个…

    2022年4月15日
    54
  • MATLAB对Googlenet模型进行迁移学习

    MATLAB对Googlenet模型进行迁移学习调用MATLAB中的Googlenet工具箱进行迁移学习。%%加载数据clc;closeall;clear;Location=”;%这里输入自己的数据集unzip(‘MerchData.zip’);imds=imageDatastore(‘MerchData’,…%若使用自己的数据集则改为Location(不加单引号)…

    2022年8月14日
    3
  • mongodb联表查询_mongodb聚合查询

    mongodb联表查询_mongodb聚合查询  在使用MongoDB存储数据的时候,我们查询的时候,有时候难免会需要进行连表查询。但是MongoDB本身是非关系性数据库,连表查询,很多时候,需要我们自己在代码里手工操作。但是从MongoDB3.2版本过后,我们可以使用lookup∗∗∗进行连表查询。下面就简单介绍一下MongoDB的∗∗∗lookup∗∗∗进行连表查询。下面就简单介绍一下MongoDB的∗∗∗lookup***进行…

    2022年9月14日
    0
  • 解决virtualbox虚拟机桥接模式下不能上网

    解决virtualbox虚拟机桥接模式下不能上网virtualbox虚拟机桥接模式下不能上网欢迎使用Markdown编辑器欢迎使用Markdown编辑器https://blog.csdn.net/wild46cat/article/details/53100931跟着这个链接操作在主机上设置好网络然后在虚拟机上设置,也是跟着其他作者教程做的,一时找不到那个网页了vim/etc/resolv.confvim/etc/network/interfaces/…

    2022年6月10日
    63
  • java怎样调用oracle存储函数_oracle如何调用存储过程

    java怎样调用oracle存储函数_oracle如何调用存储过程之前给大家介绍了java代码调用存储过程,下面要给大家介绍的就是java当中调用oracle存储过程,一起来看看吧。首先来看一下项目结构:在数据库创建存储过程的脚本,假如,使用的是本地的oracle数据库,那么,就需要开启服务-OracleOraDb11g_home1TNSListener和OracleServiceORCL。实现:输入用户的工号,输出用户名字、薪水以及工作:createorr…

    2022年10月20日
    0
  • 安卓rom包制作_android原生rom下载

    安卓rom包制作_android原生rom下载制作ROM包是做Android系统工程师的第一步,Android第三方的市场

    2022年10月16日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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