迷宫和栈的头文件(.h)
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#define MAX_SIZE 100
#define N 6
typedef struct Pos{
int _x; //行
int _y; //列
}Pos;
typedef struct Maze
{
int _mz[N][N];
Pos _entry;
}Maze;
typedef Pos DataType;
typedef struct Stack
{
DataType _data[MAX_SIZE];
int _top;
}SqStack;
void SqStackInit(SqStack* Stack); //初始化
void Push(SqStack* Stack, DataType data); //压栈
void Pop(SqStack* Stack); //弹栈
Pos StackTop(SqStack*Stack); //栈顶元素个数
int StackTopMaxSize(); //栈能容纳的元素个数
int StackEmpty(SqStack* Stack); //判断栈是否为空
void MazeInit(Maze* m, int arr[][N]); //初始化
void ShortPath(SqStack* path, SqStack* shortpath); //最短路径
void MazeLoopGetShortPath(Maze* m, Pos cur, Pos entry, SqStack* path, SqStack* shortpath); //带环迷宫
void MazePrint(Maze* m); //打印迷宫
int MazeEntry(Pos entry); //判断迷宫入口是否正确
int CheckIsAccess(Maze* m, Pos cur, Pos next); //判断下一步是否正确
void PassMazeLoop(Maze* m, Pos entry, SqStack* path, SqStack* shortpath); //判断迷宫是否带环
功能模块
#include"May.h"
int IsExit(Maze* m, Pos cur, Pos entry);
void SqStackInit(SqStack* Stack)
{
assert(Stack);
Stack->_top = 0;
}
void Push(SqStack*Stack, DataType data)
{
assert(Stack);
if (Stack->_top == MAX_SIZE)
{
printf("栈已满!!!\n");
return;
}
Stack->_data[Stack->_top] = data;
++Stack->_top;
}
void Pop(SqStack*Stack)
{
assert(Stack);
if (Stack->_top == 0)
{
printf("栈已空\n");
return;
}
--Stack->_top;
}
Pos StackTop(SqStack* Stack)
{
assert(Stack);
return Stack->_data[Stack->_top - 1];
}
int StackTopMaxSize()
{
return MAX_SIZE;
}
int StackEmpty(SqStack* Stack)
{
assert(Stack);
if (0 == Stack->_top)
return 1;
return 0;
}
void MazeInit(Maze*m, int arr[][N])
{
int i = 0;
int j = 0;
assert(m);
for (; i < N; ++i)
{
for (j = 0; j<N; ++j)
{
m->_mz[i][j] = arr[i][j];
}
}
}
//最短路径
void ShortPath(SqStack* path, SqStack* shortpath)
{
int i = 0;
int size = path->_top;
for (; i < size; i++)
{
shortpath->_data[i] = path->_data[i];
}
shortpath->_top = i;
}
//带环迷宫
void MazeLoopGetShortPath(Maze* m, Pos cur, Pos entry, SqStack* path, SqStack* shortpath)
{
Pos next;
Push(path, cur);
if (IsExit(m, cur, entry))
{
if (StackEmpty(shortpath) || (path->_top < shortpath->_top))
{
ShortPath(path, shortpath);
}
Pop(path);
return;
}
cur = StackTop(path);
//上
next = cur;
next._x -= 1;
if (CheckIsAccess(m, cur, next))
{
m->_mz[next._x][next._y] = m->_mz[cur._x][cur._y] + 1;
MazeLoopGetShortPath(m, next, entry, path, shortpath);
}
//左
next = cur;
next._y -= 1;
if (CheckIsAccess(m, cur, next))
{
m->_mz[next._x][next._y] = m->_mz[cur._x][cur._y] + 1;
MazeLoopGetShortPath(m, next, entry, path, shortpath);
}
//右
next = cur;
next._y += 1;
if (CheckIsAccess(m, cur, next))
{
m->_mz[next._x][next._y] = m->_mz[cur._x][cur._y] + 1;
MazeLoopGetShortPath(m, next, entry, path, shortpath);
}
//下
next = cur;
next._x += 1;
if (CheckIsAccess(m, cur, next))
{
m->_mz[next._x][next._y] = m->_mz[cur._x][cur._y] + 1;
MazeLoopGetShortPath(m, next, entry, path, shortpath);
}
Pop(path);
return;
}
void PassMazeLoop(Maze* m, Pos entry, SqStack* path, SqStack* shortpath)
{
if (!MazeEntry(entry))
{
printf("入口不合法!\n");
return;
}
m->_mz[entry._x][entry._y] = 2;
MazeLoopGetShortPath(m, entry, entry, path, shortpath);
}
//判断迷宫入口是否正确
int MazeEntry(Pos entry)
{
if (entry._x == 0 || entry._x == N - 1 || \
entry._y == 0 || entry._y == N - 1)
{
return 1;
}
return 0;
}
//判断是否为出口
int IsExit(Maze* m, Pos cur, Pos entry)
{
if (((cur._x != entry._x&&cur._y != entry._y)) &&
(cur._x == N - 1 || cur._y == N - 1 ||
cur._x == 0 || cur._y == 0))
return 1;
return 0;
}
//判断下一步是否正确
int CheckIsAccess(Maze* m, Pos cur,Pos next)
{
assert(m);
if (next._x >= 0 && next._x<N && next._y >= 0 && next._y < N
&&m->_mz[next._x][next._y] == 1 ||
(m->_mz[cur._x][cur._y] < m->_mz[next._x][next._y]))
return 1;
return 0;
}
void MazePrint(Maze* m)
{
int i = 0;
int j = 0;
assert(m);
for (; i<N; ++i)
{
for (j = 0; j<N; ++j)
{
printf("%d ", m->_mz[i][j]);
}
printf("\n");
}
}
测试文件
#define _CRT_SECURE_NO_WARNINGS 1
#include"May.h"
int main()
{
Maze m;
SqStack path;
SqStack shortpath;
int a[N][N] = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 1, 0 },
{ 0, 0, 1, 0, 1, 0 },
{ 0, 0, 1, 1, 1, 1 },
{ 0, 0, 1, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0 }
};
MazeInit(&m, a);
m._entry._x = 5;
m._entry._y = 2;
SqStackInit(&path);
SqStackInit(&shortpath);
MazePrint(&m);
//MazeGetPath(&m);
PassMazeLoop(&m, m._entry, &path, &shortpath);
MazePrint(&m);
system("pause");
return 0;
}