您的位置:首页 > 理论基础 > 数据结构算法

数据结构-迷宫实验

2013-06-11 10:44 309 查看
//迷宫实验

#include<stdio.h>

#include<time.h>

#include<stdlib.h>

#include<malloc.h>

#include<conio.h>

#define ERROR 0

#define OK 1

#define TRUE 1

#define FALSE 0

#define OVERFLOW -2

#define STACK_INIT_SIZE 100

/*存储空间初始分配量*/

#define STACKINCREMENT 10

/*存储空间分配这增量*/

typedef struct{

int x;

int y;

}PosType;

typedef struct{

int di; /*下一个通道的方向*/

int pass; /*该通道是否可以通过*/

PosType seat; /*该点的位置*/

}SElemType;

typedef struct{

int lenx;

int leny;

SElemType *mz;

SElemType start;

SElemType end;

}MazeType;

typedef struct{

SElemType *base; /*在栈构造之前和销毁之后,base的值为NULL*/

SElemType *top; /*栈顶指针*/

int stacksize; /*当前已分配的存储空间,以元素为单位*/

}SqStack;

int InitStack(SqStack *s){/*构造空栈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;

}

int StackEmpty(SqStack s){/*若栈为空,则返回TURE,否则返回FALSE*/

if (s.base == s.top){

return TRUE;

}

return FALSE;

}

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;

}

int Pop(SqStack *s,SElemType *e){ /*若栈不为空,则删除s的栈顶元素,用e返回其值,并返回OK;

否则返回ERROR*/

if (s->top == s->base) return ERROR;

*e = *--s->top;

return OK;

}

int PrintMaze(MazeType M){

int i,j;

printf(" ");

for(i=0;i<M.leny;++i)

{

printf("%2d",i);

}

printf("\n");

for (i = 0; i<M.lenx; i++){

printf("%2d",i);

for (j = 0; j<M.leny; j++){

if ((*(M.mz+i*M.leny+j)).pass == 1){

printf("%c%c",2,2);

}

else if ((*(M.mz+i*M.leny+j)).pass == 0){

printf("%c%c",0,0);

}

}

printf("\n");

}

return OK;

}

int InitMaze(MazeType *M){

int i,j;

srand((unsigned)time(NULL));

printf("please enter the maze's length and depth:\n");

scanf("%d",&M->lenx);

scanf("%d",&M->leny);

M->mz = (SElemType *)malloc(M->lenx*M->leny*sizeof(SElemType));

for (i = 0; i<M->lenx; i++){

for (j = 0; j<M->leny; j++){

if (j==0||i==0||i==M->lenx-1||j==M->leny-1){

(*(M->mz+i*M->leny+j)).pass = 1;

(*(M->mz+i*M->leny+j)).di = 0;

(*(M->mz+i*M->leny+j)).seat.x = i;

(*(M->mz+i*M->leny+j)).seat.y = j;

}

else{

(*(M->mz+i*M->leny+j)).pass = rand()%2;

(*(M->mz+i*M->leny+j)).di = 0;

(*(M->mz+i*M->leny+j)).seat.x = i;

(*(M->mz+i*M->leny+j)).seat.y = j;

}

}

}

PrintMaze(*M);

printf("please enter the start and end:(x,y)\n");

scanf("%d,%d",&M->start.seat.x,&M->start.seat.y);

scanf("%d,%d",&M->end.seat.x,&M->end.seat.y);

M->start = *(M->mz+M->start.seat.x*M->leny+M->start.seat.y);

M->end = *(M->mz+M->end.seat.x*M->leny+M->end.seat.y);

return OK;

}

int NextPos(SElemType **curpos,int *di,MazeType M){

switch(*di){

case 0:

*curpos = (M.mz+(*curpos)->seat.x*M.leny+1+(*curpos)->seat.y);

*di = *di+1;break;

case 1:

*curpos = (M.mz+((*curpos)->seat.x-1)*M.leny+(*curpos)->seat.y);

*di++;break;

case 2:

*curpos = (M.mz+(*curpos)->seat.x*M.leny-1+(*curpos)->seat.y);

*di++;break;

case 3:

*curpos = (M.mz+((*curpos)->seat.x+1)*M.leny+(*curpos)->seat.y);

*di++;break;

}

return OK;

}

int MazePath(SqStack *S,MazeType M){

SElemType *curpos = (M.mz+M.start.seat.x*M.leny+M.start.seat.y),e;

do{

if (curpos->pass == 0){

curpos->pass = 2; /*表是位置为当前路径上的*/

Push(S,*curpos);

if (curpos->seat.x == M.end.seat.x&&curpos->seat.y == M.end.seat.y) return OK;

NextPos(&curpos,&curpos->di,M);

}

else{

if (!StackEmpty(*S)){

Pop(S,&e);

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

e.pass = 3;

Pop(S,&e);

}

if (e.di < 4){

e.di++;Push(S,e);curpos = &e; NextPos(&curpos,&curpos->di,M);

}

}

}

}while (!StackEmpty(*S));

return FALSE;

}

int Print(SqStack *s){ SElemType *p = s->base;

if(StackEmpty(*s))printf("null");

while (s->top != p){

printf("(%d,%d) ",p->seat.x,p->seat.y);

p++;

}

return OK;

}

void main(){

MazeType M;

SqStack S;

InitStack(&S);

InitMaze(&M);

MazePath(&S,M);

Print(&S);

getch();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: