过河问题-数学建模C++实现

过河问题-数学建模C++实现,第1张

经典的过河问题:一个人(猎人)带了:一只鸡(羊),一条狗(狼),一袋米(草),遇到一条河,河边有一条船,船太小每次只能带一样东西,此人如何将自己的三件物品完好的带到对岸?

(注:若是VS2010开发工具源码复制可直接运行,若是其他开发工具,可能要小部分修改,源码核心算法不用改动。具体实现请查看相应注释!此文仅供学习参考!)

程序源码:

// CrossRiver.cpp :定义控制台应用程序的入口点。

//开发工具:VS2010旗舰版

//系统环境:window7旗舰版

//实现语言:C语言

//班级:计算机XXX班

//学号:20XXXXXXXXXX

#include"stdafx.h"

#include

boolisEnd(int [],int )//判断是否结束

int_tmain(int argc, _TCHAR* argv[])

{

intA[4]={0,0,0,0}//定义人、狗、鸡、米的初始状态,此时四者都在此岸

//定义过渡状态,包括:人自己过河,人载狗过河,人载鸡过河,人载米过河,四种状态。

//注:其中最后的一维用来标记该向量是否已被使用,使用记为1,未使用记为0,在数组中即是a[x][4]存放的变量。

inta[4][5]={{1,0,0,0,0},{1,1,0,0,0},{1,0,1,0,0},{1,0,0,1,0}}

//保存输出的对应步骤的字符串

char* str[4]={"人划船过河","人划船载狗过河","人划船载鸡过河","人划船载米过河"}

//问题描述

printf(">>>>>>>>某日,路人甲在河边遇到了一个难题:\n")

printf("\t他带了三件物品:一只狗、一只鸡、一袋米要到河的对岸去,然而河边的\n")

printf("\t小船载重太小,每次只允许载三件物品里的一件过河。但是,人不在的\n")

printf("\t时候,鸡和米或狗和鸡在一边时,鸡会去吃米,狗会去咬鸡。\n")

printf(">>>>>>>>那么他该如何过河才能保证三件物品完好无损呢?\n")

printf("\n---解---过河步骤如下:\n")

//记录步骤

int n=1

//开始进行状态转移

while(!isEnd(A,4)){

for(int i=0i<4i++){

//当过渡状态向量未被使用时

if(!a[i][4]){

//保存转换中间态

int B[4]

//异或运算,求转换态

for(int k=0k<4k++){

B[k]=A[k]^a[i][k]

}

//状态(1,0,0,x)是不允许的,此时人在河的彼岸,有没有载米过去,狗和鸡都会出现问题。

//状态(0,1,1,x)是不允许的,此时人在河的此岸,有没有载米回来,狗和鸡都会出现问题。

if((!B[1]&&!B[2]&&B[0])||(B[1]&&B[2]&&!B[0]))continue

//状态(1,x,0,0)是不允许的,此时人在河的彼岸,有没有载狗过去,鸡和米都会出现问题。

//状态(0,x,1,1)是不允许的,此时人在河的此岸,有没有载狗回来,鸡和米都会出现问题。

elseif((!B[2]&&!B[3]&&B[0])||(B[2]&&B[3]&&!B[0]))continue

//其它状态允许

else {

//改变状态

for(int j=0j<4j++){

A[j]=B[j]

}

//表示该状态已被使用

a[i][4]=1

//输出对应的步骤描述

printf("\n-%d-%s\n",n,str[i])//输出相应步骤

//步骤加一

n++

}

}

//当过渡向量已被使用时,修改其使用状态值,以便于下一次使用。

else a[i][4]=0

}

}

printf("\n----------------------此时,人、狗、鸡、米已全部过河!\n")

system("pause")

//结束

return 0

}

boolisEnd(int L[],int n){//判断是否结束,只要状态向量有一个为零,即还未结束。

for(int i=0i

//判断状态向量的各个值,为零即停止循环,返回false

if(!L[i])return false

}

//状态向量的各个值都为1,这表示四者都已在彼岸。过河完成!

return true

}

运行结果:

cocos2d-x 1.0版本的

这个东西很有意思!可以完成技能冷却的效果,也有人拿他来做进度条!

CCSize s = CCDirector::sharedDirector()->getWinSize()

CCProgressTo *to1 = CCProgressTo::actionWithDuration(2, 100)//2代表时间,100代表百分比

CCProgressTimer *left = CCProgressTimer::progressWithFile("Icon.png")

left->setType( kCCProgressTimerTypeHorizontalBarLR )

addChild(left)

left->setPosition(CCPointMake(100, s.height/2))

left->runAction( CCRepeatForever::actionWithAction(to1))

cocos2d-x 2.0以上

CCSprite *s=CCSprite::create("Icon.png")

CCProgressTimer *pt=CCProgressTimer::create(s)

pt->setPosition(ccp(300,300))

pt->setType(cocos2d::CCProgressTimerType(kCCProgressTimerTypeRadial))

this->addChild(pt,1)

CCProgressTo *t=CCProgressTo::create(8,100)

pt->runAction(CCRepeatForever::create(t))

我用1.0做的dome 所以1.0有6种效果

typedef enum {

/// Radial Counter-Clockwise

kCCProgressTimerTypeRadialCCW,

/// Radial ClockWise

kCCProgressTimerTypeRadialCW,

/// Horizontal Left-Right

kCCProgressTimerTypeHorizontalBarLR,

/// Horizontal Right-Left

kCCProgressTimerTypeHorizontalBarRL,

/// Vertical Bottom-top

kCCProgressTimerTypeVerticalBarBT,

/// Vertical Top-Bottom

kCCProgressTimerTypeVerticalBarTB,

} CCProgressTimerType

kCCProgressTimerTypeRadialCCW 和kCCProgressTimerTypeRadialCW 一个以中心 顺时方向 和逆时方向

kCCProgressTimerTypeHorizontalBarLR和kCCProgressTimerTypeHorizontalBarRL 是左右方向

kCCProgressTimerTypeVerticalBarBT和kCCProgressTimerTypeVerticalBarTB 上下方向

AMI编码VHDL程序

library  ieee

use  ieee.std_logic_1164.all

use  ieee.std_logic_unsigned.all

entity ami  is

port(clk:in bit

input:in bit

output1:out bit

output2:out bit)

end ami

architecture a of ami is

begin

process(clk)

variable c:bit:='0'

begin

if clk'event and clk='1‘ then

if input='1‘ then

if c='0‘ then

output1<='1'

output2<='0'

c:=not c 

else

output1<='0'

output2<='1'

c:=not c 

end if

else

output1<='0'

output2<='0'

end if

end if

end process

end a

AMI译码程序

library ieee

use ieee.std_logic_1164.all

use ieee.std_logic_unsigned.all

entity amiym is

port(clk:in bit

input1,input2:in bit

output: out bit)

end amiym

architecture a of amiym is

begin

process(clk)

begin

if clk'event and clk='0'then

output<=input1 or input2

end if

end process

end a

ps:因为只能传一张图片,我就传了编码的仿真图,译码的简单一点,你自己试试。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11318169.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-15
下一篇2023-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存