版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
#include <iostream>
#include <Windows.h>
#include <iomanip>
#include <time.h>
/***************内存检测区*********************/
#define _CRTDBG_MAP_ALLOC //* 这个是 相当于一种开关 打开这个内存检测的开关
#include <cstdlib> //*
#include <crtdbg.h> //*
#ifdef _DEBUG //*
#ifndef DBG_NEW //*
#define DBG_NEW new (_NORMAL_BLOCK, __FILE__, __LINE__)//*
#define new DBG_NEW //*
#endif //*
#endif
/***************宏定义和类型转换区*****************/
#define MAX_SIZE 128
#define STACK_S typedef
typedef struct Point_c {
int x;
int y;
}point;
#define ELEM_TYPE Point_c
#define WIDTH_X 15
#define LENTH_Y 10
using namespace std;
/***************结构体区*********************/
STACK_S struct stack_s {
ELEM_TYPE* base; // 栈底标记
ELEM_TYPE* top; // 栈顶元素
}stack;
/************全局变量**************/
int map[LENTH_Y][WIDTH_X]{
{0,0,0,1,0,0,0,0,0,0,0,1,0,0,0},
{0,0,1,1,1,1,0,0,0,1,1,1,1,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,1,1,1,1,1,1,1,1,1,1,1,0,0},
{0,0,0,1,1,1,1,1,1,1,1,1,0,0,0},
{0,0,0,0,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,1,1,1,1,1,0,0,0,0,0},
{0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
/***************函数定义区*********************/
bool isEmpty(stack& st);
bool isFull(stack& st);
// 1.初始化栈
bool initStack(stack& st);
// 2.入栈 e是要入栈的元素
bool pushStack(stack& st, ELEM_TYPE e);
// 3.出栈 e是要出栈的元素
bool popStack(stack& st, ELEM_TYPE& e);
// 4.销毁栈
void destroyStack(stack& st);
// 5.栈的遍历
void printStack(stack& st);
// 6.栈的长度
int stackLen(stack& st);
/**************与迷宫相关的函数*****************/
bool isValidStart(point& start);
// start 是入口 end 是出口
void play(point& start, point& end, stack& st);
// 判断下一个 步时否符合要求
bool isValidNext(point& next);
// 判断终点是否有效
bool isValidEnd(point& end, point& start);
// 定时器 函数
void lastTime();
/***************用户接口区*********************/
int main(void) {
stack st;
// 1.初始化
if (initStack(st)) {
printf_s("初始化成功!\n");
}else {
cerr << "栈初始化失败!" << endl;
exit(-1);
}
point start; //迷宫的入口
start.x = 3;
start.y = 0;
point end; ///用于保存出口的坐标
// 走出迷宫的实现
play(start, end, st);
printf_s("迷宫的出口为:end, x坐标为:%d, y坐标为:%d \n", end.x, end.y);
// 遍历地图
for (int i = 0; i < LENTH_Y; i++) {
for (int j = 0; j < WIDTH_X; j++) {
cout << setw(3) <<map[i][j] ;
}
cout << endl;
cout << endl;
}
printf_s("栈的长度为 :%d \n", stackLen(st));
cout << "是不是不够明显?" << endl;
Sleep(5000);
cout << "那么....." << endl;
Sleep(4000);
system("cls");
cout << "***************哈****************哈***************" << endl;
for (int i = 0; i < LENTH_Y; i++) {
for (int j = 0; j < WIDTH_X; j++) {
if (map[i][j]) {
cout << setw(3) << map[i][j];
}
else {
cout << setw(3) << ' ';
}
}
cout << endl;
cout << endl;
}
cout << "开启我们的快乐迷宫!" << endl;
//printStack(st);
destroyStack(st);
system("pause");
_CrtDumpMemoryLeaks();
return 0;
}
/***************函数定义区*********************/
bool initStack(stack& st) {
st.base = new ELEM_TYPE[MAX_SIZE];
if (!st.base) {
return false;
}
st.top = st.base;
return true;
}
bool pushStack(stack& st, ELEM_TYPE e) {
if ((st.top - st.base) == MAX_SIZE) {//栈已经满了
return false;
}
*(st.top++) = e;
return true;
}
bool popStack(stack& st, ELEM_TYPE& e) {
if (st.base == st.top) {//栈为空
return false;
}
e = *(--st.top);
return true;
}
void destroyStack(stack& st) {
st.top = nullptr;
if(st.base) delete[] st.base;
return;
}
inline bool isEmpty(stack& st) {
if (st.base == st.top) {
return true;
}else {
return false;
}
}
inline bool isFull(stack& st) {
if (st.top - st.base == MAX_SIZE) {
return true;
}else {
return false;
}
}
void printStack(stack& st) {
auto tmp = st.base;
int i = 1;
while (tmp != st.top) {
printf_s("第 %d 个坐标的 x: %d, y: %d \n", i++, tmp->x, tmp->y);
tmp++;
}
return;
}
int stackLen(stack& st) {
auto len = st.top - st.base;
return len;
}
bool isValidStart(point& start) {
if ( (start.x == 0 || start.y == 0
|| start.x == WIDTH_X - 1 || start.y == LENTH_Y - 1)
&& map[start.y][start.x] == 1 )
{
return true;
}
else
return false;
}
bool isValidNext(point& next) {
if ( next.x >= 0 && next.x <= WIDTH_X-1
&& next.y >= 0 && next.y <= LENTH_Y-1
&& map[next.y][next.x] == 1 )
{
return true;
}
else
return false;
}
bool isValidEnd(point& end, point& start) {
if ((end.x == 0 || end.y == 0
|| end.x == WIDTH_X - 1 || end.y == LENTH_Y - 1)
&& (end.x != start.x || end.y != start.y))
{
return true;
}
else
return false;
}
void play(point& start, point& end, stack& st) {
point next; // 记录下一步的坐标
point cur = start; // 初始化当前的坐标
if (isValidStart(start)) {
pushStack(st, cur); // 如果当前坐标有效 就入栈
map[start.y][start.x]++; // 当前坐标的值加一
}
while ( !isEmpty(st) ){
cur = *(st.top - 1); //栈顶的元素 就是当前的坐标
end = *(st.top - 1); //刷新一下 最后的元素 以便判断
if (isValidEnd(end, start)) {//如果出口是合法的
return;
}
next = cur;
next.y -= 1; //向上
if (isValidNext(next)) {
pushStack(st, next);
map[next.y][next.x] = map[cur.y][cur.x] + 1;//下一个位置比上一个位置多一
continue;
}
next = cur; //上面的next的无效的话 在重新赋值
next.x -= 1; //向左
if (isValidNext(next)) {
pushStack(st, next);
map[next.y][next.x] = map[cur.y][cur.x] + 1;//下一个位置比上一个位置多一
continue;
}
next = cur; //上面的next的无效的话 在重新赋值
next.y += 1; //向下
if (isValidNext(next)) {
pushStack(st, next);
map[next.y][next.x] = map[cur.y][cur.x] + 1;//下一个位置比上一个位置多一
continue;
}
next = cur; //上面的next的无效的话 在重新赋值
next.x += 1; //向右
if (isValidNext(next)) {
pushStack(st, next);
map[next.y][next.x] = map[cur.y][cur.x] + 1;//下一个位置比上一个位置多一
continue;
}
point tmp; //用于记录 出栈的一点
popStack(st, tmp);
}
return;
}
void lastTime() {
int n = 2;
clock_t start = clock();
clock_t last = 10 * CLOCKS_PER_SEC;
while (clock() - start < last);
return;
}