#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 10
#define STACK_INCREMENT 2
#define MAXLENGTH 25
#define OVERFLOW 0
#define FALSE 0
#define TRUE 1
#define ERROR -1
#define OK 1
typedef int MazeType[MAXLENGTH][MAXLENGTH];
typedef int Status;
struct PosType
{
int x;
int y;
};
PosType begin, end;
PosType direc[4] = {{0, 1}, {1, 0}, {0,-1}, {-1, 0}};
MazeType m;
int x, y;
int curstep = 1;
void Print()
{
int i, j;
for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
printf("%2d", m[i][j]);
}
printf("\n");
}
}
void Init()
{
int i, j, x1, y1;
printf("请输入迷宫的行数,列数(包括外墙):" );
scanf("%d,%d", &x, &y);
for(i = 0; i < y; i++)
{
m[0][i] = 0;
m[x - 1][i] = 0;
}
for(i = 1; i < x - 1; i++)
{
m[i][0] = 0;
m[i][y-1] = 0;
}
for(i = 1; i < x - 1; i++)
{
for(j = 1; j < y - 1; j++)
{
m[i][j] = 1;
}
}
printf("请输入迷宫内墙单元数:");
scanf("%d", &j);
printf("请依次输入迷宫内墙每个单元的行数,列数:\n");
for(i = 0; i < j ; i++)
{
scanf("%d,%d", &x1, &y1);
m[x1][y1] = 0;
}
printf("迷宫结构如下:\n");
Print();
printf("请输入入口行数,列数:");
scanf("%d,%d", &begin.x, &begin.y);
printf("请输入出口的行数,列数:");
scanf("%d,%d", &end.x, &end.y);
}
struct SElemType
{
int ord;
PosType seat;
int di;
};
struct SqStack
{
SElemType *base;
SElemType *top;
int stacksize;
};
void InitStack(SqStack &S)
{
S.base = (SElemType *)malloc(sizeof(SElemType) * STACK_INIT_SIZE);
if(!S.base)
exit(0);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
}
void DestroyStack(SqStack &S)
{
free(S.base);
S.top = S.base = NULL;
S.stacksize = 0;
}
void ClearStack(SqStack &S)
{
S.top = S.base;
}
Status StackEmpty(SqStack S)
{
if(S.top == S.base)
return TRUE;
else
return FALSE;
}
Status StackFull(SqStack S)
{
if(S.top - S.base == S.stacksize)
return TRUE;
else
return FALSE;
}
int StackLength(SqStack S)
{
return S.top - S.base;
}
void Push(SqStack &S, SElemType e)
{
if(StackFull(S))
{
S.base = (SElemType *)realloc(S.base, (S.stacksize + STACK_INCREMENT) * sizeof(SElemType));
if(!S.base)
exit(OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize += STACK_INCREMENT;
}
*S.top++ = e;
}
Status Pop(SqStack &S, SElemType &e)
{
if(StackEmpty(S))
return ERROR;
e = *--S.top;
return OK;
}
Status Pass(PosType b)
{
if(m[b.x][b.y] == 1)
return OK;
else
return ERROR;
}
void FootPrint(PosType b)
{
m[b.x][b.y] = curstep;
}
void NextPos(PosType &b, int di)
{
b.x += direc[di].x;
b.y += direc[di].y;
}
void MarkPrint(PosType b)
{
m[b.x][b.y] = -1;
}
Status MazePath(PosType start, PosType end)
{
PosType curpos = start;
SqStack S;
SElemType e;
InitStack(S);
do
{
if(Pass(curpos) == OK)
{
FootPrint(curpos);
e.ord = curstep;
e.seat = curpos;
e.di = 0;
Push(S, e);
curstep++;
if(curpos.x == end.x && curpos.y == end.y)
return TRUE;
NextPos(curpos, e.di);
}
else
{
if(!StackEmpty(S))
{
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);
curstep++;
curpos = e.seat;
NextPos(curpos, e.di);
}
}
}
}while(!StackEmpty(S));
return FALSE;
}
int main()
{
Init();
if(MazePath(begin, end))
{
printf("此迷宫从入口到出口的一条路径如下:\n");
Print();
}
else
{
printf("此迷宫没有从入口到出口的路径\n");
}
return 0;
}
Maze
最新推荐文章于 2024-01-11 21:11:47 发布