毕业论文论文范文课程设计实践报告法律论文英语论文教学论文医学论文农学论文艺术论文行政论文管理论文计算机安全
您现在的位置: 毕业论文 >> 课程设计 >> 正文

迷宫问题

更新时间:2007-10-20:  来源:毕业论文
迷宫问题|数据结构课程设计

/*****************迷宫问题***************/
#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

迷宫问题下载如图片无法显示或论文不完整,请联系qq752018766
设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©751com.cn 辣文论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。