void Zprintpath(Sqstack s)//输出迷宫路径,栈中保存的是一条迷宫的路径
{
elemtype temp;
printf("(%d,%d)<--",M,N);
while(!Stackempty(s))
{
pop(&s,&temp);
printf("(%d,%d)<--",temp.x,temp.y);
}printf("\n");
}
void Zmazepath(int maze[M+2][N+2],item move[8]) //栈的迷宫求解输出
{
Sqstack s;
elemtype temp;int x,y,d,i,j;
InitStack(&s);//栈的初始化
temp.x=1;temp.y=1;temp.d=-1;
push(&s,temp);
while(!Stackempty (s))
{
pop(&s,&temp);
x=temp.x;y=temp.y;d=temp.d+1;
while(d<8)
{
i=x+move[d].x;
j=y+move[d].y;
if(maze[i][j]==0)
{ temp.x=x;temp.y=y;temp.d=d;
push(&s,temp);
x=i;y=j;
maze[x][y]=-1;
if(x==M&&y==N)
{Zprintpath(s);
return;
}
else d=0;
}//if
else d++;
}//while
}//while
return;
printf("迷宫无路径\n");return;
}
void DLprintpath(SqQueue q)//输出迷宫路径,队列中保存的是一条迷宫的路径
{
int i; i=q.rear-1;
do
{
printf("(%d,%d)<--",(q.elem[i]).x,(q.elem[i]).y);
i=(q.elem[i]).pre;
} while(i!=-1);
printf("\n");
}
void DLmazepath(int maze1[M+2][N+2],item move[8]) //队列的迷宫求解
{
SqQueue q;
Elemtype head,e;
int x,y,v,i,j;
InitQueue(&q); //队列初始化
e.x=1;e.y=1;e.pre=-1;
EnQueue (&q,e);
maze1[1][1]=-1;
while(!QueueEmpty (q))
{
GetHead(q,&head);
x=head.x;y=head.y;
for(v=0;v<8;v++)
{
i=x+move[v].x;
j=y+move[v].y;
if(maze1[i][j]==0)
{
e.x=i;e.y=j;e.pre=q.front;
EnQueue(&q,e);
maze1[x][y]=-1;
} //if
if(i==M&&j==N)
{
DLprintpath(q);
return ;
}
}//for
DeQueue(&q,&head);
}//while
printf("迷宫无路径!\n");
return;
}
void main()
{int a,i,j,maze2[M+2][N+2];
int maze[M+2][N+2]={
{1,1,1,1,1,1,1,1,1,1},
{1,0,1,1,1,0,1,1,1,1},
{1,1,0,1,0,1,1,1,1,1},
{1,0,1,0,0,0,0,0,1,1},
{1,0,1,1,1,0,1,1,1,1},
{1,1,0,0,1,1,0,0,0,1},
{1,0,1,1,0,0,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1}}
item move[8]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
;/*构造一个迷宫*/
/*坐标增量数组 move 的初始化*/
for(i=0;i