问题描述
源码如下:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
int mazeArr[10][10] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 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, 9, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
typedef struct {
int x;
int y;
}PosType;
typedef struct {
int ord;
PosType seat;
int di;
}SElemType;
typedef struct {
int row, col;
int arr[10][10];
}MazeType;
typedef struct SNode {
SElemType data;
struct SNode* next;
} SNode, * LinkStack;
Status visit(SElemType e);
Status InitStack(LinkStack& S) {
S = (LinkStack)malloc(sizeof(SNode));
if (!S)
exit(OVERFLOW);
S->next = NULL;
return OK;
}
Status DestroyStack(LinkStack& S) {
LinkStack p = S->next, ptmp;
while (p) {
ptmp = p->next;
free(p);
p = ptmp;
}
free(S);
return OK;
}
Status ClearStack(LinkStack& S) {
LinkStack p = S->next, ptmp;
while (p) {
ptmp = p->next;
free(p);
p = ptmp;
}
S->next = NULL;
return OK;
}
Status StackEmpty(LinkStack S) {
if (S->next == NULL)
return TRUE;
else
return FALSE;
}
int StackLength(LinkStack S) {
int n = 0;
LinkStack p = S->next;
while (p) {
n++;
p = p->next;
}
return n;
}
Status GetTop(LinkStack S, SElemType& e) {
if (S->next == NULL)
return ERROR;
e = S->next->data;
return OK;
}
Status Push(LinkStack& S, SElemType e) {
LinkStack p = (LinkStack)malloc(sizeof(SNode));
p->data = e;
p->next = S->next;
S->next = p;
return OK;
}
Status Pop(LinkStack& S, SElemType& e) {
if (S->next == NULL)
return ERROR;
e = S->next->data;
LinkStack ptmp = S->next->next;
free(S->next);
S->next = ptmp;
return OK;
}
Status visit(SElemType e) {
printf(" 序号:%2d, 坐标:(%d,%d), 方向:", e.ord, e.seat.x, e.seat.y);
switch (e.di)
{
case 1: printf("东"); break;
case 2: printf("南"); break;
case 3: printf("西"); break;
case 4: printf("北"); break;
}
printf("\n");
return OK;
}
Status StackTraverse(LinkStack S, Status(*pfn_visit)(SElemType)) {
if (S->next == NULL)
{
printf("栈为空!\n");
return ERROR;
}
for (int i = StackLength(S); i > 0; i--)
{
LinkStack p = S->next;
int j = 1;
while (p && j < i) {
p = p->next;
++j;
}
visit(p->data);
}
printf("\n");
return OK;
}
Status StackTraverse_Top(LinkStack S, Status(*pfn_visit)(SElemType)) {
if (S->next == NULL)
{
printf("栈为空!\n");
return ERROR;
}
LinkStack p = S->next;
while (p) {
visit(p->data);
p = p->next;
}
printf("\n");
return OK;
}
Status InitMaze(MazeType& maze)
{
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
maze.arr[i][j] = mazeArr[i][j];
maze.row = 10;
maze.col = 10;
return OK;
}
Status Pass(MazeType maze, PosType curpos)
{
if (maze.arr[curpos.x][curpos.y] == 1 ||
maze.arr[curpos.x][curpos.y] == 8 ||
maze.arr[curpos.x][curpos.y] == 9)
return TRUE;
else return FALSE;
}
Status FootPrint(MazeType& maze, PosType curpos)
{
maze.arr[curpos.x][curpos.y] = 2;
return OK;
}
Status MarkPrint(MazeType& maze, PosType curpos)
{
maze.arr[curpos.x][curpos.y] = 3;
return OK;
}
SElemType CreatSElem(int curstep, PosType curpos, int di)
{
SElemType e;
e.ord = curstep;
e.seat = curpos;
e.di = di;
return e;
}
PosType NextPos(PosType curpos, int di)
{
PosType pos = curpos;
switch (di)
{
case 1: pos.y++; break;
case 2: pos.x++; break;
case 3: pos.y--; break;
case 4: pos.x--; break;
}
return pos;
}
Status PosEquare(PosType curpos, PosType end)
{
if (curpos.x == end.x && curpos.y == end.y)
return TRUE;
else return FALSE;
}
void PrintMaze(MazeType maze)
{
printf(" ");
for (int i = 0; i < 10; i++)
printf("%2d", i);
printf("\n");
for (int i = 0; i < 10; i++)
{
printf("%2d", i);
for (int j = 0; j < 10; j++)
{
switch (maze.arr[i][j])
{
case 0: printf("■"); break;
case 1: printf(" "); break;
case 2: printf(" +"); break;
case 3: printf(" x"); break;
case 8: printf("⊙"); break;
case 9: printf("○"); break;
}
}
printf("\n");
}
}
Status MazePath(MazeType& maze, PosType start, PosType end)
{
LinkStack S;
SElemType e;
InitStack(S);
PosType curpos = start;
int curstep = 1;
do
{
if (Pass(maze, curpos))
{
FootPrint(maze, curpos);
e = CreatSElem(curstep, curpos, 1);
Push(S, e);
if (PosEquare(curpos, end))
{
StackTraverse(S, visit);
DestroyStack(S);
return (TRUE);
}
curpos = NextPos(curpos, 1);
curstep++;
}
else
{
if (!(StackEmpty(S)))
{
Pop(S, e);
while (e.di == 4 && !StackEmpty(S))
{
MarkPrint(maze, e.seat);
Pop(S, e);
}
if (e.di < 4)
{
e.di++;
Push(S, e);
curpos = NextPos(e.seat, e.di);
}
}
}
} while (!StackEmpty(S));
return (FALSE);
}
int main() {
printf(" ");
for (int i = 0; i < 10; i++)
printf("%2d", i);
printf("\n");
for (int i = 0; i < 10; i++)
{
printf("%2d", i);
for (int j = 0; j < 10; j++)
printf("%2d", mazeArr[i][j]);
printf("\n");
}
printf("\n");
MazeType maze;
InitMaze(maze);
PrintMaze(maze);
PosType start, end;
printf("迷宫入口坐标:1 1");
start.x = 1; start.y = 1;
printf("迷宫出口坐标:8 8");
end.x = 8; end.y = 8;
printf("\n");
if (MazePath(maze, start, end))
{
maze.arr[start.x][start.y] = 8;
maze.arr[end.x][end.y] = 9;
PrintMaze(maze);
}
else
{
printf("迷宫无解!");
}
return 0;
}
运行截图: