蚁群算法小程序CC++语言实现Word文档格式.docx

上传人:b****3 文档编号:6379708 上传时间:2023-05-06 格式:DOCX 页数:34 大小:24.82KB
下载 相关 举报
蚁群算法小程序CC++语言实现Word文档格式.docx_第1页
第1页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第2页
第2页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第3页
第3页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第4页
第4页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第5页
第5页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第6页
第6页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第7页
第7页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第8页
第8页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第9页
第9页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第10页
第10页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第11页
第11页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第12页
第12页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第13页
第13页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第14页
第14页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第15页
第15页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第16页
第16页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第17页
第17页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第18页
第18页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第19页
第19页 / 共34页
蚁群算法小程序CC++语言实现Word文档格式.docx_第20页
第20页 / 共34页
亲,该文档总共34页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

蚁群算法小程序CC++语言实现Word文档格式.docx

《蚁群算法小程序CC++语言实现Word文档格式.docx》由会员分享,可在线阅读,更多相关《蚁群算法小程序CC++语言实现Word文档格式.docx(34页珍藏版)》请在冰点文库上搜索。

蚁群算法小程序CC++语言实现Word文档格式.docx

stdio.h"

conio.h"

dos.h"

stdlib.h"

process.h"

ctype.h"

math.h"

voidWorldInitial(void);

voidBlockInitial(void);

voidCreatBlock(void);

voidSaveBlock(void);

voidLoadBlock(void);

voidHomeFoodInitial(void);

voidAntInitial(void);

voidWorldChange(void);

voidAntMove(void);

voidAntOneStep(void);

voidDealKey(charkey);

voidClearSmellDisp(void);

voidDispSmell(inttype);

intAntNextDir(intxxx,intyyy,intddir);

intGetMaxSmell(inttype,intxxx,intyyy,intddir);

intIsTrace(intxxx,intyyy);

intMaxLocation(intnum1,intnum2,intnum3);

intCanGo(intxxx,intyyy,intddir);

intJudgeCanGo(intxxx,intyyy);

intTurnLeft(intddir);

intTurnRight(intddir);

intTurnBack(intddir);

intMainTimer(void);

charWaitForKey(intsecnum);

voidDispPlayTime(void);

intTimeUse(void);

voidHideCur(void);

voidResetCur(void);

/*---------------*/

structHomeStruct

{

intxxx,yyy;

intamount;

intTargetFood;

}home;

structFoodStruct

}food;

structAntStruct

intdir;

intspeed;

intSpeedTimer;

intfood;

intSmellAmount[2];

inttracex[TRACE_REMEMBER];

inttracey[TRACE_REMEMBER];

intTracePtr;

intIQ;

}ant[MAX_ANT];

intAntNow;

inttimer10ms;

structtimestarttime,endtime;

intSmell[2][MAXX+1][MAXY+1];

intblock[MAXX+1][MAXY+1];

intSmellGoneTimer;

intSmellDispFlag;

intCanFindFood;

intHardtoFindPath;

/*-----Main--------*/

voidmain(void)

charKeyPress;

inttu;

clrscr();

HideCur();

WorldInitial();

do

timer10ms=MainTimer();

if(timer10ms)AntMove();

if(timer10ms)WorldChange();

tu=TimeUse();

if(tu>

=60&

&

!

CanFindFood)

gotoxy(1,MAXY+1);

printf("

Cannotfindfood,maybeablockworld."

);

WaitForKey(10);

}

=180&

home.amount<

100&

HardtoFindPath)

God!

itissodifficulttofindapath."

if(WaitForKey(10)==0x0d)WorldInitial();

else

HardtoFindPath=1;

"

if(home.amount>

=home.TargetFood)

gettime(&

endtime);

KeyPress=WaitForKey(60);

DispPlayTime();

elseif(kbhit())

KeyPress=getch();

DealKey(KeyPress);

elseKeyPress=NULL;

while(KeyPress!

=ESC);

ResetCur();

/*------generalsubprocess-----------*/

intMainTimer(void)

/*output:

howmuch10mshavepassfromlasttimecallthisprocess*/

staticintoldhund,oldsec;

structtimet;

inttimeuse;

t);

timeuse=0;

if(t.ti_hund!

=oldhund)

if(t.ti_sec!

=oldsec)

timeuse+=100;

oldsec=t.ti_sec;

timeuse+=t.ti_hund-oldhund;

oldhund=t.ti_hund;

elsetimeuse=0;

return(timeuse);

charWaitForKey(intsecnum)

/*funtion:

ifhavekeyin,exitimmediately,elsewait'

secnum'

sencondsthenexit

input:

secnum--waitthissenconds,must<

3600(1hour)

output:

keychar,ifnokeyin(exitwhentimeout),returnNULL*/

intsecin,secnow;

intminin,minnow;

inthourin,hournow;

intsecuse;

secin=t.ti_sec;

minin=t.ti_min;

hourin=t.ti_hour;

if(kbhit())return(getch());

secnow=t.ti_sec;

minnow=t.ti_min;

hournow=t.ti_hour;

if(hournow!

=hourin)minnow+=60;

if(minnow>

minin)secuse=(minnow-1-minin)+(secnow+60-secin);

elsesecuse=secnow-secin;

/*countingerrorcheck*/

if(secuse<

0)

Timeconutingerror,anykeytoexit..."

getch();

exit(3);

while(secuse<

=secnum);

return(NULL);

voidDispPlayTime(void)

intph,pm,ps;

ph=endtime.ti_hour-starttime.ti_hour;

pm=endtime.ti_min-starttime.ti_min;

ps=endtime.ti_sec-starttime.ti_sec;

if(ph<

0)ph+=24;

if(pm<

0){ph--;

pm+=60;

}

if(ps<

0){pm--;

ps+=60;

Timeuse:

%dhour-%dmin-%dsec"

ph,pm,ps);

intTimeUse(void)

return(ps+(60*(pm+60*ph)));

voidHideCur(void)

unionREGSregs0;

regs0.h.ah=1;

regs0.h.ch=0x30;

regs0.h.cl=0x31;

int86(0x10,®

s0,®

s0);

voidResetCur(void)

regs0.h.ch=0x06;

regs0.h.cl=0x07;

/*------------mainANTprograme-------------*/

voidWorldInitial(void)

intk,i,j;

randomize();

HomeFoodInitial();

for(AntNow=0;

AntNow<

MAX_ANT;

AntNow++)

AntInitial();

}/*offorAntNow*/;

BlockInitial();

for(k=0;

k<

=1;

k++)

/*SMELLTYPEFOODandHOME*/

for(i=0;

i<

=MAXX;

i++)

for(j=0;

j<

=MAXY;

j++)

Smell[k][i][j]=0;

SmellGoneTimer=0;

starttime);

SmellDispFlag=0;

CanFindFood=0;

HardtoFindPath=0;

voidBlockInitial(void)

inti,j;

intbn;

block[i][j]=0;

bn=1+MAX_BLOCK/2+random(MAX_BLOCK/2);

=bn;

i++)CreatBlock();

voidCreatBlock(void)

intx1,y1,x2,y2;

intdx,dy;

x1=random(MAXX)+1;

y1=random(MAXY)+1;

dx=random(MAXX/10)+1;

dy=random(MAXY/10)+1;

x2=x1+dx;

y2=y1+dy;

if(x2>

MAXX)x2=MAXX;

if(y2>

MAXY)y2=MAXY;

if(food.xxx>

=x1&

food.xxx<

=x2&

food.yyy>

=y1&

food.yyy<

=y2)return;

if(home.xxx>

home.xxx<

home.yyy>

home.yyy<

for(i=x1;

=x2;

for(j=y1;

=y2;

block[i][j]=1;

gotoxy(i,j);

putch(BLOCK_CHAR);

voidSaveBlock(void)

FILE*fp_block;

charFileNameBlock[20];

Savetofile..."

FileNameBlock);

gets(FileNameBlock);

if(FileNameBlock[0]==0)strcpy(FileNameBlock,"

Ant.ant"

elsestrcat(FileNameBlock,"

.ant"

if((fp_block=fopen(FileNameBlock,"

wb"

))==NULL)

{gotoxy(1,MAXY+1);

Creatfile%sfail..."

exit

(2);

fputc(home.xxx,fp_block);

fputc(home.yyy,fp_block);

fputc(food.xxx,fp_block);

fputc(food.yyy,fp_block);

fputc(block[i][j],fp_block);

fclose(fp_block);

voidLoadBlock(void)

inti,j,k;

Loadfile..."

rb"

Openfile%sfail..."

home.xxx=fgetc(fp_block);

home.yyy=fgetc(fp_block);

food.xxx=fgetc(fp_block);

food.yyy=fgetc(fp_block);

gotoxy(home.xxx,home.yyy);

putch(HOME_CHAR);

gotoxy(food.xxx,food.yyy);

putch(FOOD_CHAR);

food.amount=random(MAX_FOOD/3)+2*MAX_FOOD/3+1;

/*food.amount=MAX_FOOD;

*/

home.amount=0;

home.TargetFood=(food.amount<

TARGET_FOOD)?

food.amount:

TARGET_FOOD;

block[i][j]=fgetc(fp_block);

if(block[i][j])

voidHomeFoodInitial(void)

intrandnum;

inthomeplace;

/*1--homeatleft-up,foodatright-down

2--homeatleft-down,foodatright-up

3--homeatright-up,foodatleft-down

4--homeatright-down,foodatleft-up*/

randnum=random(100);

if(randnum<

25)homeplace=1;

elseif(randnum>

=25&

randnum<

50)homeplace=2;

=50&

75)homeplace=3;

elsehomeplace=4;

switch(homeplace)

case1:

home.xxx=random(MAXX/3)+1;

home.yyy=random(MAXY/3)+1;

food.xxx=random(MAXX/3)+2*MAXX/3+1;

food.yyy=random(MAXY/3)+2*MAXY/3+1;

break;

case2:

home.yyy=random(MAXY/3)+2*MAXY/3+1;

food.yyy=random(MAXY/3)+1;

case3:

home.xxx=random(MAXX/3)+2*MAXX/3+1;

food.xxx=random(MAXX/3)+1;

case4:

/*datacorrectnesscheck*/

if(home.xxx<

=0||home.xxx>

MAXX||home.yyy<

=0||home.yyy>

MAXY||

=0||food.xxx>

MAXX||food.yyy<

=0||food.yyy>

food.amount<

=0)

Worldinitialfail,anykeytoexit..."

gotoxy(food.

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2