迷宫求解

  1. // 1. 对简单迷宫进行求解----只有一条通路,迷宫不带环  
  2. // >> 用循环的方式求解简单迷宫问题  
  3. #include <stdio.h>  
  4. #include <stdlib.h>  
  5. #include "stack.h"  
  6. #define MAX_ROW 6   
  7. #define MAX_COL 6   
  8.   
  9. // 判断是不是通路,判断是不是分支节点,入栈操作,判断是不是出口  
  10. #define OPERATE if (IsPass(m, cur))\  
  11.                 {\  
  12.                     if (isBranch == 1)\  
  13.                         StackPush(s, next);\  
  14.                     isBranch = 0;\  
  15.                     m->_map[cur.y][cur.x] = 2;\  
  16.                     next = cur;\  
  17.                     StackPush(s, cur);\  
  18.                     if (IsMazeExit(m, cur, enter))\  
  19.                         return;\  
  20.                     continue;\  
  21.                 }  
  22.   
  23. typedef struct Maze  
  24. {  
  25.     int _map[MAX_ROW][MAX_COL];  
  26. }Maze;  
  27.   
  28. // 初始化迷宫地图数据   
  29. void InitMap(Maze* m, int map[MAX_ROW][MAX_COL]);  
  30.   
  31. // 检测迷宫的入口是否有效   
  32. int IsValidEnter(Maze* m, Position enter);  
  33.   
  34. // 检测cur位置是否为迷宫的出口   
  35. int IsMazeExit(Maze* m, Position cur, Position enter);  
  36.   
  37. // 检测当前位置是否为通路   
  38. int IsPass(Maze* m, Position cur);  
  39.   
  40. // 走迷宫   
  41. void PassMazeNor(Maze* m, Position enter, Stack* s);  
  42.   
  43. // 打印迷宫地图数据   
  44. void PrintMap(Maze* m);  
  45.   
  46. // 打印路径   
  47. void PrintPath(Stack* s);  
  48.   
  49. int main() {  
  50.     int map[MAX_ROW][MAX_COL] = {   
  51.         { 0, 0, 0, 0, 0, 0 },  
  52.         { 0, 1, 0, 1, 0, 0 },  
  53.         { 0, 1, 0, 1, 0, 0 },  
  54.         { 0, 1, 1, 1, 1, 1 },  
  55.         { 0, 0, 0, 1, 0, 0 },  
  56.         { 0, 0, 0, 1, 0, 0 },  
  57.     };  
  58.   
  59.     Maze* m = malloc(sizeof(Maze));  
  60.     if (m == NULL)  
  61.         return;  
  62.   
  63.     InitMap(m, map);  
  64.     PrintMap(m);  
  65.     Position enter = {3, 5};  
  66.     Stack* path = (Stack*)malloc(sizeof(Stack));  
  67.     StackInit(path);  
  68.     PassMazeNor(m, enter, path);  
  69.     // 打印路径   
  70.     printf("\n");  
  71.     PrintMap(m);  
  72.     PrintPath(path);  
  73.     return 0;  
  74. }  
  75.   
  76. // 初始化迷宫地图数据   
  77. void InitMap(Maze* m, int map[MAX_ROW][MAX_COL]) {  
  78.     for (int i = 0; i < MAX_COL; i++) {  
  79.         for (int j = 0; j < MAX_ROW; j++) {  
  80.             m->_map[i][j] = map[i][j];  
  81.         }  
  82.     }  
  83. }  
  84.   
  85. // 检测迷宫的入口是否有效   
  86. int IsValidEnter(Maze* m, Position enter) {  
  87.     if (m->_map[enter.y][enter.x] != 0)  
  88.         return 1;  
  89.     return 0;  
  90. }  
  91.   
  92. // 检测cur位置是否为迷宫的出口   
  93. int IsMazeExit(Maze* m, Position cur, Position enter) {  
  94.     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)) {  
  95.         printf("找到出口\n");  
  96.         return 1;  
  97.     }  
  98.     return 0;  
  99. }  
  100.   
  101. // 检测当前位置是否为通路   
  102. int IsPass(Maze* m, Position cur) {  
  103.     if (m->_map[cur.y][cur.x] == 1)  
  104.         return 1;  
  105.     return 0;  
  106. }  
  107.   
  108. // 走迷宫   
  109. void PassMazeNor(Maze* m, Position enter, Stack* s) {  
  110.     if (!IsValidEnter(m, enter))  
  111.         return;  
  112.   
  113.     //判断是不是分支节点  
  114.     int isBranch = 0;  
  115.   
  116.     m->_map[enter.y][enter.x] = 2;  
  117.     Position cur = enter;  
  118.     Position next = enter;  
  119.   
  120.     StackPush(s, enter);  
  121.     while (1) {  
  122.   
  123.         //左  
  124.         cur = next;  
  125.         cur.x--;  
  126.         //宏:定义了走迷宫的步骤  
  127.         OPERATE;  
  128.   
  129.         //上  
  130.         cur = next;  
  131.         cur.y--;  
  132.         OPERATE;  
  133.   
  134.         //右  
  135.         cur = next;  
  136.         cur.x++;  
  137.         OPERATE;  
  138.   
  139.         //下  
  140.         cur = next;  
  141.         cur.y++;  
  142.         OPERATE;  
  143.   
  144.         if (StackEmpty(s))  
  145.             return;  
  146.   
  147.         StackPop(s, &next);  
  148.         isBranch = 1;  
  149.     }  
  150.   
  151. }  
  152.   
  153. // 打印迷宫地图数据   
  154. void PrintMap(Maze* m) {  
  155.     for (int i = 0; i < MAX_COL; i++) {  
  156.         for (int j = 0; j < MAX_ROW; j++) {  
  157.             printf("%d ", m->_map[i][j]);  
  158.         }  
  159.         printf("\n");  
  160.     }  
  161. }  
  162.   
  163. // 打印路径   
  164. void PrintPath(Stack* s) {  
  165.     Position position;  
  166.     Stack* path = (Stack*)malloc(sizeof(Stack));  
  167.     StackInit(path);  
  168.     while (!StackEmpty(s)) {  
  169.         StackPop(s, &position);  
  170.         StackPush(path, position);  
  171.     }  
  172.     while (!StackEmpty(path)) {  
  173.         StackPop(path, &position);  
  174.         printf("(%d, %d)-->", position.x, position.y);  
  175.     }  
  176. }  
// 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 "stack.h"  
  2. #include "assert.h"  
  3. //初始化栈    
  4. void StackInit(Stack* stack) {  
  5.     stack->_array = (DataType*)malloc(sizeof(DataType)*Init_StackSize);  
  6.     if (!stack->_array) {  
  7.         exit(0);  
  8.     }  
  9.     stack->_top = 0;  
  10.     stack->_capacity = Init_StackSize;  
  11. }  
  12.   
  13. //压栈    
  14. void StackPush(Stack* stack, DataType data) {  
  15.     assert(stack);  
  16.     //判断栈是否已满    
  17.     if (stack->_top >= stack->_capacity) {  
  18.         //申请10个空间    
  19.         stack->_array = (DataType*)realloc(stack->_array,  
  20.             sizeof(DataType)*(stack->_capacity + 10));  
  21.         //判断是否申请成功    
  22.         if (!stack->_array) {  
  23.             exit(0);  
  24.         }  
  25.         stack->_top = stack->_capacity;  
  26.         stack->_capacity = stack->_capacity + 10;  
  27.     }  
  28.   
  29.     *(stack->_array + stack->_top) = data;  
  30.     stack->_top++;  
  31. }  
  32.   
  33. //出栈    
  34. int StackPop(Stack* stack, DataType* data) {  
  35.     assert(stack);  
  36.     //判断栈是否为空    
  37.     if (stack->_top == 0) {  
  38.         return 0;  
  39.     }  
  40.     //先减一再取值    
  41.     --stack->_top;  
  42.     *data = *(stack->_array + stack->_top);  
  43.   
  44.     return 1;  
  45. }  
  46.   
  47. //判断栈是否为空    
  48. int StackEmpty(Stack* stack) {  
  49.     assert(stack);  
  50.     //为空返回1    
  51.     return stack->_top == 0 ? 1 : 0;  
  52. }  
#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;
}

运行结果截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值