迷宫问题
/*****************迷宫问题***************/
#define MAX 100
typedef struct { /* */
short int row;
short int col;
short int dir;
}element;
element stack[MAX];
typedef struct{
short int vert;
short int horiz;
}offsets;
offsets move[8];
int top=-1;
int maze[9][6],mark[MAX][MAX];
int EXIT_ROW;
int EXIT_COL;
void add(int *top,element a);
element delete(int *top);
void path(void);
void chushi(offsets move[8]);
main()
{
int i,j;
chushi(move);
printf("The maze is:"); /*输入迷宫:最外围必须是1*/
for(i=0;i<9;i++)
for(j=0;j<6;j++)
scanf("%d",&maze[i][j]);
for(i=0;i<9;i++)
{
printf("\n");
for(j=0;j<6;j++)
printf("%d",maze[i][j]);
}
EXIT_ROW=i-2;
EXIT_COL=j-2;
printf("\n");
path();
}
void add(int *top,element a) /*入栈函数*/
{
stack[++*top]=a;
}
element delete(int *top)
{
return stack[(*top)--];
}
void path(void) /*搜索出口*/
{
int i, row, col, next_row, next_col, dir, found = 0;
element position;
mark[1][1] = 1; top = 0;
stack[0].row = 1; stack[0].col = 1; stack[0].dir = 1;
while ( top > -1 && !found ) {
position = delete(&top);
row = position.row; col = position.col;
dir = position.dir;
while ( dir < 8 && !found )
{
next_row = row + move[dir].vert;
next_col = col + move[dir].horiz;
if ( next_row == EXIT_ROW && next_col == EXIT_COL )
found = 1;
else if ( !maze[next_row][next_col] && !mark[next_row][next_col] ) {
mark[next_row][next_col] = 1;
position.row = row; position.col = col;
position.dir = ++dir;
add(&top, position);
row = next_row; col = next_col; dir = 0;
}
else ++dir;
}
}
if ( found==1 )
{
printf ("The path is:\n");
printf ("row col\n");
printf("\n");
for ( i = 0; i <= top; i++ )
printf ("%2d %5d", stack[i].row, stack[i].col);
printf("\n");
printf ("%2d %5d\n",row,col);
printf("\n");
printf ("%2d%5d\n",EXIT_ROW,EXIT_COL);
}
else printf ("The maze does not have a path\n");
}
void chushi(offsets move[8]) /*初始化8个方向*/
{
move[0].vert=-1;move[0].horiz=0;
move[1].vert=-1;move[1].horiz=1;
move[2].vert=0;move[2].horiz=1;
move[3].vert=1;move[3].horiz=1;
move[4].vert=1;move[4].horiz=0;
move[5].vert=1;move[5].horiz=-1;
move[6].vert=0;move[6].horiz=-1;
move[7].vert=-1;move[7].horiz=-1;
}
为什么能运行,但却输不出path.
问题补充:没少函数,单步运行后,问题出在EXIT_ROW,EXIT_COL