数据结构:各类迷宫问题详解(c语言版)

#第一类 简单迷宫(不含多条出路,不带环)(0代表墙,1代表通路)
这里写图片描述
###思路分析:
####1.以入口为起点,寻找出口(除了起点以外的边缘上的点)
####2.判定当前点坐标是否可以走。(坐标合法且不为0)
####3.如果合法则将当前点标记成走过的并入栈(维护一个栈可以记录走过的路径,栈的长度就是路径的长度)
####4.判断当前点是否是出口,是出口就return(该迷宫不存在别的出口),如果不是出口,以顺时针的方向(上,右,下,###左)探测临界点是否可以走(不为0且不为已经走过的点),并以递归形式重复步骤2-4.
####5.当一个点的4个方向都已经探测过,就返回上一层栈帧。

maze.h

#pragma once
#include<stdio.h>
#include<windows.h>
#include<assert.h>
//要定义的有:
//一个结构体为结构体pos,用于记录迷宫每个店的横纵坐标
//两个栈path和shortpath,记录通路的最短距离,栈内元素序列即是最短
//迷宫(迷宫地图,入口点)
#define N 6
#define Stack_size 20
typedef struct pos            //迷宫内每个点的坐标
{
	int row;
	int col;
}pos;

typedef pos DataType;

typedef struct Stack         //存放节点信息的栈
{
	DataType* _array;        //数组指针
	size_t  _top;            //栈顶 
	size_t  _end;            //最大容量
}Stack;

typedef struct maze          //迷宫
{
	int mz[N][N];
	pos entry;               //入口点
}maze;

###function.h

#include"maze.h"

void StackInit(Stack* s)//栈的初始化
{
	assert(s);
	s->_array = (DataType*)malloc(sizeof(DataType)*Stack_size);
	s->_end = Stack_size;
	s->_top = 0;

}

void StackPush(Stack* s, DataType x)//入栈
{
	assert(s);
	if (s->_end == s->_top)//栈已满
	{
		s->_end *= 2;
		s->_array = (DataType*)realloc(s->_array, sizeof(DataType)*(s->_end));
		s->_array[s->_top] = x;
		(s->_top)++;
	}
	else
	{
		s->_array[s->_top] = x;
		(s->_top)++;
	}
}


void StackPop(Stack* s)//出栈
{
	assert(s);
	if (s->_top == 0)
	{
		printf("the stack is empty");
	}
	else
	{
		s->_top--;
	}
}

