#pragma once
/************************************/
//Stack.h
#define MAXSIZE 10
//typedef int SDataType;
//typedef char SDataType;
///////////////////////////
//迷宫定义的结构体与栈的数据类型不同
typedef struct Position
{
int _x;
int _y;
}Position;
typedef Position SDataType;
//////////////////////////
typedef struct Stack
{
SDataType _array[MAXSIZE];
int _top; //标记栈顶元素
}Stack;
//初始化
void StackInit(Stack *ps);
//压栈
void StackPush(Stack *ps, SDataType data);
//出栈
void StackPop(Stack *ps);
//返回栈顶元素
SDataType StackTop(Stack *ps);
//返回栈空间长度
int StackSize(Stack *ps);
//栈是否为空
int StackEmpty(Stack *ps);
/****************************************/
//Stack.c
#include <assert.h>
#include <stdio.h>
#include <string.h>
//初始化
void StackInit(Stack *ps)
{
assert(ps);
ps->_top = 0;
}
//压栈
void StackPush(Stack *ps, SDataType data)
{
assert(ps);
if (MAXSIZE == ps->_top)
{
printf("栈空间已满,无法插入!!!\N");
return;
}
ps->_array[ps->_top++] = data;
}
//出栈
void StackPop(Stack *ps)
{
assert(ps);
if (StackEmpty(ps))
return;
ps->_top--;
}
//返回栈顶元素
SDataType StackTop(Stack *ps)
{
assert(ps);
return ps->_array[ps->_top - 1];
}
//返回栈空间长度
int StackSize(Stack *ps)
{
assert(ps);
return ps->_top;
}
//栈是否为空
int StackEmpty(Stack *ps)
{
assert(ps);
return 0 == ps->_top;
}
/////////////////////////////////////////////////
#pragma once
#include <assert.h>
#include <stdio.h>
#include "Stack.h"
//typedef struct Position
//{
// int _x;
// int _y;
//}Position;
#define MAX_ROW 6
#define MAX_COL 6
typedef struct Maze
{
int _map[MAX_ROW][MAX_COL];
}Maze;
//初始化迷宫
void InitMaze(Maze *m, int map[MAX_ROW][MAX_COL])
{
int i = 0;
int j = 0;
assert(m);
for (i = 0; i < MAX_ROW; i++)
{
for (j = 0; j < MAX_COL; j++)
{
m->_map[i][j] = map[i][j];
}
}
}
//打印迷宫
void PrintMaze(Maze *m)
{
int i = 0;
int j = 0;
assert(m);
for (i = 0; i < MAX_ROW; i++)
{
for (j = 0; j < MAX_COL; j++)
{
printf("%d ", m->_map[i][j]);
}
printf("\n");
}
printf("\n");
}
//检测有效入口
int IsValidEntry(Maze *m, Position entry)
{
if (entry._x == 0 || entry._x == MAX_ROW - 1
|| entry._y == 0 || entry._y == MAX_COL - 1)
{
if (1 == m->_map[entry._x][entry._y])
return 1;
}
return 0;
}
//检测出口
int IsExit(Maze *m, Position entry, Position cur)
{
if (cur._x == entry._x && cur._y == entry._y)
{
return 0;
}
if (cur._x == 0 || cur._x == MAX_ROW - 1
|| cur._y == 0 || cur._y == MAX_COL - 1)
{
return 1;
}
return 0;
}
//判断下一步是否能走:下一步为 1 || 大于当前步
int IsNextPass(Maze *m, Position cur, Position next)
{
if (m->_map[next._x][next._y] == 1 || m->_map[next._x][next._y] > m->_map[cur._x][cur._y])
return 1;
return 0;
}
//将shortPath栈更新为最短路径、
void SaveshortPath(Stack *shortPath, Stack *path)
{
int i = 0;
for (; i < StackSize(path); ++i)
{
StackPush(shortPath, path->_array[i]);
}
}
//走迷宫具体方式
void GetPassMaze(Maze *m, Position entry, Position cur, Stack *path, Stack *shortPath)
{
Position next;
if (cur._x == entry._x && cur._y == entry._y)
{
m->_map[cur._x][cur._y] = 2;
StackPush(path, cur);
}
if (IsExit(m, entry, cur))
{
if (StackEmpty(shortPath) || StackSize(path) < StackSize(shortPath))
SaveshortPath(shortPath, path);
StackPop(path);
return;
}
//向上侧走
next = cur;
next._x -= 1;
if (IsNextPass(m, cur, next))
{
m->_map[next._x][next._y] = m->_map[cur._x][cur._y] + 1;
GetPassMaze(m, entry, next, path, shortPath);
}
//向左侧走
next = cur;
next._y -= 1;
if (IsNextPass(m, cur, next))
{
m->_map[next._x][next._y] = m->_map[cur._x][cur._y] + 1;
GetPassMaze(m, entry, next, path, shortPath);
}
//向右侧走
next = cur;
next._y += 1;
if (IsNextPass(m, cur, next))
{
m->_map[next._x][next._y] = m->_map[cur._x][cur._y] + 1;
GetPassMaze(m, entry, next, path, shortPath);
}
//向下方走
next = cur;
next._x += 1;
if (IsNextPass(m, cur, next))
{
m->_map[next._x][next._y] = m->_map[cur._x][cur._y] + 1;
GetPassMaze(m, entry, next, path, shortPath);
}
StackPop(path);
}
//走迷宫
void PassMaze(Maze *m, Position entry, Stack *shortPath)
{
Stack path;
if (!IsValidEntry(m, entry))
{
printf("入口无效!!!\n");
return;
}
StackInit(&path);
StackInit(shortPath);
GetPassMaze(m, entry, entry, &path, shortPath);
}
//测试函数
void TestMaze()
{
int map[MAX_ROW][MAX_COL] = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 1, 0 },
{ 0, 0, 1, 0, 1, 0 },
{ 0, 0, 1, 0, 1, 0 },
{ 0, 0, 1, 1, 1, 1 },
{ 0, 0, 1, 0, 0, 0 },
};
Maze m;
Stack shortPath;
Position entry;
InitMaze(&m, map);
PrintMaze(&m);
entry._x = 5;
entry._y = 2;
PassMaze(&m, entry, &shortPath);
PrintMaze(&m);
}
//////////////////////////////////////////////////////////////////
#include "MazeOP.h"
int main()
{
TestMaze();
system("pause");
return 0;
}
带环迷宫
最新推荐文章于 2018-10-09 12:43:59 发布