C语言 自动生成查找迷宫最短路径的代码

#include
#include
#include
#include
#include
using namespace std;

#define OVERFLOW 0
#define OK 1
#define ERROE 0
#define TRUE 1
#define FALSE 0
#define SIZE 102//迷宫的最大范围
typedef int Status;

typedef struct{
int x;
int y;
}PosType;//坐标位置
typedef struct {
PosType seat; //通道块在迷宫中的"坐标位置"
int di; //从上一通道块走向此通道块的"方向"
}SElemType;


void Random(int (*mg)[SIZE],int size,PosType start,PosType end);/*随机生成迷宫的函数
/*为了能够让尽量能通过,将能通过的块和不能通过的块数量比大致为3:1*/

Status Pass(PosType e,int (*mg)[SIZE]);//当前块可否通过
Status FootPrint(PosType e,int (*mg)[SIZE]);//留下通过的足迹
PosType NextPos(PosType e,int dir);//下一步
Status Equal(PosType e1,PosType e2);//e1与e2的位置坐标是否相同
Status MarkPath(PosType e,int (*mg)[SIZE],int di);//对最短可行路径上的“通道块”进行标记
PosType FrontPos(PosType e,int dir);//寻找当前通道块的上一步的位置
Status PathPrint(stack s,int (*mg)[SIZE]);//迷宫最短路径的标记
Status PathClean(int (*mg)[SIZE],stack s);//路径清除
Status MazePath(PosType start,PosType end,int (*mg)[SIZE],stack &s);/*迷宫函数
/* 若迷宫maze中从入口 start到出口 end的通道,则求得一条存放在栈中
/* 并返回TRUE;否则返回FALSE*/

void PrintMaze(int (*mg)[SIZE],int size);//打印迷宫
Status Check(char &choice);//确认输入正确

int main(){

stack s;
int mg[SIZE][SIZE]={1},size;
PosType start,end;
char choice;

system("mode con cols=220 lines=220");
printf("\n==================迷宫最短路径游戏==================");
printf("\n说明:■不能走的区域");
printf("\n '空格'代表可通过的区域");
printf("\n默认起点为左上角位置,默认终点为右下角位置\n");
printf("\n============================================\n");

printf("请输入迷宫边长(3~%d),系统将为你产生一个随机迷宫:",SIZE-2);
scanf("%d",&size);
while((size>SIZE-2)||(size<1))
{
printf("输入有误!\n");
printf("请输入迷宫边长(3~%d),系统将为你产生一个随机迷宫:",SIZE-2);
scanf("%d",&size);
}
size+=2;//补上外围
getchar();//跳过'\n'
start.x=1;start.y=1; //起点坐标
end.x=size-2;end.y=size-2; //终点坐标

Random(mg,size,start,end);
PrintMaze(mg,size);

while(!((choice=='Q')||(choice=='q')))
{
printf("是否使用该迷宫?(y/n)\n");
Check(choice);
if((choice=='Y')||(choice=='y'))
{
PathClean(mg,s);
}

while((choice=='n')||(choice=='N'))
{
while(!s.empty())
s.pop();

choice=' ';
printf("请输入迷宫边长(3~%d),系统将为你产生一个随机迷宫:",SIZE-2);
scanf("%d",&size);
while((size>SIZE-2)||(size<1))
{
pr

intf("输入有误!\n");
printf("请输入迷宫边长(3~%d),系统将为你产生一个随机迷宫:",SIZE-2);
scanf("%d",&size);
}
size+=2;//补上外围
start.x=1;start.y=1;//起点坐标
end.x=size-2;end.y=size-2; //终点坐标
getchar();//跳过'\n'
Random(mg,size,start,end);
PrintMaze(mg,size);
printf("是否使用该迷宫?(y/n)\n");
Check(choice);
}

printf("是否人工选择起点和终点(y/n)?【默认:起点(1,1),终点(%d,%d)】\n",size-2,size-2);
Check(choice);
if((choice=='y')||(choice=='Y'))
{
printf("请输入“起点”坐标(1~%d)用空格分隔:",size-2);
scanf("%d %d",&start.x,&start.y);
while(((start.x>size-2)||start.x<1)||((start.y>size-2)||(start.y<1))||!Pass(start,mg))
{
if(!Pass(start,mg)) printf("些位置不能为“起点”!\n");
else printf("输入有误!\n");
printf("请输入“起点”坐标(1~%d)用空格分隔:",size-2);
scanf("%d %d",&start.x,&start.y);
}
printf("请输入“终点”坐标(1~%d)用空格分隔:",size-2);
scanf("%d %d",&end.x,&end.y);
while(((end.x>size-2)||end.x<1)||((end.y>size-2)||(end.y<1))||!Pass(end,mg)||Equal(start,end))
{
if(!Pass(end,mg)) printf("些位置不能为“终点”!\n");
else if(Equal(start,end)) printf("该位置已为起点!\n");
else printf("输入有误!\n");
printf("请输入“终点”坐标(1~%d)用空格分隔:",size-2);
scanf("%d %d",&end.x,&end.y);
}

getchar();//跳过'\n'
}
MazePath(start,end,mg,s);
PrintMaze(mg,size);

printf("退出游戏请输入\"Q\"否则继续游戏!\n");
choice=getchar();
getchar();//跳过'\n'
}

printf("\n==========程序退出,感谢使用!==========\n");
return 0;
}