int StackEmpty(Stack* s)//判断栈是否为空
{
	if (s->_top == 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

DataType StackTop(Stack* s)//取栈顶元素
{
	assert(s);


	int num = s->_top;
	DataType  i = s->_array[(--num)];
	return i;

}

void mazeinit(maze *M)//迷宫初始化
{
	assert(M);
	int a[N][N] = {
		{ 0, 0, 0, 0, 0, 0 },
		{ 0, 0, 1, 0, 0, 0 },
		{ 0, 0, 1, 0, 0, 0 },
		{ 0, 0, 1, 1, 1, 0 },
		{ 0, 0, 1, 0, 1, 1 },
		{ 0, 0, 1, 0, 0, 0 }
	};
	for (size_t i = 0;i < N;i++)
	{
		for (size_t j = 0;j < N;j++)
		{
			M->mz[i][j] = a[i][j];
		}
	}
	M->entry.row = 5;
	M->entry.col = 2;
}

void mazeprint(maze *M)
{
	assert(M);
	for (int i = 0;i < N;i++)
	{
		for (int j = 0;j < N;j++)
		{
			printf("%d  ", (M->mz)[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}
int can_stay(pos cur, maze *M)//判断是否能入栈
{
	if (cur.row >= 0 && cur.row < N&&cur.col >= 0 && cur.col < N //坐标合法
		&&M->mz[cur.row][cur.col] != 0 && M->mz[cur.row][cur.col] != 2)   //该点不为墙也不为走过的点
	{
		return 1;
	}
	return 0;
}
void assign_pos(pos cur, maze **M)//标记
{
	(*M)->mz[cur.row][cur.col] = 2;
}
int check_exit(pos cur, maze *M)//检查是否到达出口
{
	if ((cur.row == 0) || (cur.row == N - 1) || (cur.col == 0) || (cur.col == N - 1))
	{
		if ((cur.row) != ((M->entry).row) && (cur.col) != ((M->entry).col))
		{
			return 1;
		}

	}
	return 0;
}

void print_stack(Stack *s)
{
	printf("找到出口!\n\n");
	printf("栈顶\n");
	for (size_t i = 0;i < s->_top;i++)
	{
		printf("[%d %d]\n", s->_array[i].row, s->_array[i].col);
	}
	printf("栈底\n\n");
	printf("路径长度为%u\n", s->_top);
}
void simple_getpath(maze *M, pos cur, Stack *s)
{

	assert(M);
	//2.判定当前点坐标是否可以走。(坐标合法且不为0)

	if (can_stay(cur, M) == 1)
	{
		//3.如果合法则将当前点标记成走过的并入栈
		//(维护一个栈可以记录走过的路径,栈的长度就是路径的长度)
		assign_pos(cur, &M);
		StackPush(s, cur);
	}
	else
	{
		return;
	}
	if (check_exit(cur, M) == 1)
	{
		print_stack(s);
		return;
	}
	//4.判断当前点是否是出口,是出口就return(该迷宫不存在别的出口),
	//如果不是出口,以顺时针的方向(上,右,下,左)探测临界点是否可以走(不为0且不为已经走过的点),
	//并以递归形式重复步骤2-4.

	//up
	pos up = cur;
	up.row -= 1;
	simple_getpath(M, up, s);

	//right
	pos right = cur;
	right.col += 1;
	simple_getpath(M, right, s);

	//down
	pos down = cur;
	down.row += 1;
	simple_getpath(M, down, s);

	//left
	pos left = cur;
	left.col -= 1;
	simple_getpath(M, left, s);


	//5.当一个点的4个方向都已经探测过,就返回上一层栈帧。
	StackPop(s);
	return;



}

###test.c

#include"function.h"

void simplemaze()
{
	maze M;
	Stack path;
	StackInit(&path);
	mazeinit(&M);
	mazeprint(&M);
	simple_getpath(&M, M.entry, &path);
	mazeprint(&M);
}

int main()
{
	simplemaze();
	system("pause");
	return 0;
}

这里写图片描述
#第二类 多通路迷宫(不带环)
这里写图片描述
###思路:
####这个迷宫与简单迷宫非常类似,唯一不同就是有多通路,我们要求解离开迷宫的最短距离,每当移动到出口时,我们的栈path就记录了该条路径,再与shortpath进行比较(初次遇到出口时直接令shortpath和path相等即可),让shortpath始终保留最短的通路,直到再次回到起点(所有栈帧均释放完毕)。

###function.h

#include"maze.h"

void StackInit(Stack* s)//栈的初始化
{
	assert(s);
	s->_array = (DataType*)malloc(sizeof(DataType)*Stack_size);
	s->_end = Stack_size;
	s->_top = 0;

}

void StackPush(Stack* s, DataType x)//入栈
{
	assert(s);
	if (s->_end == s->_top)//栈已满
	{
		s->_end *= 2;
		s->_array = (DataType*)realloc(s->_array, sizeof(DataType)*(s->_end));
		s->_array[s->_top] = x;
		(s->_top)++;
	}
	else
	{
		s->_array[s->_top] = x;
		(s->_top)++;
	}
}


void StackPop(Stack* s)//出栈
{
	assert(s);
	if (s->_top == 0)
	{
		printf("the stack is empty");
	}
	else
	{
		s->_top--;
	}
}

int StackEmpty(Stack* s)//判断栈是否为空
{
	if (s->_top == 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

DataType StackTop(Stack* s)//取栈顶元素
{
	assert(s);


	int num = s->_top;
	DataType  i = s->_array[(--num)];
	return i;

}

//1.创建迷宫(二维数组)并初始化
//2.将迷宫入口的值设为2,入栈,然后依次对上,右,下,左依次进行判断,
//如果不越界&&坐标不为0 || 当前坐标值比下一步的坐标值小,则进行递归,并入栈,递归出口为当前节点不为起点
//3.每当遇到出口,如果shortpath为空或者path比shortpath短,则将path的值依次赋给shortpath。


void mazeinit(maze *M)//迷宫初始化
{
	assert(M);
	int a[N][N] = {
		{ 0, 0, 0, 0, 0, 0 },
		{ 0, 1, 1, 1, 1, 1 },
		{ 0, 1, 0, 0, 0, 0 },
		{ 0, 1, 0, 0, 0, 0 },
		{ 0, 1, 1, 1, 1, 1 },
		{ 0, 1, 0, 0, 0, 0 }
	};
	for (size_t i = 0;i < N;i++)
	{
		for (size_t j = 0;j < N;j++)
		{
			M->mz[i][j] = a[i][j];
		}
	}
	M->entry.row = 5;
	M->entry.col = 1;
}

void mazeprint(maze *M)
{
	assert(M);
	for (int i = 0;i < N;i++)
	{
		for (int j = 0;j < N;j++)
		{
			printf("%d  ", (M->mz)[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}
int can_stay(pos cur, maze *M)//判断是否能入栈
{
	if (cur.row >= 0 && cur.row < N&&cur.col >= 0 && cur.col < N //坐标合法
		&&M->mz[cur.row][cur.col] != 0 && M->mz[cur.row][cur.col] != 2)   //该点不为墙也不为走过的点
	{
		return 1;
	}
	return 0;
}
void assign_pos(pos cur, maze **M)//标记
{
	(*M)->mz[cur.row][cur.col] = 2;
}
int check_exit(pos cur, maze *M)//检查是否到达出口
{
	if ((cur.row == 0) || (cur.row == N - 1) || (cur.col == 0) || (cur.col == N - 1))
	{
		if ((cur.row) != ((M->entry).row) && (cur.col) != ((M->entry).col))
		{
			return 1;
		}

	}
	return 0;
}

void print_stack(Stack *s)
{
	printf("找到出口!\n\n");
	printf("栈顶\n");
	for (size_t i = 0;i < s->_top;i++)
	{
		printf("[%d %d]\n", s->_array[i].row, s->_array[i].col);
	}
	printf("栈底\n\n");
	printf("路径长度为%u\n", s->_top);
}
void get_shortpath(maze *M, pos cur, Stack *path,Stack *shortpath)
{

	assert(M);
	//2.判定当前点坐标是否可以走。(坐标合法且不为0)

	if (can_stay(cur, M) == 1)
	{
		//3.如果合法则将当前点标记成走过的并入栈
		//(维护一个栈可以记录走过的路径,栈的长度就是路径的长度)
		assign_pos(cur, &M);
		StackPush(path, cur);
	}
	else
	{
		return;
	}
	
	if (check_exit(cur, M) == 1)
	{
		if ((shortpath->_top == 0) || (shortpath->_top > path->_top))
		{
			shortpath->_top = path->_top;
			memcpy(shortpath, path, sizeof(path));
			print_stack(path);
			StackPop(path);
			return;
		}
	}
	//4.判断当前点是否是出口,是出口就return(该迷宫不存在别的出口),
	//如果不是出口,以顺时针的方向(上,右,下,左)探测临界点是否可以走(不为0且不为已经走过的点),
	//并以递归形式重复步骤2-4.

	//up
	pos up = cur;
	up.row -= 1;
	get_shortpath(M, up, path,shortpath);

	//right
	pos right = cur;
	right.col += 1;
	get_shortpath(M, right, path, shortpath);

	//down
	pos down = cur;
	down.row += 1;
	get_shortpath(M, down, path, shortpath);

	//left
	pos left = cur;
	left.col -= 1;
	get_shortpath(M, left, path, shortpath);


	//5.当一个点的4个方向都已经探测过,就返回上一层栈帧。
	StackPop(path);
	return;



}

###maze.h

#pragma once
#include<stdio.h>
#include<windows.h>
#include<assert.h>
//要定义的有:
//一个结构体为结构体pos,用于记录迷宫每个店的横纵坐标
//两个栈path和shortpath,记录通路的最短距离,栈内元素序列即是最短
//迷宫(迷宫地图,入口点)
#define N 6
#define Stack_size 20
typedef struct pos            //迷宫内每个点的坐标
{
	int row;
	int col;
}pos;

typedef pos DataType;

typedef struct Stack         //存放节点信息的栈
{
	DataType* _array;        //数组指针
	size_t  _top;            //栈顶 
	size_t  _end;            //最大容量
}Stack;

typedef struct maze          //迷宫
{
	int mz[N][N];
	pos entry;               //入口点
}maze;




###test.c

#include"function.h"

void simplemaze()
{
	maze M;
	Stack path;
	Stack shortpath;
	StackInit(&path);
	StackInit(&shortpath);
	mazeinit(&M);
	mazeprint(&M);
	get_shortpath(&M, M.entry, &path,&shortpath);
	mazeprint(&M);
}

int main()
{
	simplemaze();
	system("pause");
	return 0;
}

这里写图片描述

#第三类 带环迷宫
#####带环迷宫是这三种迷宫中最难的一种,也是最容易弄错的一种,如果是用上面两种把走过的路标记为2的方法做带环问题,有一些通路可能会被忽略。所以这里用的方法是:将入口点标记成2,之后每移动一步就将下一个点的值设为X+1(x是上一个点的值,每个点的值表示从起点算X-1步可以到达
####而判断是否可以入栈的规则也有变化**((坐标合法&&不为0)|| 该点值 - 1 > 上一个点的坐标)**.
####这里肯定有人会为什么规则是这样的,打个比方:
####点A的值为10(从起点通过9步到达),他的相邻点B的值是11(从起点通过10步到达),这时候就不用走,因为已经有另一条路通过相同的步数到达B。
####点A的值是10,相邻点B的值是10,这时候就一定不能走,因为如果走,B的值就得改成11,比原先的步数还要多,这一定不是最短的路程。
####点A的值是10,相邻点B的值是12,这时候就可以走,因为通过A到B可以比原先另一种方式节省一步。
###function.h

#include"maze.h"

void StackInit(Stack* s)//栈的初始化
{
	assert(s);
	s->_array = (DataType*)malloc(sizeof(DataType)*Stack_size);
	s->_end = Stack_size;
	s->_top = 0;

}

void StackPush(Stack* s, DataType x)//入栈
{
	assert(s);
	if (s->_end == s->_top)//栈已满
	{
		s->_end *= 2;
		s->_array = (DataType*)realloc(s->_array, sizeof(DataType)*(s->_end));
		s->_array[s->_top] = x;
		(s->_top)++;
	}
	else
	{
		s->_array[s->_top] = x;
		(s->_top)++;
	}
}


void StackPop(Stack* s)//出栈
{
	assert(s);
	if (s->_top == 0)
	{
		printf("the stack is empty");
	}
	else
	{
		s->_top--;
	}
}

int StackEmpty(Stack* s)//判断栈是否为空
{
	if (s->_top == 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

DataType StackTop(Stack* s)//取栈顶元素
{
	assert(s);


	int num = s->_top;
	DataType  i = s->_array[(--num)];
	return i;

}

//1.创建迷宫(二维数组)并初始化
//2.将迷宫入口的值设为2,入栈,然后依次对上,右,下,左依次进行判断,
//如果不越界&&坐标不为0 || 当前坐标值比下一步的坐标值小,则进行递归,并入栈,递归出口为当前节点不为起点
//3.每当遇到出口,如果shortpath为空或者path比shortpath短,则将path的值依次赋给shortpath。


void mazeinit(maze *M)//迷宫初始化
{
	assert(M);
	int a[N][N] = {
		{ 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,1 },
		{ 0,1,0,0,0,0 },
	};
	for (size_t i = 0;i < N;i++)
	{
		for (size_t j = 0;j < N;j++)
		{
			M->mz[i][j] = a[i][j];
		}
	}
	M->entry.row = 5;
	M->entry.col = 1;
}

void mazeprint(maze *M)
{
	assert(M);
	for (int i = 0;i < N;i++)
	{
		for (int j = 0;j < N;j++)
		{
			printf("%d  ", (M->mz)[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}
int can_stay(pos cur, maze *M,int value)//判断是否能入栈
{
	
	if (cur.row >= 0 && cur.row < N&&cur.col >= 0 && cur.col < N) //坐标合法
	{
		if (M->mz[cur.row][cur.col] == 1 || M->mz[cur.row][cur.col] - 1>value )
		{
			return 1;
		}														//该点不为墙且大于前一个点的值+1
	}					  
	return 0;
}
void assign_pos(pos cur, maze **M,int value)//标记
{
	(*M)->mz[cur.row][cur.col] = value;
}
int check_exit(pos cur, maze *M)//检查是否到达出口
{
	if ((cur.row == 0) || (cur.row == N - 1) || (cur.col == 0) || (cur.col == N - 1))
	{
		if ((cur.row) != ((M->entry).row) && (cur.col) != ((M->entry).col))
		{
			return 1;
		}

	}
	return 0;
}

void print_stack(Stack *s)
{
	printf("找到出口!\n\n");
	printf("栈顶\n");
	for (size_t i = 0;i < s->_top;i++)
	{
		printf("[%d %d]\n", s->_array[i].row, s->_array[i].col);
	}
	printf("栈底\n\n");
	printf("路径长度为%u\n", s->_top);
}
void get_shortpath(maze *M, pos cur, Stack *path,Stack *shortpath,int value)
{

	assert(M);
	//2.判定当前点坐标是否可以走。(坐标合法且不为0)
	
	if (can_stay(cur, M,value) == 1)
	{
		//3.如果合法则将当前点标记成走过的并入栈
		//(维护一个栈可以记录走过的路径,栈的长度就是路径的长度)
		assign_pos(cur, &M,value);
		value += 1;
		StackPush(path, cur);
	}
	else
	{
		return;
	}
	
	if (check_exit(cur, M) == 1)
	{
		if ((shortpath->_top == 0) || (shortpath->_top > path->_top))
		{
			shortpath->_top = path->_top;
			memcpy(shortpath, path, sizeof(path));
			print_stack(path);
			StackPop(path);
			return;
		}
	}
	//4.判断当前点是否是出口,是出口就return(该迷宫不存在别的出口),
	//如果不是出口,以顺时针的方向(上,右,下,左)探测临界点是否可以走(不为0且不为已经走过的点),
	//并以递归形式重复步骤2-4.

	//up
	pos up = cur;
	up.row -= 1;
	get_shortpath(M, up, path,shortpath,value);

	//right
	pos right = cur;
	right.col += 1;
	get_shortpath(M, right, path, shortpath,value);

	//down
	pos down = cur;
	down.row += 1;
	get_shortpath(M, down, path, shortpath,value);

	//left
	pos left = cur;
	left.col -= 1;
	get_shortpath(M, left, path, shortpath,value);


	//5.当一个点的4个方向都已经探测过,就返回上一层栈帧。
	StackPop(path);
	return;



}

###maze.h

#pragma once
#include<stdio.h>
#include<windows.h>
#include<assert.h>
//要定义的有:
//一个结构体为结构体pos,用于记录迷宫每个店的横纵坐标
//两个栈path和shortpath,记录通路的最短距离,栈内元素序列即是最短
//迷宫(迷宫地图,入口点)
#define N 6
#define Stack_size 20
typedef struct pos            //迷宫内每个点的坐标
{
	int row;
	int col;
}pos;

typedef pos DataType;

typedef struct Stack         //存放节点信息的栈
{
	DataType* _array;        //数组指针
	size_t  _top;            //栈顶 
	size_t  _end;            //最大容量
}Stack;

typedef struct maze          //迷宫
{
	int mz[N][N];
	pos entry;               //入口点
}maze;




###test.c

#include"function.h"

void simplemaze()
{
	maze M;
	Stack path;
	Stack shortpath;
	StackInit(&path);
	StackInit(&shortpath);
	mazeinit(&M);
	mazeprint(&M);
	get_shortpath(&M, M.entry, &path,&shortpath,2);
	mazeprint(&M);
}

int main()
{
	simplemaze();
	system("pause");
	return 0;
}

这里写图片描述

  • 33
    点赞
  • 213
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值