[c]代码库
typedef struct
{
int x,y;
int pre;
} sqtype;
sqtype sq[num];
int front,rear;
void path ( maze,move )
int maze[m][n];/*迷宫数组*/
item move[8];/*坐标增量数组*/
{ sqtype sq[NUM];
int front,rear;
int x,y,i,j,v;
front=rear=0;
sq[0].x=1;
sq[0].y=1;
sq[0].pre=-1; /*入口点入队*/
maze[1,1]=-1;
while ( front<=rear ) /*队列不空*/
{
x=sq[front].x ;
y=sq[front ].y ;
for ( v=0; v<8; v++ )
{
i=x+move[v].x;
j=x+move[v].y;
if ( maze[i][j]==0 )
{
rear++;
sq[rear].x=i;
sq[rear].y=j;
sq[rear].pre=front;
maze[i][j]=-1;
}
if ( i==m&&j==n )
{
printpath ( sq,rear ); /*打印迷宫*/
restore ( maze ); /*恢复迷宫*/
return 1;
}
} /*for v*/
front++; /*当前点搜索完,取下一个点搜索*/
} /*while*/
return 0;
} /*path*/
void printpath ( sqtype sq[],int rear ) /*打印迷宫路径*/
{
int i;
i=rear;
do
{
printf ( " ( %d,%d ) --",sq[i].x , sq[i].y ) ;
i=sq[i].pre; /*回溯*/
}
while ( i!=-1 );
} /*printpath*/
[代码运行效果截图]