void Random(int (*mg)[SIZE],int size,PosType start,PosType end)
{
int i,j,k;
srand(time(NULL));
for(j=0;jmg[0][j]=mg[size-1][j]=1; /*设置迷宫外围"不可走",保证只有一个出口和入口*/

for(i=1;img[i][0]=mg[i][size-1]=1; /*设置迷宫外围"不可走",保证只有一个出口和入口*/

for(i=1;ifor(j=1;jk=rand()%4; //随机生成0、1、2、4三个数
if(k)
mg[i][j]=0;
else{
mg[i][j]=1;
}//else
}
mg[start.y][start.x]=0;
mg[end.y][end.x]=0; //将入口、出口设置为"0"即可通过
}

Status Pass(PosType e,int (*mg)[SIZE])
{
if (mg[e.y][e.x]==0) //0时可以通过
return OK; // 如果当前位置是可以通过,返回1
return OVERFLOW; // 其它情况返回0
}

Status FootPrint(PosType e,int (*mg)[SIZE])
{
mg[e.y][e.x]=7;
return OK;
}

PosType NextPos(PosType e,int dir)
{
PosType E;
switch(dir){
case 1:E.x=e.x+1; //向右
E.y=e.y;
break;
case 2:E.x=e.x; //向下
E.y=e.y+1;
break;
case 3:E.x=e.x-1; //向左
E.y=e.y;
break;
case 4:E.x=e.x; //向上
E.y=

e.y-1;
break;

}
return E;
}

Status Equal(PosType e1,PosType e2)
{
if((e1.x==e2.x)&&(e1.y==e2.y))
return TRUE;
return FALSE;
}

Status MarkPath(PosType e,int (*mg)[SIZE],int di)
{
switch(di)
{
case 1://向右
mg[e.y][e.x]=11;
break;
case 2://向下
mg[e.y][e.x]=12;
break;
case 3://向左
mg[e.y][e.x]=13;
break;
case 4://向上
mg[e.y][e.x]=14;
break;
}
return OK;
}

PosType FrontPos(PosType e,int dir)
{
PosType E;
switch(dir){
case 1:E.x=e.x-1; //向左
E.y=e.y;
break;
case 2:E.x=e.x; //向上
E.y=e.y-1;
break;
case 3:E.x=e.x+1; //向右
E.y=e.y;
break;
case 4:E.x=e.x; //向下
E.y=e.y+1;
break;
}
return E;
}

Status PathPrint(stack s,int (*mg)[SIZE])
{
SElemType e,front,tail;
int di;

e=s.top();
tail=e;
s.pop();
MarkPath(e.seat,mg,1);
while(!s.empty())
{
front=s.top();
s.pop();
if(Equal(front.seat,FrontPos(e.seat,e.di)))
{
di=e.di;
e=front;
MarkPath(e.seat,mg,di);
}
}
mg[tail.seat.y][tail.seat.x]=20;
mg[e.seat.y][e.seat.x]=10;
return OK;
}

Status PathClean(int (*mg)[SIZE],stack s)
{
SElemType e;
while(!s.empty())
{
e=s.top();
s.pop();
mg[e.seat.y][e.seat.x]=0;
}
return OK;
}

Status MazePath(PosType start,PosType end,int (*mg)[SIZE],stack &s)
{
queue q;
SElemType e;
int di=0;

e.di=di;
e.seat=start;// 设定"当前位置"为"入口位置"
q.push(e);
s.push(e);
do
{
e=q.front();
q.pop();
for(di=1;di<=4;di++)
{
e.seat=NextPos(e.seat,di);
e.di=di;
if(Pass(e.seat,mg))
{
q.push(e);
s.push(e);
FootPrint(e.seat,mg);
if(Equal(e.seat,end))
{
PathPrint(s,mg);
return TRUE;
}
}
e.seat=FrontPos(e.seat,di);
}
}while(!q.empty());
printf("\n\n囧 ! 不能到达终点!");
return FALSE;
}

void PrintMaze(int (*mg)[SIZE],int size)
{
int i,j;

printf("\n");
for(i=0;ifor(j=0;jswitch(mg[i][j])
{
case 0:
case 7:
printf(" ");
break;
case 1:
printf("■");
break;
case 10:
printf("起");
break;
case 20:
printf("终");
break;
case 11:
printf("→");
break;
case 12:
printf("↓");
break;
case 13:
printf("←");
break;
case 14:
printf("↑");
break;
}
}
printf("\n");
}
printf("\n");
}

Status Check(char &choice)
{
while(!(((choice=getchar())=='y')||(choice=='n')||(choice=='Y')||(choice=='N')))//非正确输入
{
if(choice!='\n')
{
printf("请输入确定选择(y/n)\n");
getchar();
}
}
getchar();//跳过'\n'
return OK;
}

相关文档
最新文档