高分跪求c++迷宫求解问题 (要用深度优先和广度优先两种方法)

2024-11-26 18:46:29
推荐回答(1个)
回答1:

很久以前做的,现在都忘记了。你看一下吧。我的报告,也可以发给你,不过还是期望你自己可以学会怎么做,少年智则国智。C++和C一回事,你把其中的语法规则改一下就好,比如输入输出语句,将结构体改成类,也可不改,都支持的。

#include "stdio.h"
#include "conio.h" /*provide the support of getch */
#define max 10
#define maxlength (max*max) /*the max lenth of the path*/
#define zdir 4 /*the total direction*/
#include "graphics.h" /*provide the support of graphics*/
#include "dos.h" /* provide the support of function:sleep */
#include "time.h" /* provide the support of sleep */
#define M 10 /*the max lenth of the map*/
#define N 10 /*the max lenth of the map*/
/*the following four function are stating functions*/
void printmap(void);
int zfindpath(int row,int col,int erow,int ecol);
void creategraphich(int n1,int n2,int n3,int n4);
void creategraphich(int n1,int n2,int n3,int n4);
int zmap[max][max]={ {1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};

struct {
int row,col;
}offsets[zdir]={{0,1 },
{1,0 },
{0,-1},
{-1,0}
};
struct {
int row;
int col;
} path[maxlength];
/*define a globle variable to convenience output*/
int len;

int zfindpath(int row,int col,int erow,int ecol)
{
int dir,row2,col2;
/* int x=150+col*22,y=100+row*22; */
sleep(1);
setcolor(10); /* draw a cirlce */
setfillstyle(1, 14);
circle(150+(col)*22+10,100+(row)*22+10, 8);
floodfill(150+(col)*22+10+4, 100+(row)*22+10+4, 10);
if((row==erow)&&(col==ecol))
{ path[0].row=erow;
path[0].col=ecol; /* judge if is the exit */
return 1;
} /* the end of if */
zmap[row][col]=2; /* block current position */
for(dir=0;dir {
row2=row+offsets[dir].row; /* step to next space */
col2=col+offsets[dir].col;
if(zmap[row2][col2]==0) /* mean the way could go at this stage */
{

len=zfindpath(row2,col2,erow,ecol);
if(len>0)
{
path[len].row=row2; /* use recursion to find the path */
path[len].col=col2;
return ++len;
} /*the end of if */
} /*the end of if */

} /* the end of for */
sleep(1);
setfillstyle(1, 15);
bar( 150+col*22,100+row*22,170+col*22,120+row*22);
/* sleep(1); */
return 0;

} /* the end of zfindpaht */

void printmap() /* first ,print the map that we will go */
{
int i,j;
for(i=0;i {
for(j=0;j printf(" %d",zmap[i][j]);
printf("\n");
}
getch();

}

void createmap(int n1,int n2,int n3,int n4)

{
int i,j,size,trow,tcol,x=150+n2*22,y=100+n1*22;
void *buf;
int gdriver=DETECT,gmode=VGAHI;
initgraph(&gdriver,&gmode,"c:\\tc");
cleardevice();
setbkcolor(10);
setcolor(1);
settextstyle(4, 0, 5);
outtextxy(70,20,"hello,welcome to here! ");
setcolor(4);
setfillstyle(1, 4);
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
if(zmap[i][j]==1) {setfillstyle(1, 4); bar( 150+j*22,100+i*22,170+j*22,120+i*22);/*sleep(1);*/}
else {setfillstyle(1, 15); bar( 150+j*22,100+i*22,170+j*22,120+i*22);/*sleep(1);*/}

}

line(100,80,150+n2*22,100+n1*22); /* draw a line */
line(337+(n4-8)*22,287+(n3-8)*22,337,360);
setcolor(1);
settextstyle(4, 0, 3);
outtextxy(80,60,"this is the entrance! ");
outtextxy(320,360,"this is the exit! ");
}

void creategraphich(int n1,int n2,int n3,int n4)
{
int i,j,size,trow,tcol,x=150+n2*22,y=100+n1*22;
void *buf;
int gdriver=DETECT,gmode=VGAHI;
initgraph(&gdriver,&gmode,"c:\\tc");
cleardevice();
setbkcolor(10);
setcolor(1);
settextstyle(4, 0, 5);
outtextxy(70,20,"hello,welcome to here! ");
setcolor(4);
setfillstyle(1, 4);
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
if(zmap[i][j]==1) {setfillstyle(1, 4); bar( 150+j*22,100+i*22,170+j*22,120+i*22);/*sleep(1);*/}
else {setfillstyle(1, 15); bar( 150+j*22,100+i*22,170+j*22,120+i*22);/*sleep(1);*/}

}

line(100,80,150+n2*22,100+n1*22); /* draw a line */
line(337+(n4-8)*22,287+(n3-8)*22,337,360);
setcolor(1);
settextstyle(4, 0, 3);
outtextxy(80,60,"this is the entrance! ");
outtextxy(320,360,"this is the exit! ");
setcolor(4); /* draw a circle heart */
setfillstyle(1, 4);
sector(250,350,0,180,20,20); /* draw the upper circle */
sector(290,350,0,180,20,20); /* draw the downer circle */
sector(310,350,180,240,80,76);
sector(230,350,300,360,80,76);
setcolor(10); /* draw a cirlce */
setfillstyle(1, 14);
circle(150+(n2)*22+10,100+(n1)*22+10, 8);
floodfill(150+(n2)*22+10+4, 100+(n1)*22+10+4, 10);
size=imagesize(150+(n2)*22+10-8, 100+(n1)*22+10-8, 150+(n2)*22+10+8, 100+(n1)*22+10+8);
buf=malloc(size);
getimage(150+(n2)*22+10-8, 100+(n1)*22+10-8, 150+(n2)*22+10+8, 100+(n1)*22+10+8,buf);
/*sleep(2);
putimage(192,122 , buf, COPY_PUT); */
for(i=len;i>0;i--) /* the total number of the path is len-1 */
{
trow=path[i-1].row-path[i].row;
tcol=path[i-1].col-path[i].col;
sleep(1);
x=x+tcol*22;
y=y+trow*22;
putimage(x,y, buf, COPY_PUT);

}
/* printf the result*/
setcolor(1);
settextstyle(4, 0, 3);
outtextxy(357,287,"sussessfully!!!");
getch();
closegraph();
free((void *)buf);
}

void changemap(int d)

{
FILE *fw;
int i,j;
if(d==1)
{
fw=fopen("c:\\tc\\a.txt","r");
if(!fw) {printf("not found!"); getch();return;}
for(i=0;i { for( j=0;j
} /* the end of for */
}/* the end of first if*/
else {
fw=fopen("c:\\tc\\b.txt","r");
if(!fw) {printf("not found!"); getch();return;}
for(i=0;i { for( j=0;j
} /* the end of for */
} /* the end of else */
printf("the map we will follow to search :\n");
for(i=0;i { for(j=0;j printf("\n");
}

getch();
fclose(fw);
}

main()
{
while(1){
int i,j,n1,n2,n3,n4,n5,n6;
int zch;
/* while(1){ */
len=0;
printf("if you want to use the default map,press 1,if you want to use the spare map,press 2 :\n");
scanf("%d",&zch);
if(zch==2)
changemap(1);
/*printmap(); */
else
{
changemap(2);
/* printmap(); */ /* first ,print the map that we will go */
}
printf("please input the startpoint(row,col):\n"); /* input the start point and end point ,those two value should less then 10 and larger then 0*/
scanf("%d%d",&n1,&n2);
printf("please input the endpoint(row,col):\n");
scanf("%d%d",&n3,&n4);
n5=n1;
n6=n2;
createmap(n5,n6,n3,n4); /* in this step ,we will create the primitive map */
zfindpath(n1,n2,n3,n4); /* function of searching the path */
path[len].row=n5;
path[len].col=n6;
getch();
closegraph();
printf("the resultant path is \n");
for(i=len;i>0;i--)
{
printf("(%d,%d)\n",path[i].row,path[i].col);
} /* the end of for*/

getch();
printf("now,we will create the final graphics of the map:\n"); /* create the final graphics of the map*/
getch();
creategraphich(n5,n6,n3,n4);
} /* the end of while */

} /* the end of main */