From df6c3aa2a9ecc5289684e819b3d4af706e4dbc62 Mon Sep 17 00:00:00 2001 From: tong tong zhang Date: Tue, 13 Jun 2023 21:15:40 +0800 Subject: [PATCH] 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