迷宫(数据结构栈课设-可运行)

数据机构迷宫源码:用栈来完成一个迷宫。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<time.h>
#include<vector>
#define MAXSIZE 100
#define X 9
#define Y 9
typedef struct {
	int state;		//0是墙 1是未走过的路 2走过的路 3分岔路 4是返回的路
	int x;
	int y;
}SElemType;

typedef struct StackNode {
	SElemType data;
	struct StackNode* next;
}StackNode, * LinkStack;

void InitStack(LinkStack& S);//初始化栈
int StackEmpty(LinkStack S); //判栈空
int PushStack(LinkStack& S, SElemType e);//压栈
int PopStack(LinkStack& S, SElemType& e);//出栈
int Read(LinkStack& S, SElemType e);  //读取栈顶

void Printf_Map(int Map[][Y]);  //画迷宫

int RandMap(int* b_x, int* b_y, int* o_x, int* o_y);//读取迷宫

int Map[X][Y]; //地图二维数组

int main() {
	LinkStack S;
	SElemType e;
	int num = 0;
	int d, u, l, r;
	int x, y;
	int b_x, b_y, o_x, o_y;
	int return_Read = 1;
	InitStack(S);//初始化栈
	d = u = l = r = 0;//初始化四个方向
	Map[X][Y] = { 0 };//初始化地图

	RandMap(&b_x, &b_y, &o_x, &o_y);//随机产生地图后返回初始地址和终点地址
	e.x = b_x;
	e.y = b_y;   //给起始结点赋值
	x = o_x;     
	y = o_y;
	printf("起点%d %d 终点%d %d\n", b_x, b_y, o_x, o_y);


	e.state = 2;
	PushStack(S, e);   //起始结点入栈

	//将起点和终点打印在地图上
	Map[e.x][e.y] = 2;
	Map[o_x][o_y] = 1;
	Printf_Map(Map);
	system("pause");
	system("cls");




	while (1)
	{
		//向下走
		if (Map[e.x + 1][e.y] == 1)
		{
			d = 1;
			num++;
		}
		//向上走
		if (Map[e.x - 1][e.y] == 1)
		{
			u = 1;
			num++;
		}
		//向左走
		if (Map[e.x][e.y - 1] == 1)
		{
			l = 1;
			num++;
		}
		//向右走
		if (Map[e.x][e.y + 1] == 1)
		{
			r = 1;
			num++;
		}
		if (num == 0)
		{
			e.state = 2;//标记为走过的路
			//退回到分叉路口
			while (e.state != 3)
			{
				PopStack(S, e);
				return_Read = Read(S, e);
				Map[e.x][e.y] = 4;//把返回的路标记为4
			}
			PopStack(S, e);
			Read(S, e);
			Map[e.x][e.y] = 4;
			e.state = 3;
		}
		else if (num == 1)
		{
			e.state = 2;
		}
		else if (num > 1)
		{
			e.state = 3;
		}

		num = 0;
		if (r == 1)
		{
			e.y = e.y + 1;
		}
		else if (d == 1)
		{
			e.x = e.x + 1;
		}
		else if (l == 1)
		{
			e.y = e.y - 1;
		}
		else if (u == 1)
		{
			e.x = e.x - 1;
		}


		d = u = l = r = 0;
		PushStack(S, e);

		//退出条件 走到终点时
		if (e.x == o_x && e.y == o_y)
		{
			Map[e.x][e.y] = 2;
			Sleep(1000);
			system("cls");
			Printf_Map(Map);
			printf("\n成功走出\n");
			system("pause");
			printf("走出的倒序路径为");
			while (S != NULL)
			{
				printf("%d %d", S->data.x, S->data.y);
				printf("\n");
				S = S->next;
			}
			break;
		}

		Map[e.x][e.y] = 2;
		Printf_Map(Map);
		Sleep(1000);
		system("cls");//迷宫每走一步就清屏达到动态移动效果
		///
		if (return_Read == 0)
		{
			//退出条件 本来就没有终点
			Map[e.x][e.y] = 2;
			Sleep(1000);
			system("cls");
			Printf_Map(Map);
			printf("\n该迷宫没有通路\n");
			system("pause");

			break;
		}
	}
	
	
	return 0;
}


//S为栈顶指针

void InitStack(LinkStack& S) {	//构造一个空栈,栈顶指针为空
	S = NULL;
}

int StackEmpty(LinkStack S) {    //判断栈空
	if (S == NULL)
		return 1;
	else
		return 0;
} 

int PushStack(LinkStack& S, SElemType e) {
	LinkStack p;
	p = new StackNode;	//生成新的节点P
	p->data = e;	    //将数据的新节点赋值成e
	p->next = S;		//将新节点插入栈顶
	S = p;				//将S向上移动成为新的栈顶指针
	return 0;
}
int PopStack(LinkStack& S, SElemType& e) {
	LinkStack p;
	if (S == NULL)	    //空栈
		return 0;
	e = S->data;	    //将栈顶节点数据的数据赋值到e里面
	p = S;				//栈顶增加一个p指针
	S = S->next;		//指针S向下移动
	delete p;			//释放栈顶空间
	return 1;
}

int Read(LinkStack& S, SElemType e)
{
	if (S == NULL)
	{
		return 0;
	}
	e = S->data;
	return 1;
}

