#pragma once
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREASMENT 10
#define OVERFLOW -2
#define ERROR 0
#define OK 1
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef struct
{
int x, y;
}PosType; //位置数据结构
typedef struct
{
int ord;
PosType seat;
int di;
}SElemType; //Stack ElemType,包含位置,路径长度和下一步走的方
//向di三个成员
typedef int** MazeType; //二维数组指针MazeType
typedef struct //Sequence Stack,包含栈底栈底指针以及分配的内存空
//间三个成员
{
SElemType* base, * top;
int stacksize;
}SqStack;
Status Push(SqStack* S, SElemType e)
{
if (S->top - S->base >= S->stacksize)
{
SElemType* base;
base = (SElemType*)realloc(S->base, (S->stacksize + STACKINCREASMENT) * sizeof(SElemType));
if (!base) exit(OVERFLOW);
S->base = base;
S->top = S->base + S->stacksize;
S->stacksize += STACKINCREASMENT;
}
*S->top = e;
S->top++;
return OK;
}
Status Pop(SqStack* S, SElemType* e)
{
if (S->top == S->base) return ERROR;
S->top--;
*e = *S->top;
return OK;
}
Status InitStack(SqStack* S)
{
S->base = (SElemType*)malloc(sizeof(SElemType) * STACK_INIT_SIZE);
if (!S->base) exit(OVERFLOW);
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
return OK;
}
Status GetTop(SqStack* S, SElemType* e)
{
if (S->base == S->top) return ERROR;
*e = *(S->top - 1);
return OK;
}
Status StackEmpty(SqStack S)
{
if (S.base == S.top) return TRUE;
else return FALSE;
}
Status InitMaze(MazeType* maze, int row, int col) //以0与1表示墙壁和通路
{
*maze = (MazeType)malloc(sizeof(int*) * row);
if (!(*maze)) exit(OVERFLOW);
for (int i = 0; i < row; i++)
{
(*maze)[i] = (int*)malloc(sizeof(int) * col);
if (!(*maze)[i]) exit(OVERFLOW);
for (int j = 0; j < col; j++)
{
printf("请输入第%d行第%d列的状态\n", i, j);
scanf("%d", &(*maze)[i][j]);
}
}
return OK;
}
Status GetNext(SElemType* e) //根据当前的位置以及方向指示获得下一个位置的坐标
{
int di = e->di;
switch (di)
{
case 1:e->seat.x++; break;
case 2:e->seat.y--; break;
case 3:e->seat.x--; break;
case 4:e->seat.y++; break;
default:return ERROR;
}
e->ord++;
e->di = 1;
return OK;
}
void PrintStack(SqStack S)
{
SElemType* p = S.base;
while (p != S.top)
{
printf("(%d,%d)->", p->seat.x, p->seat.y);
p++;
}
}
Status MazePath(MazeType maze, PosType st, PosType en)
{
SqStack S;
SElemType* next;
next = (SElemType*)malloc(sizeof(SElemType));
if (!next) exit(OVERFLOW);
InitStack(&S);
int row, col;
printf("请输入迷宫行数和列数\n");
scanf("%d%d", &row, &col);
InitMaze(&maze, row, col);
SElemType cur;
cur.seat.x = st.x;
cur.seat.y = st.y;
cur.di = 1;
cur.ord = 1;
Push(&S, cur);
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
printf("%d\t", maze[i][j]);
printf("\n");
}
maze[1][1] = 0;
while (!StackEmpty(S))
{
GetTop(&S, next);
GetNext(next); //获得栈顶元素的下一个元素
if (maze[next->seat.x][next->seat.y] == 0)//如果下一个元素无法通行,更换方向
{ //如果最终四个方向均无法通行,将此地设为0
Pop(&S, &cur);
if (cur.di < 4)
{
cur.di++;
Push(&S, cur);
}
else
maze[cur.seat.x][cur.seat.y] = 0;
}
else //如果可以通行,不是终点就入栈
{ //是终点就打印栈中从底到顶所有位置
cur = *next;
if (cur.seat.x != en.x || cur.seat.y != en.y)
{
Push(&S, cur);
maze[cur.seat.x][cur.seat.y] = 0;
}
else
{
PrintStack(S);
printf("(%d,%d)", en.x, en.y);
return OK;
}
}
}
printf("无有效路径\n");
return ERROR;
}
//主函数
int main()
{
MazeType* maze;
maze = (MazeType*)malloc(sizeof(MazeType));
if (!maze) return ERROR;
PosType st, en;
printf("请输入起点与终点的坐标值\n");
scanf("%d%d%d%d", &st.x, &st.y, &en.x, &en.y);
MazePath(*maze, st, en);
return 0;
}
输入(严蔚敏数据结构P50):
1 1 8 8
10 10
0 0 0 0 0 0 0 0 0 0
0 1 1 0 1 1 1 0 1 0
0 1 1 0 1 1 1 0 1 0
0 1 1 1 1 0 0 1 1 0
0 1 0 0 0 1 1 1 1 0
0 1 1 1 0 1 1 1 1 0
0 1 0 1 1 1 0 1 1 0
0 1 0 0 0 1 0 0 1 0
0 0 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
结果: