数据结构实验之迷宫

该程序使用广度优先遍历、easyX界面化显示。

#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OVERFLOW -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define QUEUE_INIT_SIZE 100
#define QUEUEINCREMENT 10
#define MAXQSIZE 10001
#define CELL_SIZE 50
struct ElemType
{
	int i;
	int j;
	int pre;
};
typedef struct ElemType QElemType;
typedef struct ElemType SElemType;
typedef int Status;

#include "stack.h"
#include "queue.h"
#include 
#include 
#include 
#include 
#include 

void InitMaze(int** MAZE, int** MARK, int m, int n,float ratio);
void InitPos(QElemType* e,int i,int j,int pre);
void MazeExplore(SqQueue* q,int** MAZE, int** MARK, int m, int n,int* flag, int Direction[8][2]);
void RecordPath(SqQueue* q, SqStack* s);
void DrawPath(int m, int n,SqQueue q,int** MAZE);
void fill(int i, int j, int k, int l);

int main()
{
	int m, n;
	printf("请输入迷宫的行和列>:");
	scanf("%d %d",&m,&n);
	
	int** MAZE = (int**)malloc(sizeof(int*) * (m + 2));
	int** MARK = (int**)malloc(sizeof(int*) * (m + 2));
	int Direction[8][2] = { {0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1} };

	
	/*printf("请输入比率>:");
	scanf("%d",&rate);*/
	InitMaze(MAZE, MARK,m,n,0.3);
	SqQueue q;
	InitSqQueue(&q);
	int flag = 0;  //标识有没有路
	MazeExplore(&q, MAZE, MARK, m, n,&flag,Direction);

	if (flag)
	{
		DrawPath(m, n, q, MAZE);
	}
	else
	{
		printf("无法通过\n");
	}

	DestoryQueue(&q);
	return 0;
}

//初始化迷宫
void InitMaze(int** MAZE,int** MARK,int m,int n,float ratio)
{
	srand(time(NULL));
	for (int i = 0; i < m + 2; i++)
	{
		MAZE[i] = (int*)malloc(sizeof(int) * (n + 2));
		MARK[i] = (int*)calloc((n + 2), sizeof(int));
	}
	//围墙
	for (int j = 0; j < n + 2; j++)
	{
		MAZE[0][j] = 1;
		MAZE[m+1][j] = 1;
	}
	for (int i = 0; i < m + 2; i++)
	{
		MAZE[i][0] = 1;
		MAZE[i][n + 1] = 1;
	}
	//随机生成迷宫
	for (int i = 1; i <= m; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			float r = (float)rand() / RAND_MAX;  // 生成0到1之间的浮点数
			if (r < ratio)
			{
				MAZE[i][j] = 1;
			}
			else 
			{
				MAZE[i][j] = 0;
			}
		}
	}
	MAZE[1][1] = 0;
	MAZE[m][n] = 0;

	return;
}

//初始化坐标
void InitPos(QElemType* e,int i,int j,int pre)
{
	e->i = i;
	e->j = j;
	e->pre = pre;
}

//探索迷宫
void MazeExplore(SqQueue* q, int** MAZE, int** MARK, int m, int n, int* flag, int Direction[8][2])
{
	QElemType e;
	InitPos(&e, 1, 1, -1);
	EnQueue(q, e);
	MARK[1][1] = 1;
	while (!QueueEmpty(*q))
	{
		GetHead(*q, &e);
		for (int num = 0; num < 8; num++)
		{
			QElemType e1;
			InitPos(&e1, e.i + Direction[num][0], e.j + Direction[num][1], (*q).front);
			//是0、没有被走过
			if (MARK[e1.i][e1.j] == 0 && MAZE[e1.i][e1.j] == 0)
			{
				EnQueue(q, e1);
				MARK[e1.i][e1.j] = 1;
				if (e1.i == m && e1.j == n)
				{
					*flag = 1;
					return;
				}
			}
		}
		DeQueue(q);
	}

	return;
}
//记录迷宫路径
void RecordPath(SqQueue* q, SqStack* s)
{
	SqStack stack;
	stack.base = q->base;
	stack.top = q->base + q->rear;
	stack.stacksize = q->rear;
	int index = stack.stacksize - 1;
	ElemType e;
	ElemType e1;
	Pop(&stack, &e);
	Push(s, e);
	index--;
	while (!StackEmpty(stack))
	{
		GetTop(*s, &e1);
		Pop(&stack, &e);
		if (index == e1.pre)
		{
			Push(s, e);
		}
		index--;
	}
	return;
}

void fill(int i, int j, int k, int l)
{
	bar(i, j, k,l);
	rectangle(i, j, k, l);

	return;
}

//画出迷宫和路径
void DrawPath(int m, int n, SqQueue q,int** MAZE)
{
	SqStack s;
	InitStack(&s);
	RecordPath(&q, &s);

	IMAGE mouse;
	IMAGE wall;
	loadimage(&mouse, "asset/mouse.jpg", CELL_SIZE, CELL_SIZE);
	loadimage(&wall, "asset/wall.jpg", CELL_SIZE, CELL_SIZE);
	initgraph((m + 2) * CELL_SIZE, (n + 2) * CELL_SIZE);
	setbkcolor(WHITE);
	cleardevice();
	

	//围墙
	for (int j = 0; j < n + 2; j++)
	{
		putimage(j * CELL_SIZE ,0,&wall);
		putimage(j * CELL_SIZE, (m + 1) * CELL_SIZE, &wall);
	
	}
	for (int i = 0; i < m + 2; i++)
	{
		putimage(0, i * CELL_SIZE,  &wall);
		putimage((n + 1) * CELL_SIZE, i * CELL_SIZE, &wall);
		
	}
	//死路
	for (int i = 1; i <= m; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			if (MAZE[i][j] == 1)
			{
				putimage(j * CELL_SIZE, i * CELL_SIZE, &wall);
			}
			else
			{
				setfillcolor(LIGHTGRAY);
				fill(j * CELL_SIZE, i * CELL_SIZE, (j + 1) * CELL_SIZE, (i + 1) * CELL_SIZE);
			}
		}
	}
	putimage(50, 50, &mouse);
	//运动路径

	while (!StackEmpty(s))
	{
		ElemType e;
		Pop(&s, &e);
		putimage(e.j * CELL_SIZE, e.i * CELL_SIZE, &mouse);
		Sleep(500);
	}
	_getch();
	closegraph();

	DestroyStack(&s);
}



运行截图
数据结构实验之迷宫_第1张图片

你可能感兴趣的:(数据结构,c语言,算法)