- 11
- 0
- 约5.56千字
- 约 7页
- 2021-01-28 发布于甘肃
- 举报
#include stdio.h
#include malloc.h
#include stdlib.h
#include string.h
#include time.h
#define OK 1
#define ERROR 0
#define NULL 0
#define OVERFLOW -2
#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;
SElemType *top;
int stacksize; //当前已分配的存储空间,以元素为单位
}SqStack;
//基本操作
int 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;
}
//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
int GetTop(SqStack *S,SElemType *e)
{
if(S-top==S-base) return ERROR;
*e=*(S-top-1);
return OK;
}
int Push(SqStack *S,SElemType *e)//插入元素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;
}
//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
int Pop(SqStack *S,SElemType *e)
{
if(S-top==S-base) return ERROR;
*e=*--S-top;
return OK;
}
int StackEmpty(SqStack *S)
{
return(S-top==S-base) ;
}
//迷宫程序
typedef struct {
int lie; /*列数*/
int hang; /*行数*/
char a[999][999];
}MazeType; /*迷宫类型*/
/*随机生成迷宫*/
int generatemaze( MazeType *maze)
{
int i,j;
maze-a[0][0]=2;
maze-a[++maze-hang][++maze-lie]=3;
/*设置外墙*/
maze-a[0][maze-lie]=!;
maze-a[maze-hang][0]=!;
for(j=1;jmaze-lie;j++)
{maze-a[0][j]=!;maze-a[maze-hang][j]=!;}
for(i=1;imaze-hang;i++)
{maze-a[i][0]=!;maze-a[i][maze-lie]=!;}
srand((unsigned)time( NULL ));
rand();
for(i=1; i maze-hang; i++)
for(j=1;jmaze-lie;j++)
{
if (rand()=RAND_MAX/4) maze-a[i][j] = ; // 暗示出路
else maze-a[i
原创力文档

文档评论(0)