#头文件
//Stack.h
#include
#include
#include
#include
#include"Maze.h"
#define MAX 100
typedef struct Position DataType;
typedef struct Stack
{
int top;
DataType stack[MAX];
}Stack;
void StackInit(Stack* s);
void StackPush(Stack* s, DataType data);
void StackPop(Stack* s);
DataType GetStackTop(Stack*s);
int GetStackSize(Stack*s);
int StackEmpty(Stack *s);
#pragma once
//Maze.h
#include
#include
#include
#define ROW 6
#define COL 6
typedef struct Maze
{
int _map[ROW][COL];
}Maze;
typedef struct Position
{
int _x;
int _y;
}Position;
// 初始化迷宫
void InitMaze(Maze* m, int map[ROW][COL]);
// 打印迷宫
void PrintMaze(Maze* m);
// 检测是否为有效入口
int IsValidEnter(Maze* m, Position enter);
// pos 必须在地图中 pos位置如果是1
int IsPass(Maze* m, Position pos);
// 检测是否在出口的位置
int IsExit(Position pos, Position enter);
// 走迷宫
void PassMaze(Maze* m, Position enter);
#操作函数
//Stack.c
#include"Stack.h"
void StackInit(Stack* s)
{
s->top = 0;
}
void StackPush(Stack* s, DataType data)
{
if (s->top == MAX)
{
printf("栈已满!无法入栈元素\n");
return;
}
(s->top)++;
s->stack[s->top] = data;
}
void StackPop(Stack *s)
{
if (s->top == 0)
{
printf("栈为空,无法出栈元素\n");
return;
}
(s->top)--;
}
DataType GetStackTop (Stack*s)
{
if (s->top == 0)
{
printf("栈为空,无法获取栈顶\n");
}
return s->stack[s->top];
}
int GetStackSize(Stack*s)
{
return s->top;
}
int StackEmpty(Stack *s)
{
if (s->top == 0)
{
return 1;
}
else
{
return 0;
}
}
//Maze.c
#include"Maze.h"
#include"Stack.h"
void InitMaze(Maze * m, int map[ROW][COL])
{
int i = 0;
int j = 0;
assert(m);
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
m->_map[i][j] = map[i][j];
}
}
}
void PrintMaze(Maze* m)
{
assert(m);
int i = 0;
int j = 0;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
printf(" %d ", m->_map[i][j]);
}
printf("\n");
}
}
int IsValidEnter(Maze* m, Position enter)
{
assert(m);
if (enter._x == ROW - 1 || enter._x == 0 || enter._y == COL - 1 || enter._y == 0)
{
return 1;
}
else
return 0;
}
// pos 必须在地图中 pos位置如果是1
int IsPass(Maze* m, Position pos)
{
assert(m);
if (pos._x >= ROW || pos._x < 0 || pos._y >= COL || pos._y >= COL)
{
return 0;
}
else if (m->_map[pos._x][pos._y] != 1)
{
return 0;
}
else
return 1;
}
// 检测是否在出口的位置
int IsExit(Position pos, Position enter)
{
if ((pos._x == ROW - 1 || pos._x == 0 || pos._y == COL - 1 || pos._y == 0) && ((pos._x != enter._x) || (pos._y) != enter._y))
{
return 1;
}
return 0;
}
void PassMaze(Maze* m, Position enter)
{
Stack s;
Position cur, next;
if (!IsValidEnter(m, enter))
{
printf("没有入口!\n");
return;
}
StackInit(&s);
StackPush(&s, enter);
while (!StackEmpty(&s))
{
cur = GetStackTop(&s);
if (IsExit(cur, enter))
{
return;
}
m->_map[cur._x][cur._y] = 2;
next = cur;
next._x = next._x - 1;
if (IsPass(m, next))
{
StackPush(&s, next);
continue;
}
next._y = next._y - 1;
if (IsPass(m, next))
{
StackPush(&s, next);
continue;
}
next._y = next._y + 1;
if (IsPass(m, next))
{
StackPush(&s, next);
continue;
}
next._x = next._x + 1;
if (IsPass(m, next))
{
StackPush(&s, next);
continue;
}
//cur 四个方向都走不通;
m->_map[cur._x][cur._y] = 3;
StackPop(&s);
}
}
#验证操作
//test.c
#include "Maze.h"
#include "Stack.h"
int main()
{
int map[ROW][COL] ={
{ 0,0,0,0,0,0 },
{ 0,0,1,0,0,0 },
{ 0,0,1,0,0,0 },
{ 0,0,1,1,1,0 },
{ 0,0,1,0,1,1 },
{ 0,0,1,0,0,0 }
};
Maze m;
InitMaze(&m, map);
Position enter;
enter._x = 5;
enter._y = 2;
PrintMaze(&m);
PassMaze(&m, enter);
printf("\n");
PrintMaze(&m);
system("pause");
return 0;
}