“迷宫求解”出现“Process exited after 37.49 seconds with return value 3221225477“

问题遇到的现象和发生背景

运行网上“迷宫求解”C++代码时出现“Process exited after 37.49 seconds with return value 3221225477”

用代码块功能插入代码,请勿粘贴截图

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

#define MAXSIZE 500 //栈最大的容量

#define MAXLENGTH 20//设置最大的行和列是20 20*20

typedef struct{

int x;

int y;

}PosType;//PosType 坐标类型的变量PosType

typedef struct {

int ord;//用于存放路线

PosType seat;

int di;//用于指示下一个方向

}SElemType;//栈里面存放的每个元素的类型

typedef struct {
SElemType *top;
SElemType *base;

//int stacksize;

}SqStack; //top和base是指向SElemType类型的变量的

PosType begin,end;//定义迷宫的出入口坐标 全局变量

PosType direc[4]={{0,1},{1,0},{0,-1},{-1,0},};//移动方向依次为东南西北

int curstep=1;//记录路径的一个东西

void InitStack(SqStack*);

void Push(SqStack *S,SElemType e);

bool Pop(SqStack *S,SElemType e);

bool StackEmpty(SqStack S);

void Init(void);

void Print(void); //打印函数的声明

bool Pass(PosType b);

void MarkPrint(PosType b);

void NextPos(PosType *b,int di);

bool MazePath(PosType begin,PosType end);

int x,y;//告诉你迷宫有几行几列

typedef int MazeType[MAXLENGTH][MAXLENGTH];

MazeType m; //定义了一个迷宫数组

int main(){
    

Init();

if(MazePath(begin,end)){

printf("The path of this maze is as follows\n") ;
Print();

}

else

printf("\n\nThe path of this maze has no path from the start to the end!\n") ;

return 0;

}

void InitStack(SqStack *S){

SElemType* p;

p=(SElemType *)malloc(sizeof(SElemType)*MAXSIZE);

if(!p)

exit(-1);//程序异常退出 非零都是异常退出

S->base=S->top=p;//初始化之前的时候top和base都为空

//初始化之后top和base都指向刚才返回的首地址

// S->stacksize=0;

}

void Push(SqStack *S,SElemType e){

//1看栈是否满了

//2满了就追加空间



*(S->top)=e;

S->top++;

// S->stacksize ++;

}

bool Pop(SqStack *S,SElemType *e){

if(S->base==S->top)

{

// printf(“栈为空,出栈失败!”);

return false;

}
*e=*--S->base; 
//free(S->top);

//S->stacksize–;

return true;

}

bool StackEmpty(SqStack S){

if(S.top==S.base)

return true;

else

return false;

}

void Init(void){

//设计迷宫的布局

int i ,j,x1,y1; //定义了一个i,j 用于计数x1 y1是用于输入坐标

printf("Please enter the number of rows and columns of the maze (including the exterior wall ):");// 提示用户输入

scanf("%d,%d",&x,&y); //输入迷宫的行数和列数

for(i=0;i[i]=0;

m[x-1][i]=0;

} //把迷宫的上墙和下墙给打印出来(用0表示)

for(i=0;i[0]=0;

m[i][y-1]=0;

} //把迷宫的左墙和右墙给打印出来(用0表示)


for(i=1;ij=1;j[j]=1;//把除墙之外的所有位置全部标为1 提示用户可以走



printf("Please enter the number of walls inside the maze:");// 自己制作迷宫,墙的个数

scanf("%d",&j);

printf("Please enter the coordinates of the wall in the maze\n");

for(i=1;i<=j;i++){

scanf("%d,%d",&x1,&y1);//墙的介质,每输入一次,就改变一次。

m[x1][y1]=0;//输入什么,什么就变成了这个位置就变成了一个墙

}



printf("The structure of the maze is as follows:\n");

Print();//迷宫打印出来看一下



printf("Please enter the coordinates of the entrance :");

scanf("%d,%d",&begin.x,&begin.y );

printf("Please enter the coordinates of the exit :");

scanf("%d,%d",&end.x,&end.y );

}

void Print(void){

int i,j; //同样是用来计数

for(i=0;ij=0;j"%3d",m[i][j]);



}

printf("\n"); 
}

}

bool Pass(PosType b){//判断b坐标是否为可以通过的坐标

if(m[b.x][b.y]==1)

return true ;

else

return false;

}


void FootPrint(PosType b){ //当b是可以通过的时候把b这个点的位置放上足迹

m[b.x][b.y]=curstep; //记录坐标的位置,用于路线的显示

}

void MarkPrint(PosType b){ //这条路不能走的话 -1 死胡同

m[b.x][b.y]=-1;

}



void NextPos(PosType *b,int di){//根据当前位置和di修改b为下一个位置

b->x =b->x+direc[di].x ;

b->y =b->y+direc[di].y ;

} //根据di 来决定下一个坐标位置

bool MazePath(PosType begin,PosType end){//若迷宫m从入口位置到出口位置可以走得通,就把这条道路放到栈中

PosType curpos=begin; //curpos表示当前的坐标 等于begin 等于入口坐标-0

SqStack S; //定义了一个栈

SElemType e;//栈中的元素 这个栈中存放的是SElemType类型的变量(相当于int)

InitStack(&S);//初始化栈

do{

//printf("\n%d\n",e.ord);

if(Pass(curpos)){//当前坐标可以通过

// printf("\n%d\n",e.ord);

FootPrint(curpos);//在这个位置上赋值



e.ord =curstep;//当前在栈中位置



e.seat =curpos; //栈中存放当前坐标 ?

e.di =0; //让从东开始

//printf("\n%d\n",e.di);

Push(&S,e);



curstep++;

if(curpos.x==end.x&&curpos.y==end.y )

return true;



NextPos(&curpos,e.di);

// printf("%d ,%d",curpos.x ,curpos.y );

}//如果我们走到的位置等于我们规定的位置,我们就跳出循环。

else{

if(!StackEmpty(S)){

//S.top --;

Pop(&S,&e);

curstep--;

while(e.di==3&&!StackEmpty(S)){

MarkPrint(e.seat);

Pop(&S,&e);

curstep--;

}

}

if(e.di<3)

{

e.di++;
Push(&S,e);
curstep++;

curpos=e.seat;

NextPos(&curpos,e.di); 

} 

}

}while(!StackEmpty(S)) ;

return false;

} 


运行结果及报错内容

img

我的解答思路和尝试过的方法

在网上搜索过是访问越界的问题

img


但是具体没有找到是哪个地方有问题,代码仔细对照过也和网上UP主的一样(不排除看漏了的原因)。

我想要达到的结果

希望最后可以成功在DEV-C++运行出下图的结果

img

这里错了
Pop函数里面 e=--S->base; ----- base改成top, 见下面代码。 其它代码不用改

bool Pop(SqStack *S,SElemType *e){
  if(S->base==S->top)
  {
    // printf(“栈为空,出栈失败!”);
    return false;
  }
  *e=*--S->top; /*base改成top*/
  //free(S->top);
  //S->stacksize–;
  return true;
}

还有你输入墙壁的坐标上面也有点问题,第3个点1,4是错的,这里是外墙,1,3可以

对于这种情况检查scanf函数的输入模式是否有问题,结果表示超出了数组空间