有关迷宫问题的~~帮忙看一下啊~
#include<stdio.h>#include<stdlib.h>
#define Maxsize 50
#define M = N = 8//定义迷宫的大小
typedef struct
{
int i,j;
int pre;
}Qu[Maxsize];//定义结构体
int front = -1,rear = -1;
int mg[M+2][N+2]=
{
{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}
};
int mgpath(int xi,int yi,int xe,int ye);
void print(int front);
int main()
{
mgpath(1,1,M,N);
system("pause");
return 0;
}
int mgpath(int xi,int yi,int xe,int ye)//寻找路径
{
int i,j;
int find = 0;
int di;//定义方位
rear++;
Qu[rear].i = xi;Qu[rear].j = yi;
Qu[rear].pre = -1;
mg[i][j] = -1;
while(front<=rear && ! find)
{
front++;
i = Qu[front].i;
j = Qu[front].j;
if(i == xe && j = ye)
{
find = 1;
print(front);
return(1);
}
for(di = 0; di<4; di++)
{
switch(di)//不同的方向顺时针
{
case 0:i = Qu[front].i-1; j = Qu[front].j;break;
case 1:i = Qu[front].i; j = Qu[front].j+1;break;
case 2:i = Qu[front].i+1;j = Qu[front].j;break;
case 3:i = Qu[front].i;j = Qu[front].j-1;break;
}
if(mg[i][j] == 0)
{
rear++;
Qu[rear].i = i;
Qu[rear].j = j;
Qu[rear],pre = front;
mg[i][j] = -1;
}
}
}
return (0);
}
void print(int front)//迷宫的输出
{
int k = front;
int j, ns = 0;// 每5个一行
printf("\n");
do
{
j = k;
k = Qu[k].pre;
Qu[j].pre = -1;
}while(k!=0);
printf("迷宫的路径如下:\n");
k = 0;
while(k<Maxsize)
{
if(Qu[k].pre == -1)
{
ns++;
printf("\t(%d,%d)", Qu[k].i,Qu[k].j);
if(ns%5 == 0)
printf("\n");
}
k++;
}
printf("\n");
}