迷宫求解

抽象结构见严蔚敏版《数据结构》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;		
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值