返回首页
当前位置: 首页 > 设计报告 > 数据结构课程设计 浏览正文

数据结构课程设计_迷宫问题

时间:2010年06月25日来源:网络 作者:admin 联系QQ:3710167点击这里给我发消息
原创论文提供各类原创参考论文,参考使用,欢迎联系使用联系QQ:3710167

迷宫问题代码/****************
 

迷宫问题代码

/*****************迷宫问题***************/
#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;
 int top=-1;
 int maze,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);
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;    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个方向*/
{
 move[0].vert=-1;move[0].horiz=0;
 move.vert=-1;move.horiz=1;
 move.vert=0;move.horiz=1;
 move.vert=1;move.horiz=1;
 move.vert=1;move.horiz=0;
 move.vert=1;move.horiz=-1;
 move.vert=0;move.horiz=-1;
 move.vert=-1;move.horiz=-1;
}

为什么能运行,但却输不出path.
问题补充:没少函数,单步运行后,问题出在EXIT_ROW,EXIT_COL



    
顶一下
()
%
踩一下
()
%
------分隔线----------------------------
推荐内容