From 64fa5ac4baa13b09a86775e6c51e9870881567c5 Mon Sep 17 00:00:00 2001 From: tong tong zhang Date: Tue, 13 Jun 2023 21:15:29 +0800 Subject: [PATCH 1/4] 11 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d9006bc..5da1d24 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 迷宫生成器 -成员1,成员2,成员3,成员4,成员5 +张桐桐,李玉璇,孟婷玉,周羽凡 **摘要**:本项目针对什么问题,实现了哪些功能。为了有效地存储和处理何种数据,采用了何种数据结构。为了解决什么问题,采用了什么算法,算法效率如何。针对其他特定需求做了哪些工作。项目的整体效果如何,有何亮点和创新。 From df6c3aa2a9ecc5289684e819b3d4af706e4dbc62 Mon Sep 17 00:00:00 2001 From: tong tong zhang Date: Tue, 13 Jun 2023 21:15:40 +0800 Subject: [PATCH 2/4] 111 --- src | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 src diff --git a/src b/src new file mode 100644 index 0000000..9354ff7 --- /dev/null +++ b/src @@ -0,0 +1,169 @@ +#include +#include +#include + +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; +} \ No newline at end of file From a4052e582ca35a82b4063f74aa93c6631efba623 Mon Sep 17 00:00:00 2001 From: tong tong zhang Date: Sun, 18 Jun 2023 18:57:49 +0800 Subject: [PATCH 3/4] 123 --- README.md | 7 +++++-- src | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5da1d24..165a97d 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,18 @@ 张桐桐,李玉璇,孟婷玉,周羽凡 -**摘要**:本项目针对什么问题,实现了哪些功能。为了有效地存储和处理何种数据,采用了何种数据结构。为了解决什么问题,采用了什么算法,算法效率如何。针对其他特定需求做了哪些工作。项目的整体效果如何,有何亮点和创新。 +**摘要**:设计一个程序随机生成指定大小的迷宫。一个迷宫是由 $M \times N$ 个方格组成的矩形区域,每个方格的四周可能存在墙,相邻的两个方格之间如果没有墙的阻隔则可以通行,否则无法通行。要求系统运行正常、功能完整;数据结构使用得当,算法有较高的效率;代码规范、可读性高,结构清晰;具备一定的健壮性、可靠性和可维护性。 任务分工及完成情况。 +每个成员的工作量(百分比): +| 张桐桐 | 李玉璇 | 周羽凡 |孟婷玉| +| ---- | ---- | ---- |----| +| 30 | 30 | 20 | 20 | -工作量占比。 diff --git a/src b/src index 9354ff7..b806ac2 100644 --- a/src +++ b/src @@ -161,7 +161,7 @@ void PrintMaze(struct Maze* maze) int main() { - int rows , cols; + int rows , cols; scanf("%d%d",&rows,&cols); struct Maze* maze = GenerateMaze(rows, cols); PrintMaze(maze); From 7964a62b531c7903aea3cedef060f4f4eeeea395 Mon Sep 17 00:00:00 2001 From: tong tong zhang Date: Sat, 24 Jun 2023 19:09:40 +0800 Subject: [PATCH 4/4] 1111 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 165a97d..0ee3f44 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,7 @@ void bubble_sort(T a[], int n) 个人小结: -成员1: +张桐桐: 成员2: