Merge branch 'ztt1'

master
tong tong zhang 2 years ago
commit 5c4caf75af

@ -1,16 +1,19 @@
# 迷宫生成器
成员1成员2成员3成员4成员5
张桐桐,李玉璇,孟婷玉,周羽凡
**摘要**本项目针对什么问题,实现了哪些功能。为了有效地存储和处理何种数据,采用了何种数据结构。为了解决什么问题,采用了什么算法,算法效率如何。针对其他特定需求做了哪些工作。项目的整体效果如何,有何亮点和创新
**摘要**设计一个程序随机生成指定大小的迷宫。一个迷宫是由 $M \times N$ 个方格组成的矩形区域,每个方格的四周可能存在墙,相邻的两个方格之间如果没有墙的阻隔则可以通行,否则无法通行。要求系统运行正常、功能完整;数据结构使用得当,算法有较高的效率;代码规范、可读性高,结构清晰;具备一定的健壮性、可靠性和可维护性
任务分工及完成情况。
每个成员的工作量(百分比):
| 张桐桐 | 李玉璇 | 周羽凡 |孟婷玉|
| ---- | ---- | ---- |----|
| 30 | 30 | 20 | 20 |
工作量占比。
@ -238,7 +241,7 @@ break;
个人小结:
成员1
张桐桐
成员2

169
src

@ -0,0 +1,169 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct DisjointSet {
int* parent;
int* rank;
int n;
};
/* 初始化不相交集合*/
struct DisjointSet* MakeSet(int n)
{
struct DisjointSet* set = (struct DisjointSet*) malloc(sizeof(struct DisjointSet));
set->parent = (int*) malloc(n * sizeof(int));
set->rank = (int*) malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
set->parent[i] = i;
set->rank[i] = 0;
}
set->n = n;
return set;
}
/* 查找元素所在结合的代表元素*/
int Find(struct DisjointSet* set, int x)
{
if (set->parent[x] != x) {
set->parent[x] = Find(set, set->parent[x]);
}
return set->parent[x];
}
/* 合并两个集合*/
void Union(struct DisjointSet* set, int x, int y)
{
int root1 = Find(set, x), root2 = Find(set, y);
if (root1 != root2) {
if (set->rank[root1] > set->rank[root2]) {
set->parent[root2] = root1;
} else if (set->rank[root1] < set->rank[root2]) {
set->parent[root1] = root2;
} else {
set->parent[root2] = root1;
set->rank[root1] += 1;
}
set->n--;
}
}
/* 判断两个集合是否属于同一组*/
int Connected(struct DisjointSet* set, int x, int y)
{
return Find(set, x) == Find(set, y);
}
/* 定义Maze数据结构存储*/
struct Maze {
int rows, cols;
int* top; //上边界
int* left; //左边界
};
/* 初始化迷宫结构*/
struct Maze* InitMaze(int rows, int cols)
{
struct Maze* maze = (struct Maze*) malloc(sizeof(struct Maze));
maze->rows = rows;
maze->cols = cols;
maze->top = (int*) malloc(rows * cols * sizeof(int));
maze->left = (int*) malloc(rows * cols * sizeof(int));
for (int i = 0; i < rows * cols; i++) {
maze->top[i] = i - cols >= 0 ? i - cols : -1;
maze->left[i] = i % cols > 0 ? i - 1 : -1;
}
return maze;
}
/* 生成迷宫*/
struct Maze* GenerateMaze(int rows, int cols)
{
struct Maze* maze = InitMaze(rows, cols);
struct DisjointSet* set = MakeSet(rows * cols);
srand(time(NULL));
while (set->n > 1) {
int wall = rand() % (rows * cols * 2) + 1;
int row, col;
if (wall <= rows * cols) { //竖直边界
row = (wall - 1) / cols;
col = (wall - 1) % cols;
if (maze->top[wall - 1] != -1) {
if (!Connected(set, wall - 1, maze->top[wall - 1])) {
Union(set, wall - 1, maze->top[wall - 1]);
maze->top[wall - 1] = -1;
}
}
} else { //水平边界
wall -= rows * cols;
row = (wall - 1) % rows;
col = (wall - 1) / rows;
if (maze->left[wall - 1] != -1) {
if (!Connected(set, wall - 1, maze->left[wall - 1])) {
Union(set, wall - 1, maze->left[wall - 1]);
maze->left[wall - 1] = -1;
}
}
}
}
free(set->parent);
free(set);
return maze;
}
/* 打印迷宫*/
void PrintMaze(struct Maze* maze)
{
int row, col; //打印竖直边界
for (row = 0; row < maze->rows; row++) {
for (col = 0; col < maze->cols; col++) {
printf("+");
if (row == 0 && maze->top[col] == -1) {
printf(" ");
} else if (row > 0 && maze->top[row * maze->cols + col] == -1) {
printf(" ");
} else {
printf("---");
}
}
printf("+\n");
/* 打印水平边界*/
for (col = 0; col < maze->cols; col++) {
if (col == 0 && maze->left[row * maze->cols] == -1) {
printf(" ");
} else if (col > 0 && maze->left[row * maze->cols + col] == -1) {
printf(" ");
} else {
printf("|");
}
printf(" ");
}
printf("|\n");
}
/* 打印最后一行的竖直边界*/
for (col = 0; col < maze->cols; col++) {
printf("+");
if (maze->top[(maze->rows - 1) * maze->cols + col] == -1)
printf(" ");
else
printf("---");
}
printf("+\n");
}
int main()
{
int rows , cols;
scanf("%d%d",&rows,&cols);
struct Maze* maze = GenerateMaze(rows, cols);
PrintMaze(maze);
return 0;
}
Loading…
Cancel
Save