出现了段错误,但是不会改,给出调试结果,麻烦各位解决一下~
调试结果如图应该是显示路径那个函数错误了,但是我感觉没错呀,哪里出现段错误了,麻烦解决,谢谢
附上代码如下:
程序代码:
#include <stdio.h> #include <stdlib.h> typedef int status; #define OK 1 #define OVERFLOW -2 #define ERROR 0 #define FALSE 0 #define TRUE 1 #define INFEASIBLE -1 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef struct { int x; //定义行的数值 int y; //定义列的数值 }PosType; typedef struct //定义迷宫元素 { int ord; //通道块在路径上的序号 PosType seat; //通道块在迷宫中的坐标位置 int di; //从此通道块走向下一通道块的方向 }SElemType; typedef struct //引入栈链储存路径 { SElemType * base; //栈底指针,在栈构造之前和销毁之后,base的值为NULL SElemType * top; //栈顶指针 int stacksize; //当前已分配的储存空间 }SqStack; status InitStack(SqStack &S) //初始化栈链 { S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!S.base) exit(OVERFLOW); S.top=S.base; S.stacksize=STACK_INIT_SIZE; return OK; } status Push(SqStack &S, SElemType e) //入栈 { if(S.top-S.base>=S.stacksize) { S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType)); if(!S.base) exit(OVERFLOW); S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; return OK; } status Pop(SqStack &S,SElemType &e) //出栈 { if(S.top==S.base) return ERROR; e=*--S.top; return OK; } status StackEmpty(SqStack S) //判断栈是否为空 { if(S.top==S.base) return OK; else return ERROR; } void display_maze(int maze[11][11]) //展示迷宫 { int i,j; for(i=0;i<10;i++) { for(j=0;j<10;j++) printf("%d ",maze[i][j]); printf("\n"); } } status pass(int maze[11][11],PosType current_point) //可通过的点 { if(maze[current_point.x][current_point.y]==1) return OK; else return ERROR; } void foot_print(int maze[11][11],PosType nextpoint) //打上可以通过的标志 { maze[nextpoint.x][nextpoint.y]=0; } SElemType push_element(int current_step,PosType current_point,int di) //将可以通过的点放到堆栈里面 { SElemType e; e.di=di; e.ord=current_step; e.seat=current_point; return e; } PosType next_point(PosType current_point,int di) //下一个探索点 { PosType nextpoint=current_point; switch(di) { case 1: nextpoint.y+=1;break; case 2: nextpoint.x+=1;break; case 3: nextpoint.y-=1;break; case 4: nextpoint.x-=1;break; } return nextpoint; } status maze_path(int maze[11][11],SqStack S,PosType start,PosType end) //迷宫路径 { PosType current_point=start,save_currentpoint; int current_step=1; SElemType e; InitStack(S); do { if(pass(maze,current_point)) { foot_print(maze,current_point); e=push_element(current_step,current_point,1); Push(S,e); if(current_point.x==end.x&¤t_point.y==end.y) return OK; else { save_currentpoint=current_point; current_point=next_point(current_point,1); current_step++; } } else { if(!StackEmpty(S)) { Pop(S,e); current_point=save_currentpoint; while(e.di==4&&!StackEmpty(S)) { foot_print(maze,current_point); Pop(S,e);current_step--; } if(e.di<4) { e.di++; Push(S,e); current_point=next_point(e.seat,e.di); } } } }while(!StackEmpty(S)); return OK; } void display_path(int maze[11][11],SqStack S) //展示迷宫路径 { SElemType *p=S.base; if(S.base==S.top) printf("栈为空.\n"); while(p!=S.top) { maze[p->seat.x][p->seat.y]=2; *p++; } int i,j; for(i=0;i<10;i++) { for(j=0;j<10;j++) { if(maze[i][j]==2) printf("1 "); else printf(" "); } } } int main() { SqStack S; int maze[11][11]={ {0,0,0,0,0,0,0,0,0,0},{0,1,1,0,1,1,1,0,1,0},{0,1,1,0,1,1,1,0,1,0},{0,1,1,1,1,0,0,1,1,0},{0,1,0,0,0,1,1,1,1,0},{0,1,1,1,0,1,1,1,1,0},{0,1,0,1,1,1,0,1,1,0},{0,1,0,0,0,1,0,0,1,0},{0,0,1,1,1,1,1,1,1,0},{0,0,0,0,0,0,0,0,0,0} };;; printf("迷宫如下\n"); display_maze(maze); PosType start,end; start.x=1;start.y=1; end.x=8;end.y=8; if(maze_path(maze,S,start,end)) { display_path(maze,S); } else printf("\n无通道\n"); return OK; }