问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
我想要达到的结果
用Devc++编译c语言,凡是有取地址符的地方基本·都报错是什么原因
```c
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef int DirectiveType;//定义方向类型
typedef int Status;
typedef struct{
int row;
int col;
}PosType;//通道块类型,代表的坐标(行,列)
typedef struct{
int step;//在路径中的序号
PosType seat;//位置坐标
DirectiveType di;//方向 1代表东 2代表南 3代表西 4 代表北
}SElemType;//栈元素类型,
typedef struct{
SElemType *base;//栈底
SElemType *top;//栈顶
int stacksize;//栈的尺寸大小
}SqStack;//栈的类型,
#define STACK_INIT_SIZE 100//初始栈的大小
#define STACKINCREMENT 10//每次栈补充的大小
Status InitStack(SqStack &S)//栈的初始化
{
S.base=(SElemType*)malloc(sizeof(SElemType)*STACK_INIT_SIZE);
if(!S.base) exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status GetTop(SqStack S,SElemType &e)//获取栈顶元素
{
if(S.base == S.top) return ERROR;
e=*(S.top-1);
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)//判断栈空
{
return S.base == S.top;
}
#define ROW 10
#define COL 10
#define RANGE 100
typedef struct {
int m,n;
int arr[RANGE][RANGE];
}MazeType;//定义迷宫类型
Status CreatMaze(MazeType &Maze,int a,int b)//随机产生迷宫
{
int i,j;
srand((unsigned int)time(NULL));
// 将迷宫的最外面包裹一堵墙,然后里面的随机产生墙或通路
for(i = 0; i < a; i++)
{
for(j = 0; j < b; j++)
{
// 迷宫的最外层是一堵墙
if(i==0 || j==0 || i==a-1 || j==b-1)
{
maze.arr[i][j] = 1;
printf("%2d", maze.arr[i][j]);
}
else
{
if ((i == 1 && j == 1) || (i == (a - 2) && j == (b - 2)))
{
maze.arr[i][j] = 0; // 设置入口或出口
}
else
{
int ret = rand() % 2; // 生成 0 或 1的随机数
maze.arr[i][j] = ret;
}
}
printf("%2d", maze.arr[i][j]);
}
if(j == b - 1)
printf("\n");
}
}
Status Pass(MazeType Maze,PosType CurPos)//判断路径是否可通,看迷宫矩阵里存的是0还是1,0代表可通
{
if(Maze.arr[CurPos.row][CurPos.col] == 0)
return 1;
else return 0;
}
Status FootPrint(MazeType &Maze,PosType CurPos)//如果能通过则留下足迹,防止下一次路径试探又回来
{
Maze.arr[CurPos.row][CurPos.col]=2;
return OK;
}
Status MarkPrint(MazeType &Maze,PosType CurPos)//走过的路径,但不能通过
{
Maze.arr[CurPos.row][CurPos.col]=3;
return OK;
}
SElemType CreatSElem(int step,PosType pos,int di)
{
SElemType e;
e.step = step;
e.di = di;
e.seat =pos;
return e;
}
PosType NextPos(PosType CurPos,DirectiveType di)
{
PosType Pos=CurPos;
switch(di)
{
case 1:Pos.col++;break;//East
case 2:Pos.row++;break;//south
case 3:Pos.col--;break;//west
case 4:Pos.row--;break;//north
}
return Pos;
}
Status PosEquare(PosType Pos1,PosType Pos2)//若当前路径为出口位置返回1,否则返回0
{
if(Pos1.col == Pos2.col && Pos1.row == Pos2.row)
return 1;
else return 0;
}
void PrintMaze(MazeType Maze)
{
int i,j;
printf(" ");
for(i=0;i<=COL+1;i++)
printf("%2d",i);
printf("\n");
for(i=0;i<=ROW+1;i++)
{
printf("%2d",i);
for(j=0;j<=COL+1;j++)
{
switch(Maze.arr[i][j])
{
case 0:printf(" ");break;//没有走过
case 2:printf(" *");break;//走过且走的通
case 3:printf(" @");break;//走过但不通
case 1:printf(" #");break;//障碍
}
}
printf("\n");
}
}
Status MazePath(MazeType &Maze,PosType start,PosType end)
{
SqStack S;
SElemType e;
InitStack(S);
PosType CurPos= start;
int Curstep=1;
do
{
if(Pass(Maze,CurPos))
{
FootPrint(Maze,CurPos);
e=CreatSElem(Curstep,CurPos,1);
Push(S,e);
if(PosEquare(CurPos,end)) return TRUE;
CurPos=NextPos(CurPos,1);
Curstep++;
}
else
{
if(!(StackEmpty(S)))
{
Pop(S,e);
while(e.di == 4 && !StackEmpty(S))
{
MarkPrint(Maze,e.seat);
Pop(S,e);
}
if(e.di<4)
{
e.di++;Push(S,e);
CurPos=NextPos(e.seat,e.di);
}
}
}
}while(!StackEmpty(S));
return FALSE;}
int main()
{
char cmd;int i,j;
PosType start,end;
MazeType Maze;
printf("请输入要生成的迷宫规格");
scanf("%d %d",&a,&b);
CreatMaze(Maze,a,b);
printf("\n迷宫入口坐标:");
scanf("%d%d",&start.row,&start.col);
printf("\n迷宫出口坐标:");
scanf("%d%d",&end.row,&end.col);
if(MazePath(Maze,start,end))
{
PrintMaze(Maze);
}
else
{
printf("\nNO WAY");
}
system("pause");
return 0;
}
```