From 21fa72ea9f4b660c59e7be99f5066e3da741ab34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=BD=9C=E5=8B=8B?= <59754483+Zuoxun@users.noreply.github.com> Date: Sat, 7 Oct 2023 21:30:12 +0800 Subject: [PATCH] Add hanota in C code (#819) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update vector.h 增加功能列表: 获取向量的第 i 个元素 设置向量的第 i 个元素 向量扩容 向量缩容 向量插入元素 向量删除元素 向量交换元素 向量是否为空 向量是否已满 向量是否相等 对向量内部进行排序(升序/降序) 对向量某段数据排序(升序/降序) * Create hanota.c * 新增binary_search_recur.c * Update vector.h * Delete codes/c/chapter_divide_and_conquer directory * Create hanota.c * Update vector.h * Update hanota.c * Create CMakeLists.txt * Update vector.h * Update hanota.c * RollBack vector.h * Update CMakeLists.txt * Update hanota.c * Update hanota.c --- .../chapter_divide_and_conquer/CMakeLists.txt | 1 + codes/c/chapter_divide_and_conquer/hanota.c | 82 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 codes/c/chapter_divide_and_conquer/CMakeLists.txt create mode 100644 codes/c/chapter_divide_and_conquer/hanota.c diff --git a/codes/c/chapter_divide_and_conquer/CMakeLists.txt b/codes/c/chapter_divide_and_conquer/CMakeLists.txt new file mode 100644 index 000000000..04f0bf4f1 --- /dev/null +++ b/codes/c/chapter_divide_and_conquer/CMakeLists.txt @@ -0,0 +1 @@ +add_executable(hanota hanota.c) diff --git a/codes/c/chapter_divide_and_conquer/hanota.c b/codes/c/chapter_divide_and_conquer/hanota.c new file mode 100644 index 000000000..39ab89e22 --- /dev/null +++ b/codes/c/chapter_divide_and_conquer/hanota.c @@ -0,0 +1,82 @@ +/** + * File: hanota.c + * Created Time: 2023-10-01 + * Author: Zuoxun (845242523@qq.com) + */ + +#include "../utils/common.h" + +/* 移动一个圆盘 */ +void move(vector *src, vector *tar) { + // 从 src 顶部拿出一个圆盘 + int *panTemp = vectorBack(src); + int *pan = malloc(sizeof(int)); + *pan = *panTemp; + vectorPopback(src); + // 将圆盘放入 tar 顶部 + vectorPushback(tar, pan, sizeof(int)); +} + +/* 求解汉诺塔:问题 f(i) */ +void dfs(int i, vector *src, vector *buf, vector *tar) { + // 若 src 只剩下一个圆盘,则直接将其移到 tar + if (i == 1) { + move(src, tar); + return; + } + // 子问题 f(i-1) :将 src 顶部 i-1 个圆盘借助 tar 移到 buf + dfs(i - 1, src, tar, buf); + // 子问题 f(1) :将 src 剩余一个圆盘移到 tar + move(src, tar); + // 子问题 f(i-1) :将 buf 顶部 i-1 个圆盘借助 src 移到 tar + dfs(i - 1, buf, src, tar); +} + +/* 求解汉诺塔 */ +void solveHanota(vector *A, vector *B, vector *C) { + int n = A->size; + // 将 A 顶部 n 个圆盘借助 B 移到 C + dfs(n, A, B, C); +} + +/* 打印向量中的元素 */ +void printFunc(vector *v, void *p) { + int *node = p; + printf("%d", *node); +} + +/* Driver Code */ +int main() { + // 列表尾部是柱子顶部 + int a[] = {5, 4, 3, 2, 1}; + vector *A = newVector(); // int + vector *B = newVector(); // int + vector *C = newVector(); // int + for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) { + vectorPushback(A, &a[i], sizeof(int)); + } + + printf("初始状态下:\n"); + printf("A ="); + printVector(A, printFunc); + printf("B ="); + printVector(B, printFunc); + printf("C ="); + printVector(C, printFunc); + + solveHanota(A, B, C); + + printf("圆盘移动完成后:\n"); + printf("A ="); + printVector(A, printFunc); + printf("B ="); + printVector(B, printFunc); + printf("C ="); + printVector(C, printFunc); + + // 释放内存 + delVector(A); + delVector(B); + delVector(C); + return 0; +}