#include
#include
#define STACK_INIT_SIZE 100 //存储空间初始分配量
#define STACKINCREAMENT 10 //存储空间分配增量
#define num 10
typedef int MazeType[num][num];
int curstep; //定前当前足迹
MazeType m = {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}; //定义了一个全局变量,10*10的迷宫
typedef struct{
int ord; //通道块在路径上的序号
PosType seat; //通道块在迷宫中的“坐标位置”
int di; //从此通道块走向下一通道块的“方向”。1表示向右,2表示向上,3表示向左,4表示向下。
}SElemType; //栈的元素类型
typedef struct{
SElemType *base; //栈底指针,在栈构造之前和销毁之后为NULL
SElemType *top; //栈顶指针
int stacksize; //当前已分配的存储空间,以元素为单位
}SqStack;
int InitStack(SqStack *S){
(*S).base = (SElemType *) malloc (STACK_INIT_SIZE * (sizeof(SElemType)));
if(!(*S).base) exit(0);
(*S).top = (*S).base;
(*S).stacksize = STACK_INIT_SIZE;
return 1;
}
int Push(SqStack *S, SElemType e){
if((*S).top-(*S).base >= (*S).stacksize){
(*S).base = (SElemType *) realloc ((*S).base, ((*S).stacksize + STACKINCREAMENT) * sizeof(SElemType));
if(!(*S).base) exit(0);
(*S).top = (*S).base + (*S).stacksize;
(*S).stacksize += STACKINCREAMENT;
}
*((*S).top) = e;
(*S).top ++;
return 1;
}
int Pop(SqStack S, SElemType e){ //删除S的栈顶元素并用e返回其值
if((*S).top == (*S).base) exit(0);
(*S).top --;
*e = *(*S).top;
return 1;
}
bool StackEmpty(SqStack S){
if(S.top == S.base) return true;
return false;
}
typedef struct{ //通道块在迷宫中的位置,横纵坐标
int x;
int y;
}PosType;
int Pass(PosType curpos){ //判断该位置是否可通过.若为1则可通过,若为0则此通道块为墙,若为-1则此通道块曾被纳入过路径但不通,
if ( m[curpos.x][curpos.y] == 1 ) return 1; //若为其余整数说明此通道块已在当前路径上。
else return 0;
}
void FootPrint(PosType curpos){ //留下足迹
m[curpos.x][curpos.y] = curstep;
}
PosType NextPos(PosType curpos, int n){ //下一位置
switch(n)
{
case 0: //当前位置的右通路块
curpos.x += 0;
curpos.y += 1;
case 1: //当前位置的上通路块
curpos.x -= 1;
curpos.y += 0;
case 2: //当前位置的左通路块
curpos.x += 0;
curpos.y -= 1;
case 3: //当前位置的下通路块
curpos.x += 1;
curpos.y += 0;
}
return curpos;
}
void MarkPrint(PosType curpos){ //留下不能通过的标记
m[curpos.x][curpos.y] = -1;
}
void PrintMaze(MazeType maze){ //打印迷宫
for(int i = 0; i < num; i++){
for(int j = 0; j < num; j++){
printf("%d",maze[i][j]);
}
printf("\n");
}
}
bool MazePath(MazeType maze, PosType start, PosType end){ //求迷宫中是否有从start到end的路径
PosType curpos;
SqStack S;
SElemType e;
InitStack(&S);
curpos = start; //起点为当前位置
curstep = 1; //路径中的第一步
do{
if( Pass(curpos) ){ //当前位置可通过
maze[curpos.x][curpos.y] = curstep; //在迷宫中标记出足迹
e.ord = curstep;
e.seat.x = curpos.x;
e.seat.y = curpos.y;
e.di = 0;
Push(&S, e); //加入路径中
if(curpos.x == end.x && curpos.y == end.y ) return true; //到达终点
curstep ++; //探索下一步
curpos = NextPos(curpos, 0); //下一位置在当前位置的右侧
}
else{
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);
curpos = NextPos(e.seat,e.di); //设定当前位置是该新方向上的相邻块
curstep ++;
}
}
}while(!StackEmpty(S));
return false;
}
void main(){
void PrintMaze(MazeType maze);
bool MazePath(MazeType maze, PosType start, PosType end);
PosType start = {1,1};
PosType end = {num-1,num-1};
printf("迷宫的结构如下:");
PrintMaze(m);
if( MazePath(m,start,end) ){
printf("\n从迷宫的入口到出口的路径如下:");
PrintMaze(m);
}
else printf("此迷宫没有从入口到出口的路径。");
}
错误提示有60多个... 不知道错在哪.. 求助!!
粘几个错误如下:
错误 33 error C2449: 在文件范围内找到“{”(是否缺少函数头?) e:\文件文件\数据结构程序\迷宫问题2\迷宫问题2\maze.c 70 1 迷宫问题2
错误 45 error C2449: 在文件范围内找到“{”(是否缺少函数头?) e:\文件文件\数据结构程序\迷宫问题2\迷宫问题2\maze.c 98 1 迷宫问题2
错误 55 error C2146: 语法错误: 缺少“;”(在标识符“start”的前面) e:\文件文件\数据结构程序\迷宫问题2\迷宫问题2\maze.c 153 1 迷宫问题2
错误 51 error C2146: 语法错误: 缺少“;”(在标识符“MazePath”的前面) e:\文件文件\数据结构程序\迷宫问题2\迷宫问题2\maze.c 151 1 迷宫问题2
错误 58 error C2146: 语法错误: 缺少“;”(在标识符“end”的前面) e:\文件文件\数据结构程序\迷宫问题2\迷宫问题2\maze.c 154 1 迷宫问题2