- // 1. 对简单迷宫进行求解----只有一条通路,迷宫不带环
- // >> 用循环的方式求解简单迷宫问题
- #include <stdio.h>
- #include <stdlib.h>
- #include "stack.h"
- #define MAX_ROW 6
- #define MAX_COL 6
- // 判断是不是通路,判断是不是分支节点,入栈操作,判断是不是出口
- #define OPERATE if (IsPass(m, cur))\
- {\
- if (isBranch == 1)\
- StackPush(s, next);\
- isBranch = 0;\
- m->_map[cur.y][cur.x] = 2;\
- next = cur;\
- StackPush(s, cur);\
- if (IsMazeExit(m, cur, enter))\
- return;\
- continue;\
- }
- typedef struct Maze
- {
- int _map[MAX_ROW][MAX_COL];
- }Maze;
- // 初始化迷宫地图数据
- void InitMap(Maze* m, int map[MAX_ROW][MAX_COL]);
- // 检测迷宫的入口是否有效
- int IsValidEnter(Maze* m, Position enter);
- // 检测cur位置是否为迷宫的出口
- int IsMazeExit(Maze* m, Position cur, Position enter);
- // 检测当前位置是否为通路
- int IsPass(Maze* m, Position cur);
- // 走迷宫
- void PassMazeNor(Maze* m, Position enter, Stack* s);
- // 打印迷宫地图数据
- void PrintMap(Maze* m);
- // 打印路径
- void PrintPath(Stack* s);
- int main() {
- int map[MAX_ROW][MAX_COL] = {
- { 0, 0, 0, 0, 0, 0 },
- { 0, 1, 0, 1, 0, 0 },
- { 0, 1, 0, 1, 0, 0 },
- { 0, 1, 1, 1, 1, 1 },
- { 0, 0, 0, 1, 0, 0 },
- { 0, 0, 0, 1, 0, 0 },
- };
- Maze* m = malloc(sizeof(Maze));
- if (m == NULL)
- return;
- InitMap(m, map);
- PrintMap(m);
- Position enter = {3, 5};
- Stack* path = (Stack*)malloc(sizeof(Stack));
- StackInit(path);
- PassMazeNor(m, enter, path);
- // 打印路径
- printf("\n");
- PrintMap(m);
- PrintPath(path);
- return 0;
- }
- // 初始化迷宫地图数据
- void InitMap(Maze* m, int map[MAX_ROW][MAX_COL]) {
- for (int i = 0; i < MAX_COL; i++) {
- for (int j = 0; j < MAX_ROW; j++) {
- m->_map[i][j] = map[i][j];
- }
- }
- }
- // 检测迷宫的入口是否有效
- int IsValidEnter(Maze* m, Position enter) {
- if (m->_map[enter.y][enter.x] != 0)
- return 1;
- return 0;
- }
- // 检测cur位置是否为迷宫的出口
- int IsMazeExit(Maze* m, Position cur, Position enter) {
- if (cur.x != enter.x && cur.y != enter.y && (cur.x >= MAX_ROW - 1 || cur.y >= MAX_COL - 1 || cur.x <= 0 || cur.y <= 0)) {
- printf("找到出口\n");
- return 1;
- }
- return 0;
- }
- // 检测当前位置是否为通路
- int IsPass(Maze* m, Position cur) {
- if (m->_map[cur.y][cur.x] == 1)
- return 1;
- return 0;
- }
- // 走迷宫
- void PassMazeNor(Maze* m, Position enter, Stack* s) {
- if (!IsValidEnter(m, enter))
- return;
- //判断是不是分支节点
- int isBranch = 0;
- m->_map[enter.y][enter.x] = 2;
- Position cur = enter;
- Position next = enter;
- StackPush(s, enter);
- while (1) {
- //左
- cur = next;
- cur.x--;
- //宏:定义了走迷宫的步骤
- OPERATE;
- //上
- cur = next;
- cur.y--;
- OPERATE;
- //右
- cur = next;
- cur.x++;
- OPERATE;
- //下
- cur = next;
- cur.y++;
- OPERATE;
- if (StackEmpty(s))
- return;
- StackPop(s, &next);
- isBranch = 1;
- }
- }
- // 打印迷宫地图数据
- void PrintMap(Maze* m) {
- for (int i = 0; i < MAX_COL; i++) {
- for (int j = 0; j < MAX_ROW; j++) {
- printf("%d ", m->_map[i][j]);
- }
- printf("\n");
- }
- }
- // 打印路径
- void PrintPath(Stack* s) {
- Position position;
- Stack* path = (Stack*)malloc(sizeof(Stack));
- StackInit(path);
- while (!StackEmpty(s)) {
- StackPop(s, &position);
- StackPush(path, position);
- }
- while (!StackEmpty(path)) {
- StackPop(path, &position);
- printf("(%d, %d)-->", position.x, position.y);
- }
- }
// 1. 对简单迷宫进行求解----只有一条通路,迷宫不带环
// >> 用循环的方式求解简单迷宫问题
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#define MAX_ROW 6
#define MAX_COL 6
// 判断是不是通路,判断是不是分支节点,入栈操作,判断是不是出口
#define OPERATE if (IsPass(m, cur))\
{\
if (isBranch == 1)\
StackPush(s, next);\
isBranch = 0;\
m->_map[cur.y][cur.x] = 2;\
next = cur;\
StackPush(s, cur);\
if (IsMazeExit(m, cur, enter))\
return;\
continue;\
}
typedef struct Maze
{
int _map[MAX_ROW][MAX_COL];
}Maze;
// 初始化迷宫地图数据
void InitMap(Maze* m, int map[MAX_ROW][MAX_COL]);
// 检测迷宫的入口是否有效
int IsValidEnter(Maze* m, Position enter);
// 检测cur位置是否为迷宫的出口
int IsMazeExit(Maze* m, Position cur, Position enter);
// 检测当前位置是否为通路
int IsPass(Maze* m, Position cur);
// 走迷宫
void PassMazeNor(Maze* m, Position enter, Stack* s);
// 打印迷宫地图数据
void PrintMap(Maze* m);
// 打印路径
void PrintPath(Stack* s);
int main() {
int map[MAX_ROW][MAX_COL] = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 0 },
{ 0, 1, 0, 1, 0, 0 },
{ 0, 1, 1, 1, 1, 1 },
{ 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 1, 0, 0 },
};
Maze* m = malloc(sizeof(Maze));
if (m == NULL)
return;
InitMap(m, map);
PrintMap(m);
Position enter = {3, 5};
Stack* path = (Stack*)malloc(sizeof(Stack));
StackInit(path);
PassMazeNor(m, enter, path);
// 打印路径
printf("\n");
PrintMap(m);
PrintPath(path);
return 0;
}
// 初始化迷宫地图数据
void InitMap(Maze* m, int map[MAX_ROW][MAX_COL]) {
for (int i = 0; i < MAX_COL; i++) {
for (int j = 0; j < MAX_ROW; j++) {
m->_map[i][j] = map[i][j];
}
}
}
// 检测迷宫的入口是否有效
int IsValidEnter(Maze* m, Position enter) {
if (m->_map[enter.y][enter.x] != 0)
return 1;
return 0;
}
// 检测cur位置是否为迷宫的出口
int IsMazeExit(Maze* m, Position cur, Position enter) {
if (cur.x != enter.x && cur.y != enter.y && (cur.x >= MAX_ROW - 1 || cur.y >= MAX_COL - 1 || cur.x <= 0 || cur.y <= 0)) {
printf("找到出口\n");
return 1;
}
return 0;
}
// 检测当前位置是否为通路
int IsPass(Maze* m, Position cur) {
if (m->_map[cur.y][cur.x] == 1)
return 1;
return 0;
}
// 走迷宫
void PassMazeNor(Maze* m, Position enter, Stack* s) {
if (!IsValidEnter(m, enter))
return;
//判断是不是分支节点
int isBranch = 0;
m->_map[enter.y][enter.x] = 2;
Position cur = enter;
Position next = enter;
StackPush(s, enter);
while (1) {
//左
cur = next;
cur.x--;
//宏:定义了走迷宫的步骤
OPERATE;
//上
cur = next;
cur.y--;
OPERATE;
//右
cur = next;
cur.x++;
OPERATE;
//下
cur = next;
cur.y++;
OPERATE;
if (StackEmpty(s))
return;
StackPop(s, &next);
isBranch = 1;
}
}
// 打印迷宫地图数据
void PrintMap(Maze* m) {
for (int i = 0; i < MAX_COL; i++) {
for (int j = 0; j < MAX_ROW; j++) {
printf("%d ", m->_map[i][j]);
}
printf("\n");
}
}
// 打印路径
void PrintPath(Stack* s) {
Position position;
Stack* path = (Stack*)malloc(sizeof(Stack));
StackInit(path);
while (!StackEmpty(s)) {
StackPop(s, &position);
StackPush(path, position);
}
while (!StackEmpty(path)) {
StackPop(path, &position);
printf("(%d, %d)-->", position.x, position.y);
}
}
栈的操作:
- #include "stack.h"
- #include "assert.h"
- //初始化栈
- void StackInit(Stack* stack) {
- stack->_array = (DataType*)malloc(sizeof(DataType)*Init_StackSize);
- if (!stack->_array) {
- exit(0);
- }
- stack->_top = 0;
- stack->_capacity = Init_StackSize;
- }
- //压栈
- void StackPush(Stack* stack, DataType data) {
- assert(stack);
- //判断栈是否已满
- if (stack->_top >= stack->_capacity) {
- //申请10个空间
- stack->_array = (DataType*)realloc(stack->_array,
- sizeof(DataType)*(stack->_capacity + 10));
- //判断是否申请成功
- if (!stack->_array) {
- exit(0);
- }
- stack->_top = stack->_capacity;
- stack->_capacity = stack->_capacity + 10;
- }
- *(stack->_array + stack->_top) = data;
- stack->_top++;
- }
- //出栈
- int StackPop(Stack* stack, DataType* data) {
- assert(stack);
- //判断栈是否为空
- if (stack->_top == 0) {
- return 0;
- }
- //先减一再取值
- --stack->_top;
- *data = *(stack->_array + stack->_top);
- return 1;
- }
- //判断栈是否为空
- int StackEmpty(Stack* stack) {
- assert(stack);
- //为空返回1
- return stack->_top == 0 ? 1 : 0;
- }
#include "stack.h"
#include "assert.h"
//初始化栈
void StackInit(Stack* stack) {
stack->_array = (DataType*)malloc(sizeof(DataType)*Init_StackSize);
if (!stack->_array) {
exit(0);
}
stack->_top = 0;
stack->_capacity = Init_StackSize;
}
//压栈
void StackPush(Stack* stack, DataType data) {
assert(stack);
//判断栈是否已满
if (stack->_top >= stack->_capacity) {
//申请10个空间
stack->_array = (DataType*)realloc(stack->_array,
sizeof(DataType)*(stack->_capacity + 10));
//判断是否申请成功
if (!stack->_array) {
exit(0);
}
stack->_top = stack->_capacity;
stack->_capacity = stack->_capacity + 10;
}
*(stack->_array + stack->_top) = data;
stack->_top++;
}
//出栈
int StackPop(Stack* stack, DataType* data) {
assert(stack);
//判断栈是否为空
if (stack->_top == 0) {
return 0;
}
//先减一再取值
--stack->_top;
*data = *(stack->_array + stack->_top);
return 1;
}
//判断栈是否为空
int StackEmpty(Stack* stack) {
assert(stack);
//为空返回1
return stack->_top == 0 ? 1 : 0;
}
运行结果截图: