第1个回答 2013-05-11
/* 迷宫矩阵
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 1 0 0 0 1
1 1 1 0 0 1 0 1 0 1
1 0 0 0 1 0 0 0 0 1
1 0 0 0 0 1 0 1 0 1
1 1 0 1 0 0 0 0 0 1
1 0 0 1 1 1 0 1 1 1
1 1 0 0 0 1 0 0 0 1
1 1 1 1 1 1 1 1 1 1
*/
#include<stdio.h>
#define m 7
#define n 8
void path()
{
int maze[m+2][n+2] ;
int move[4][2]={ {0,-1},{-1,0},{0,1},{1,0} };
int s[54][3];
int top=0;
int i,j,k,f=0;
int g,h,p;
for(i=0;i<m+2;++i)
for(j=0;j<n+2;++j)
scanf("%d",&maze[i][j]);
maze[1][1]=2;
s[top][0]=1;
s[top][1]=1;
s[top][2]=0;
++top;
while(top!=0&&f==0)
{
--top;
i=s[top][0];
j=s[top][1];
k=s[top][2];
while(k<4)
{
g=i+move[k][0];
h=j+move[k][1];
if(g==m&&h==n&&maze[g][h]==0)
{
for(p=0;p<top;++p)
printf("%3d,%d\n",s[p][0],s[p][1]);
printf("%3d,%d\n",i,j);
printf("%3d,%d\n",m,n);
f=1;
}//if
if(maze[g][h]==0)
{
maze[g][h]=2;
s[top][0]=i;
s[top][1]=j;
s[top][2]=k;
++top;
i=g;
j=h;
k=0;
}//if
k=k+1;
}//while
}//while
if(f==0)
printf("no path\n");
}//pathvoid main()
{
path();
}本回答被网友采纳