void Printf_Map(int Map[][Y])  //打印地图
{
	int i, j;
	for (i = 0; i < X; i++)
	{
		for (j = 0; j < Y; j++)
		{
			printf("%d ", Map[i][j]);
		}
		printf("\n");
	}
}

int RandMap(int* b_x, int* b_y, int* o_x, int* o_y)
{
	//int Map[X][Y] = { 0 };
	int i, j, k;
	int x, y;
	int location; 
	int temp;
	int begin_x, begin_y;
	int out_x, out_y;
	//初始化地图
	for (i = 1; i <= X - 1; i = i + 2)
	{
		for (j = 1; j <= Y - 1; j = j + 2)
		{
			Map[i][j] = 1;//找到迷宫的九宫格中心
		}
	}


	/*
		666
	*/

	//Printf_Map(Map);

	//生成随机地图
	srand((int)time(NULL));
	for (i = 1; i <= X - 1; i = i + 2)
	{
		for (j = 1; j <= Y - 1; j = j + 2)
		{
			temp = rand() % 2 + 1; //随机生成1~2
			for (k = 0; k < temp; k++)
			{
				while (1)
				{
					x = i;
					y = j;
					location = rand() % 4;
					if (x == 1 && location == 0)
					{
						continue;
					}
					else if (x == X - 2 && location == 1)
					{
						continue;
					}
					else if (y == 1 && location == 2)
					{
						continue;
					}
					else if (y == Y - 2 && location == 3)
					{
						continue;
					}
					else
						break;
				}
				switch (location)
				{
				case 0: x = x - 1; break;
				case 1: x = x + 1; break;
				case 2: y = y - 1; break;
				case 3: y = y + 1; break;
				}
				Map[x][y] = 1;
			}
		}
	}
	//随机产生一个起点
	srand((int)time(NULL));
	while (1)
	{
		begin_x = rand() % (X);
		begin_y = rand() % (Y);
		if (begin_x == 0 && begin_y == 0)
		{
			continue;
		}
		else if (begin_x == 0 && begin_y == Y - 1)
		{
			continue;
		}
		else if (begin_x == X - 1 && begin_y == 0)
		{
			continue;
		}
		else if (begin_x == X - 1 && begin_y == Y - 1)
		{
			continue;
		}
		if (begin_x == 0 || begin_x == X || begin_y == 0 || begin_y == Y)
		{
			break;
		}
	}
	Map[begin_x][begin_y] = 1;

	//随机产生一个终点
	srand((int)time(NULL));
	while (1)
	{
		out_x = rand() % (X);
		out_y = rand() % (Y);
		if (out_x == 0 && out_y == 0)
		{
			continue;
		}
		else if (out_x == 0 && out_y == Y - 1)
		{
			continue;
		}
		else if (out_x == X - 1 && out_y == 0)
		{
			continue;
		}
		else if (out_x == X - 1 && out_y == Y - 1)
		{
			continue;
		}
		if (out_x == begin_x || out_y == begin_y)
		{
			continue;
		}
		if (out_x == 0 || out_x == X || out_y == 0 || out_y == Y)
		{
			break;
		}
	}
	//Map[out_x][out_y] = 1;
	//Printf_Map(Map);

	//指针传参
	*b_x = begin_x;
	*b_y = begin_y;
	*o_x = out_x;
	*o_y = out_y;
	return 0;
}

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用[2]中的问题描述,迷宫问题的数据结构课设可以包括以下内容: 1. 迷宫的表示:使用一个二维矩阵来表示迷宫,其中迷宫的墙壁用1表示,通道用0表示。迷宫的入口可以表示为(1,1),出口可以表示为(8,8)。 2. 路径的表示:可以使用一个来表示路径,每次选择一个可行的方向时,将该方向的坐标入,当无法继续前进时,将顶元素出,回退到上一个位置。 3. 记录已访问的位置:为了避免重复访问,可以使用一个二维数组来记录已经访问过的位置,初始时所有位置都标记为未访问。 4. 搜索算法:可以使用深度优先搜索(DFS)算法来求解从入口到出口的所有可能路径。从入口开始,依次尝试四个方向的移动,如果该方向是通道且未访问过,则继续向该方向前进,直到到达出口或无法继续前进为止。 5. 最短路径算法:可以使用广度优先搜索(BFS)算法来求解从入口到出口的最短路径。从入口开始,将入口加入队列,然后依次从队列中取出位置,尝试四个方向的移动,如果该方向是通道且未访问过,则将该位置加入队列,并更新到达该位置的步数。 下面是一个示例代码,演示了如何使用DFS算法求解迷宫问题的所有可能路径: ```python maze = [ [1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 0, 1, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1], [1, 1, 0, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1] ] def find_paths(maze, start, end, path, paths): if start == end: paths.append(path[:]) return directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] for direction in directions: next_row = start[0] + direction[0] next_col = start[1] + direction[1] if maze[next_row][next_col] == 0: maze[next_row][next_col] = 1 path.append((next_row, next_col)) find_paths(maze, (next_row, next_col), end, path, paths) path.pop() maze[next_row][next_col] = 0 start = (1, 1) end = (8, 8) path = [(1, 1)] paths = [] find_paths(maze, start, end, path, paths) for path in paths: print(path) ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值