您的位置:首页 > 其它

迷宫求解

2014-12-10 08:54 148 查看
抽象结构见严蔚敏版《数据结构》3.2.4迷宫求解问题。

#include<stdio.h>
#include<memory.h>
#include<stdlib.h>

#define TRUE 1
#define STACK_INIT_SIZE 100
#define OVERFLOW -2
#define RANGE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
#define FALSE 0

typedef int Status;

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;

typedef struct{
Status block[RANGE][RANGE];
Status path[RANGE][RANGE];
}MazeType;

Status 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;
}

Status Push(MazeType &maze, 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;
maze.path[e.seat.y][e.seat.x]=1;
return OK;
}

Status Pop (MazeType &maze, SqStack &S, SElemType &e)
{
if (S.top == S.base)
return ERROR;
e = *--S.top;
maze.path[e.seat.y][e.seat.x]=0;
return OK;
}

Status StackEmpty (SqStack S)
{
if (S.top == S.base)
return OK;
else
return ERROR;
}

Status Pass(PosType P, MazeType maze)
{
return !maze.block[P.y][P.x];
}

void FootPrint(PosType P, MazeType &maze)
{
maze.block[P.y][P.x] = 1;
return;
}

PosType NextPos(PosType curpos, int di)
{
PosType temp;
switch (di)
{
case 1: temp.x = curpos.x + 1; 	temp.y = curpos.y; 		break;
case 2: temp.x = curpos.x; 		temp.y = curpos.y + 1; 	break;
case 3: temp.x = curpos.x - 1; 	temp.y = curpos.y; 		break;
case 4: temp.x = curpos.x; 		temp.y = curpos.y - 1; 	break;
default: break;
}
return temp;
}
void MarkPrint(PosType p, MazeType &maze)
{
maze.block[p.y][p.x] = 1;
return;
}

Status posCompare(PosType a, PosType b)
{
if (a.x == b.x && a.y == b.y)
return OK;
else
return ERROR;
}

Status MazePath(MazeType &maze, PosType start, PosType end)
{
SqStack S;
InitStack(S);
PosType curpos = start;
int curstep = 1;
SElemType e;
do
{
if (Pass(curpos, maze))
{
FootPrint(curpos, maze);
e = (SElemType){curstep, curpos, 1};
Push(maze, S, e);
if (posCompare(curpos, end))
{
return TRUE;
}
curpos = NextPos(curpos, 1);
curstep++;
}
else
{
if (!StackEmpty(S))
{
Pop(maze, S, e);
while (e.di == 4 && !StackEmpty(S))
{
MarkPrint(e.seat, maze);
Pop(maze, S, e);
}
if (e.di < 4)
{
e.di++;
Push(maze, S, e);
curpos = NextPos(e.seat, e.di);
}
}
}
}while (!StackEmpty(S));
return FALSE;
}
int main()
{
/*test code begin*/
MazeType test = {
{
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 1, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 1, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 0, 0, 1},
{1, 0, 1, 1, 1, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 1, 0, 0, 1},
{1, 0, 1, 1, 1, 0, 1, 1, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
}
};
memset(test.path, 0, sizeof(test.path));
PosType posStart = PosType{1,1};
PosType posStop = PosType{8,8};
MazePath(test, posStart, posStop);
for (int i = 0; i <= 10; i++)
{
for (int j = 0; j <= 10; j++)
printf("%d\t", test.path[i][j]);
printf("\n");
}
/*test code end*/
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: