回溯和递归实现迷宫问题(C语言)

前言

学完图的数据结构,发现可以借助其遍历思想实现迷宫问题,遂用两种方法编写实现。

本文章仅展示头文件、测试文件以及递归函数的定义,鼓励大家开动脑筋,自行补充回溯算法。至于其他函数的实现,可以在文末下载。


题目背景

用nm的矩阵表述迷宫,位置(0,0)表示入口,(n,m)表示出口,n和m分别代表迷宫的行数和列数。迷宫中的每个位置都可以用其行号和列号来指定。在矩阵中,当且仅当一个位置(x,y)处有障碍时,其值为‘ * ’ , 否则其值为‘ ’。


题目要求

寻找一条从入口<0,0>到出口<5,5>的路径
用递归和回溯两个求解方案分别实现


算法思想

递归实现
在这里插入图片描述
回溯实现
在这里插入图片描述


头文件

#ifndef MAZE_PROBLEM_H
#define MAZE_PROBLEM_H
#include <stdio.h>
#include<stdbool.h>

#define MAXSIZE 6
//路口结构体定义
struct InterSection {
	int i;//行号
	int j;//列号
	int right;//可走的方位号
} InterSection[MAXSIZE];
void MyPath(char maze[][MAXSIZE], int moved[][MAXSIZE]);

/*顺序表结构体,用于辅助递归函数*/

typedef struct
{
	int list[50][2];
	int size;
}SeqList;
void ListInitiate(SeqList* L);
bool ListInsert(SeqList* L, int x, int y);
bool ListDelete(SeqList* L);
int ListOutput(SeqList* L);
bool RecursMaze(char maze[][MAXSIZE], int moved[][MAXSIZE], SeqList* L, int i, int j);

#endif // !MAZE_PROLBLEM_H


测试函数

int main() {
	//迷宫数组
	char maze[MAXSIZE][MAXSIZE] = {
		{' ',' ',' ',' ','*',' '},
		{' ','*','*','*',' ',' '},
		{' ',' ','*',' ',' ','*'},
		{'*',' ',' ',' ','*',' '},
		{'*','*',' ','*','*',' '},
		{'*','*',' ',' ',' ',' '},
	};
	//记录是否已访问的数组
	int moved[MAXSIZE][MAXSIZE] = { 0 };
	//MyPath(maze, moved);
	SeqList	MyTest;
	ListInitiate(&MyTest);
	RecursMaze(maze, moved,&MyTest ,0, 0);
	ListOutput(&MyTest);
}

递归函数

#include "MazeProblem.h"
//递归实现
bool RecursMaze(char maze[][MAXSIZE], int moved[][MAXSIZE], SeqList* L,int i, int j) {
	if (i < 0 || j < 0 || i >= MAXSIZE || j >= MAXSIZE||moved[i][j]==1)return false;
	if ((i == MAXSIZE - 1) && (j == MAXSIZE - 1))return true;
	if (maze[i][j] == '*')return false;
	moved[i][j] = 1;
	ListInsert(L, i, j);
	if (RecursMaze(maze, moved,L, i-1, j))return true;
	if (RecursMaze(maze, moved,L, i, j+1))return true;
	if (RecursMaze(maze, moved,L, i+1, j))return true;
	if (RecursMaze(maze, moved,L, i, j-1))return true;
	ListDelete(L);
	return false;
}


下载地址:

链接: https://github.com/clolrful/Maze-problem.git.

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是递归回溯算法生成迷宫的 C 语言实现: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <time.h> #define ROW 15 // 迷宫行数 #define COL 15 // 迷宫列数 int maze[ROW][COL]; // 迷宫数组,0 表示墙,1 表示路 int direction[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // 四个方向 // 判断一个点是否在迷宫内 bool is_inside(int x, int y) { return x >= 0 && x < ROW && y >= 0 && y < COL; } // 递归回溯生成迷宫 void generate_maze(int x, int y) { int i, j, rand_dir, rand_idx; int next_x, next_y; // 随机打乱方向 for (i = 0; i < 4; i++) { rand_idx = rand() % 4; rand_dir = direction[i][0]; direction[i][0] = direction[rand_idx][0]; direction[rand_idx][0] = rand_dir; rand_dir = direction[i][1]; direction[i][1] = direction[rand_idx][1]; direction[rand_idx][1] = rand_dir; } // 尝试四个方向 for (i = 0; i < 4; i++) { next_x = x + direction[i][0] * 2; next_y = y + direction[i][1] * 2; if (is_inside(next_x, next_y) && maze[next_x][next_y] == 0) { maze[next_x][next_y] = 1; // 打通障碍 maze[x + direction[i][0]][y + direction[i][1]] = 1; generate_maze(next_x, next_y); } } } int main() { int i, j; // 初始化迷宫 for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { maze[i][j] = 0; } } // 随机生成起点和终点 srand(time(NULL)); int start_x = rand() % ROW; int start_y = rand() % COL; maze[start_x][start_y] = 1; int end_x = rand() % ROW; int end_y = rand() % COL; maze[end_x][end_y] = 1; // 生成迷宫 generate_maze(start_x, start_y); // 输出迷宫 for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { if (maze[i][j] == 0) { printf("▇"); } else if (i == start_x && j == start_y) { printf("S"); } else if (i == end_x && j == end_y) { printf("E"); } else { printf(" "); } } printf("\n"); } return 0; } ``` 该程序使用了递归回溯算法生成迷宫,并使用了随机打乱方向和随机生成起点和终点的方法增加了随机性。最后输出迷宫,墙用 "▇" 表示,起点用 "S" 表示,终点用 "E" 表示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值