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

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


相关推荐

  • pycharm 2018.2.3激活码(最新序列号破解)

    pycharm 2018.2.3激活码(最新序列号破解),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    47
  • Zuul网关调优「建议收藏」

    Zuul网关调优「建议收藏」网关的大部分工作是请求转发,属于IO密集型的应用,我们要在有限的资源的情况下结合公司实际请求场景做调优。一,容器选择在容器方面,undertow的呼声很高,一个是他很轻量级的,其次他属于java开发,性能也很好,笔者根据实际情况对tomcat和undertow做了一个对比默认配置下,8核cpu,tomcat启动后会初始化10个io线程,而undertow会初始化72个线程,8个IO线程,64个work线程(8*8)性能对比:写一个接口,接口中什么也不做,就睡眠2s请求个数

    2022年8月15日
    5
  • 小米路由器3 opkg安装

    小米路由器3 opkg安装小米路由器3opkg安装1、复制opkg到小米路由的/data路径下。下载地址:http://sangbo.pub/soft/opkg/opkg2、修改/etc/opkg.conf文件,替换为以下内容:src/gzattitude_adjustment_basehttp://openwrt.sangbo.pub/barrier_breaker/14.07/ramips/mt7620a/

    2022年6月3日
    43
  • 计算机网络之ip、子网掩码、网络号、主机号等概念解析

    计算机网络之ip、子网掩码、网络号、主机号等概念解析在工作中谈论到计算机网络时,有几个经常出现的术语,比如:ip、子网掩码、网段等等。之前对这些概念的理解都比较模糊,只知其大概意思,随着工作中遇到的网络问题越来越多,有必要详细理解一下计算机网络的基础知识了。这篇文章就先介绍几个计算机网络领域的专业术语。IP地址ip这个词是计算机网络中出现频率最高的了,甚至只要使用过电脑的人都知道这个词。IP地址全程是互联网协议地址(英文:InternetPr…

    2022年6月24日
    38
  • IBinder对象在进程间传递的形式(一)

    IBinder对象在进程间传递的形式(一)

    2021年12月2日
    42
  • 使用Activity动态加载Fragment实现主界面框架

    使用Activity动态加载Fragment实现主界面框架

    2021年9月30日
    59

发表回复

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

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