这是大一数据结构迷宫作业,用c实现
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 12
#define ERROR -1
#define OK 1
#define FALSE 0
#define TRUE 1
typedef enum{RIGHT,DOWN,LEFT,UP
}diraction;
typedef enum{YES,NO
}MarkTag;
typedef struct position
{
int x;
int y;
}Position;
typedef struct
{
int order;
Position coordinate;
diraction orientation;
}SElemType;
typedef struct
{
SElemType *elem;
int top;
}Stack;
char maze[MAXSIZE][MAXSIZE]=
{
{'*','*','*','*','*','*','*','*','*','*','*'},
{'*','0','0','0','0','0','0','1','0','0','*'},
{'*','0','0','1','0','0','0','1','1','0','*'},
{'*','1','0','1','0','1','1','0','0','0','*'},
{'*','0','0','1','1','0','0','0','0','0','*'},
{'*','0','0','1','1','0','0','0','0','0','*'},
{'*','0','1','1','0','0','1','0','0','0','*'},
{'*','0','1','1','1','0','1','1','0','0','*'},
{'*','0','0','0','0','0','0','0','0','0','*'},
{'*','*','*','*','*','*','*','*','*','*','*'}
};
int InitStack(Stack *S)
{
S->elem=(SElemType*)malloc(MAXSIZE*MAXSIZE*sizeof(SElemType));
if(!S->elem)
return ERROR;
S->top=0;
return OK;
}
int Push(Stack *S,SElemType e)
{
if(S->top>=MAXSIZE*MAXSIZE)
return ERROR;
S->elem[S->top++]=e;
return OK;
}
int Pop(Stack *S,SElemType *e)
{
if(S->top<=0)
return ERROR;
*e=S->elem[--S->top];
return OK;
}
int Empty(Stack S)
{
if(S.top==0)
return TRUE;
else
return FALSE;
}
int createMaze(Position *startpos,Position *endpos)
{
Position start,end;
printf("\n\n");
printf("请输入迷宫入口的位置:");
scanf("%d%d",&start.y,&start.x);
printf("请输入迷宫出口的位置:");
scanf("%d%d",&end.y,&end.x);
*startpos=start; *endpos=end;
return OK;
}
int canPass(Position canpos)
{
if(maze[canpos.x][canpos.y]=='0')
return TRUE;
return FALSE;
}
void markPos(Position canpos,MarkTag tag)
{
switch(tag)
{
case YES: maze[canpos.x][canpos.y]='R'; break;
case NO: maze[canpos.x][canpos.y]='X'; break;
}
}
Position nextPos(Position canpos,diraction dir)
{
Position nextpos;
switch(dir)
{ case RIGHT:nextpos.x=canpos.x ;nextpos.y =canpos.y +1; break;
case DOWN :nextpos.x=canpos.x+1 ;nextpos.y =canpos.y; break;
case LEFT :nextpos.x=canpos.x ;nextpos.y =canpos.y -1; break;
case UP :nextpos.x=canpos.x-1 ;nextpos.y =canpos.y; break;
}
return nextpos;
}
diraction nextDir(diraction dir)
{ switch(dir)
{ case RIGHT: return DOWN;
case DOWN : return LEFT;
case LEFT: return UP;
}
}
int Solve(Stack *S,Position start,Position end)
{
Position canpos;
SElemType e;
int canstep=1;
if(InitStack(S)==ERROR)
return FALSE;
canpos=start;
do{
if(canPass(canpos)){
markPos(canpos,YES);
e.order=canstep;e.coordinate=canpos;e.orientation=RIGHT;
Push(S,e);
if(canpos.x==end.x&&canpos.y==end.y)
return TRUE;
canpos=nextPos(canpos,RIGHT);
canstep++;
}
else{
if(!Empty(*S))
{
if(Pop(S,&e)==ERROR)
return FALSE;
while(e.orientation==UP&&!Empty(*S)){
canpos=e.coordinate;
markPos(canpos,NO);
if(Pop(S,&e)==ERROR)
return FALSE;
}
if(e.orientation!=UP){
e.orientation=nextDir(e.orientation);
Push(S,e);
canpos=nextPos(e.coordinate,e.orientation);
}
}
}
}
while(!Empty(*S));
return FALSE;
}
void main()
{
int i,j;
printf("迷宫地图,0位路径,1为墙壁,X为不能通过,R为可以通过路径,*为墙壁\n");
for(i=0;i<10;i++){
for(j=0;j<11;j++)
printf("%c ",maze[i][j]);
printf("\n");
}
Position startPos,endPos;
Stack path;
SElemType e;
if(createMaze(&startPos,&endPos)==ERROR) return ;
Solve(&path,startPos,endPos);
while(!Empty(path)){
Pop(&path,&e);
printf("(%d,%d)",e.coordinate.x,e.coordinate.y);
}
printf("\n");
for(i=0;i<11;i++)
{
for(j=0;j<10;j++)
printf("%c ",maze[i][j]);
printf("\n");
}
}