本文共 4713 字,大约阅读时间需要 15 分钟。
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
#include
#include
#define N 20
int oldmap[N][N];
int yes=0;
int way[100][2],wayn=0;
void Init(void);
void Close(void);
void DrawPeople(int *x,int *y,int n);
void PeopleFind(int (*x)[N]);
void WayCopy(int (*x)[N],int (*y)[N]);
int FindWay(int (*x)[N],int i,int j);
void MapRand(int (*x)[N]);
void PrMap(int (*x)[N]);
void Result(void);
void Find(void);
void NotFind(void);
void main(void)/
{
int map[N][N];
char ch;
clrscr();
printf("\n Please select hand(1) else auto\n");
scanf("%c",&ch);
Init();
MapRand(map);
PrMap(map);
if(ch=='1')
PeopleFind(map);
else
FindWay(map,10,10);
Result();
Close();
}
void Init(void)
{
int gd=DETECT,gm;
registerbgidriver(EGAVGA_driver);
Error:Graphics
not initialized (use 'initgraph')
initgraph(&gd,&gm,"c:\\tc");
}
void DrawPeople(int *x,int *y,int n)
{
switch(n)/*判断x,y的变化,8个方向的变化*/
{
case 1: (*x)--;break; /*上*/
case 2: (*x)--;(*y)++;break ;/*右上*/
case 3: (*y)++;break; /*右*/
case 4: (*x)++;(*y)++;break; /*右下*/
case 5: (*x)++;break; /*下*/
case 6: (*x)++;(*y)--;break; /*左下*/
case 7: (*y)--;break; /*左*/
case 8: (*x)--;(*y)--;break; /*左上*/
}
setfillstyle(SOLID_FILL,RED);
bar(100+(*y)*15-6,50+(*x)*15-6,100+(*y)*15+6,50+(*x)*15+6);
}
void PeopleFind(int (*map)[N])/*人工手动查找*/
{
int x,y;
char c=0;
x=y=10;
setcolor(11);
line(500,200,550,200);
outtextxy(570,197,"d");
line(500,200,450,200);
outtextxy(430,197,"a");
line(500,200,500,150);
outtextxy(497,130,"w");
line(500,200,500,250);
outtextxy(497,270,"x");
line(500,200,450,150);
outtextxy(445,130,"q");
line(500,200,550,150);
outtextxy(550,130,"e");
line(500,200,450,250);
outtextxy(445,270,"z");
line(500,200,550,250);
outtextxy(550,270,"c");
setcolor(YELLOW);
outtextxy(420,290,"Press 'Enter' to end");
setfillstyle(SOLID_FILL,RED);
bar(100+y*15-6,50+x*15-6,100+y*15+6,50+x*15+6);
while(c!=13)
{
c=getch();
if(c=='w'&&map[x-1][y]!=1)
DrawPeople(&x,&y,1);/*上*/
else
if(c=='e'&&map[x-1][y+1]!=1)
DrawPeople(&x,&y,2);/*右上*/
else
if(c=='d'&&map[x][y+1]!=1)
DrawPeople(&x,&y,3);/*右*/
else
if(c=='c'&&map[x+1][y+1]!=1)
DrawPeople(&x,&y,4);/*右下*/
else
if(c=='x'&&map[x+1][y]!=1)
DrawPeople(&x,&y,5);/*下*/
else
if(c=='z'&&map[x+1][y-1]!=1)
DrawPeople(&x,&y,6); /*左下*/
else
if(c=='a'&&map[x][y-1]!=1)
DrawPeople(&x,&y,7); /*左*/
else if(c=='q'&&map[x-1][y-1]!=1)
DrawPeople(&x,&y,8); /*左上*/
}
setfillstyle(SOLID_FILL,WHITE);
bar(100+y*15-6,50+x*15-6,100+y*15+6,50+x*15+6);
if(x==N-2&&y==N-2)
yes=1;
}
void WayCopy(int (*oldmap)[N],int (*map)[N])
{
int i,j;
for(i=0;i
for(j=0;j
oldmap[i][j]=map[i][j];
}
int FindWay(int (*map)[N],int i,int j)
{
if(i==N-2&&j==N-2)
{
yes=1;
return;
}
map[i][j]=1;
WayCopy(oldmap,map);
if(oldmap[i+1][j+1]==0&&!yes)
{
FindWay(oldmap,i+1,j+1);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
WayCopy(oldmap,map);
if(oldmap[i+1][j]==0&&!yes) {
FindWay(oldmap,i+1,j);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
WayCopy(oldmap,map);
if(oldmap[i][j+1]==0&&!yes)
{
FindWay(oldmap,i,j+1);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
WayCopy(oldmap,map);
if(oldmap[i-1][j]==0&&!yes)/*判断上方是否可以走*/
{
FindWay(oldmap,i-1,j);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
WayCopy(oldmap,map);
if(oldmap[i-1][j+1]==0&&!yes)
{
FindWay(oldmap,i-1,j+1);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
WayCopy(oldmap,map);
if(oldmap[i+1][j-1]==0&&!yes)
{
FindWay(oldmap,i+1,j-1);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
WayCopy(oldmap,map);
if(oldmap[i][j-1]==0&&!yes)
{
FindWay(oldmap,i,j-1);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
WayCopy(oldmap,map);
if(oldmap[i-1][j-1]==0&&!yes)
{
FindWay(oldmap,i-1,j-1);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
return;
}
void MapRand(int (*map)[N])
{
int i,j;
cleardevice();
randomize();
for(i=0;i
{
for(j=0;j
{
if(i==0||i==N-1||j==0||j==N-1)
map[i][j]=1;
else
if(i==1&&j==1||i==N-2&&j==N-2)
map[i][j]=0;
else
map[i][j]=random(2);
}
}
}
void PrMap(int (*map)[N])
{
int i,j;
for(i=0;i
for(j=0;j
if(map[i][j]==0)
{
setfillstyle(SOLID_FILL,WHITE);/*白色为可走的路*/
bar(100+j*15-6,50+i*15-6,100+j*15+6,50+i*15+6);
}
else
{
setfillstyle(SOLID_FILL,BLUE);
bar(100+j*15-6,50+i*15-6,100+j*15+6,50+i*15+6);
}
}
void Find(void)/*找到通路*/
{
int i;
setfillstyle(SOLID_FILL,RED);/*红色输出走的具体路线*/
wayn--;
for(i=wayn;i>=0;i--)
{
bar(100+way[i][1]*15-6,50+way[i][0]*15-6,100+
way[i][1]*15+6,50+way[i][0]*15+6);
sleep(1);
}
bar(100+(N-2)*15-6,50+(N-2)*15-6,100+
(N-2)*15+6,50+(N-2)*15+6);
setcolor(GREEN);
settextstyle(0,0,2);
outtextxy(130,400,"Find a way!");
}
void NotFind(void)
{
setcolor(GREEN);
settextstyle(0,0,2);
outtextxy(130,400,"Not find a way!");
}
void Result(void)
{
if(yes)
Find();
else
NotFind();
getch();
}
void Close(void)
{
closegraph();
}
转载地址:http://gbtnx.baihongyu.com/