课程设计首页 课程设计首页 数据结构课程设计 数据结构课程设计 C语言课程设计 C语言课程设计 电子课程设计 电子课程设计 微机原理课程设计 微机原理课程设计 机械设计课程设计 机械设计课程设计 软件工程课程设计 软计工程课程设计 平面设计课程设计 平面设计课程设计 单片机课程设计 单片机课程设计  课程设计报告 课程设计报告 课程设计心得体会 课程设计心得体会
           
课程设计栏目热门文章
·数据结构课程设计_算术表
·数据结构课程数据_飞机订
·数据结构课程设计_单链表
·数据结构课程设计_图的遍
·数据结构课程设计_Huffman
·数据结构课程设计_用静态
·数据结构课程设计_成绩管
·数据结构课程设计_joseph
·数据结构课程设计_文本编
·数据结构课程设计_迷宫问
·数据结构课程设计_拓扑排
·数据结构课程设计_几种常
·数据结构课程设计_约瑟夫
·数据结构课程设计_最小生
·数据结构课程设计_哈夫曼
·学生成绩管理系统(c语言)
·图邻接矩阵 邻接表的建立c
·图书管理系统c++版_数据结
·通讯录(c++)_数据结构课
·稀疏矩阵运算器数_据结构
·停车场管理器_数据结构课
·航班_数据结构课程设计 我
·数据结构的课程设计总结
·成绩管理系统c语言版
当前位置:首页>数据结构课程设计> 正文
数据结构课程设计_迷宫问题
文章发布:课程设计网 发布时间:2006-10-18

迷宫问题代码

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



上一篇:数据结构课程设计_校园导游咨询   下一篇:数据结构课程设计_算术表达式的实现
COPYRIGHT© 2006-2008 WWW.UT365.COM ONLINE SERVICES. ALL RIGHTS RESERVED. 鲁ICP备05047946号