用栈求解迷宫问题 C语言 数据结构

给定一个M*N的迷宫,求一条从指定入口到出口的迷宫路径。用栈采用顺序栈存储结构。代码如下 

#include
#include
#define MaxSize 100
#define M 8
#define N 8
int mg[M + 2][N + 2] = { {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}
                       };
typedef struct
{
	int i, j;
	int di; 
}Box;
typedef struct
{
	Box data[MaxSize];
	int top;
}StType;
void InitStack(StType*&s)
{
	s = (StType*)malloc(sizeof(StType));
	s->top = -1;
}
void DestroyStack(StType*&s)
{
	free(s);
}
bool StackEmpty(StType*s)
{
	return(s ->top == -1);
}
bool Push(StType*&s, Box e)
{
	if (s->top == MaxSize - 1)
		return false;
	s->top++;
	s->data[s->top] = e;
	return true;
}
bool Pop(StType*&s, Box &e)
{
	if (s->top == -1)
		return false;
	e = s->data[s->top];
	s->top--;
	return true;
}
bool GetTop(StType*s, Box &e)
{
	if (s->top == -1)
	{
		return false;
	}
	e = s->data[s->top];
	return true;
}
bool mgpath(int xi, int yi, int xe, int ye)	
{
	Box path[MaxSize], e;  
	int i, j, di, i1, j1, k;  
	bool find;
	StType *st;				
	InitStack(st);				
	e.i = xi; e.j = yi; e.di = -1;			
	Push(st, e);				
	mg[xi][yi] = -1;
	while (!StackEmpty(st))
	{
		GetTop(st, e);
		i = e.i; j = e.j; di = e.di;
		if (i == xe && j == ye)
		{
			printf("一条迷宫路径如下:\n");
			k = 0;
			while (!StackEmpty(st))
			{
				Pop(st, e);
				path[k++] = e;
			}
			while (k >= 1)
			{
				k--;
				printf("\t(%d,%d)", path[k].i, path[k].j);
				if ((k + 2) % 5 == 0)
					printf("\n");
			}
			printf("\n");
			DestroyStack(st);
			return true;
		}
		find = false;
		while (di < 4 && !find)
		{
			di++;
			switch (di)
			{
			case 0:i1 = i - 1; j1 = j;   break;
			case 1:i1 = i;   j1 = j + 1; break;
			case 2:i1 = i + 1; j1 = j;   break;
			case 3:i1 = i;   j1 = j - 1; break;
			}
			if (mg[i1][j1] == 0)  find = true;
		}
			if (find)
			{
				st->data[st->top].di = di;
				e.i = i1; e.j = j1; e.di = -1;
				Push(st, e);
				mg[i1][j1] = -1;

			}
			else
			{
				Pop(st, e);
				mg[e.i][e.j] = 0;

			}
		
	}
		DestroyStack(st);	
		return false;		
	}

                                          

int main()
{
	if (!mgpath(1, 1, M, N))
		printf("该迷宫问题没有解!");
	return 1;
}

对于迷宫的每个方块有上下左右4个相邻的方块。第i行第j列的当前方块的位置记为(i,j),规定上方方块为方位0,并按顺时针方向递增编号。在试探过程中,从0到3查找相邻的可走方块。在求解的过程中采用“穷举法”,一旦找到一个可走的相邻方块就继续走下去。为了防止死循环,一个方块进栈后将相应的mg数组元素值恢复为-1(不可走),当退栈屎将其恢复为0(可走)。 

运行结果如下 

用栈求解迷宫问题 C语言 数据结构_第1张图片

你可能感兴趣的:(c语言,c++,数据结构)