c语言编写迷宫的问题 c语言
答案:1 悬赏:0 手机版
解决时间 2021-02-04 22:32
- 提问者网友:相思似海深
- 2021-02-04 05:10
c语言编写迷宫的问题 c语言
最佳答案
- 五星知识达人网友:往事隔山水
- 2021-02-04 05:31
#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,1,1);
Result();
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
}
void DrawPeople(int *x,int *y,int n)
{
setfillstyle(SOLID_FILL,WHITE);
bar(100+(*y)*15-6,50+(*x)*15-6,100+(*y)*15+6,50+(*x)*15+6);
switch(n)
{
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=1;
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
}
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
{
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();
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