这个是以前的课程设计:
#include<iostream>
#include<ctime>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define M 8
#define N 8
#define MaxSize M*N
typedef int Status;
int mg[M+2][N+2];
struct
{
int i,j,pre;
}Qu[MaxSize];
int front=-1,rear=-1;
Status MazPath(int xi,int yi,int xe,int ye)
{
int m,n,find=0,di;
rear++;
Qu[rear].i=xi;
Qu[rear].j=yi;
Qu[rear].pre=-1;
mg[1][1]=-1;
while(front<=rear&&!find)
{
front++;
m=Qu[front].i;
n=Qu[front].j;
if(m==xe&&n==ye)
{
find=1;return OK;
}
for(di=0;di<=3;di++)
{
switch(di)
{
case 0:m=Qu[front].i-1;n=Qu[front].j;break;
case 1:m=Qu[front].i;n=Qu[front].j+1;break;
case 2:m=Qu[front].i+1;n=Qu[front].j;break;
case 3:m=Qu[front].i;n=Qu[front].j-1;break;
}
if(mg[m][n]==0)
{
rear++;
Qu[rear].i=m;
Qu[rear].j=n;
Qu[rear].pre=front;
mg[m][n]=-1;
}
}
}
return ERROR;
}
void output(int front)
{
int k=front,n,ns=0;
do
{
n=k;
k=Qu[k].pre;
Qu[n].pre=-1;
}while(k!=0);
k=0;
while(k<MaxSize)
{
if(Qu[k].pre==-1)
{
ns++;
cout<<"("<<Qu[k].i<<","<<Qu[k].j<<")"<<' ';
if(ns%5==0)cout<<endl;
}
k++;
}
}
int main()
{
srand((unsigned) time(NULL));
int m,n;
cout<<"选择操作,\n1--->自己输入迷宫\n2--->由系统产生迷宫\n";
int func;
cin>>func;
if(func==1)
{
cout<<"请输入迷宫"<<M<<'*'<<N<<endl;
for(m=1;m<=M;m++)
{
for(n=1;n<=N;n++)
cin>>mg[m][n];
}
}
if(func==2)
{
for(m=1;m<=M;m++)
{
for(n=1;n<=N;n++)
mg[m][n]=rand()%2;
}
}
for(m=0;m<M+2;m++)
{
mg[m][0]=mg[m][N+1]=1;
}
for(n=1;n<N+1;n++)
{
mg[0][n]=mg[M+1][n]=1;
}
mg[1][1]=0;mg[M][N]=0;
if(func==2)
{
cout<<"迷宫如下"<<endl;
for(m=1;m<M+1;m++)
{
for(n=1;n<N+1;n++)
cout<<mg[m][n]<<' ';
cout<<endl;
}
}
if(MazPath(1,1,M,N))
{
cout<<"路径如下:"<<endl;
output(front);
}
else cout<<"无路径可出迷宫"<<endl;
system("pause");
return OK;
}