From 95c2f4d52be4ff434435772aca134a177054e080 Mon Sep 17 00:00:00 2001 From: golitter_wsl2 <1981971524@qq.com> Date: Wed, 1 May 2024 22:27:36 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E5=BA=B7=E6=89=98=E5=B1=95=E5=BC=80?= =?UTF-8?q?=E5=8F=8A=E5=85=B6=E4=BB=A3=E7=A0=81=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- README.md | 23 +++++++++++++++++++++++ auto_build.sh | 19 +++++++++++++++++++ include/cantor_expansion.h | 22 ++++++++++++++++++++++ main.cpp | 6 +++++- src/cantor_expansion.cpp | 27 +++++++++++++++++++++++++++ 6 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 auto_build.sh create mode 100644 include/cantor_expansion.h create mode 100644 src/cantor_expansion.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f6b7a4f..416565c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ include(CTest) enable_testing() include_directories(include) -add_executable(EightPuzzle-AStar-IDDFS main.cpp src/A_star.cpp) +add_executable(EightPuzzle-AStar-IDDFS main.cpp src/A_star.cpp src/cantor_expansion.cpp) set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) diff --git a/README.md b/README.md index cd5ddca..6b537ad 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,26 @@ # EightPuzzle-AStar-IDDFS + 人工智能大作业:基于迭代加深的A*算法求解八数码问题 + + +基于迭代加深的A*算法(IA\*)本质是对dfs算法剪枝。 + +剪枝: + +- 最优性剪枝:曼哈顿距离剪枝 +- 记忆化剪枝:康托展开优化的哈希映射 + +评估函数:当前深度 + 到达目标状态所需的最小步骤 + +### 康托展开 + + + + + +[【数论系列】 康托展开-CSDN博客](https://blog.csdn.net/qq_40772692/article/details/80549303?ops_request_misc=&request_id=&biz_id=102&utm_term=康托展开&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-80549303.nonecase&spm=1018.2226.3001.4187) + +[C++ 八数码问题理解 `IDA*` 算法原则:及时止损,缘尽即散-CSDN博客](https://blog.csdn.net/y6123236/article/details/136210573) + +[康托展开 - OI Wiki (oi-wiki.org)](https://oi-wiki.org/math/combinatorics/cantor/) \ No newline at end of file diff --git a/auto_build.sh b/auto_build.sh new file mode 100644 index 0000000..e6cd60d --- /dev/null +++ b/auto_build.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +# 任何命令返回非零退出码时立即停止执行,并输出相应的错误信息 +set -e + +mkdir -p build +cd build + +# 生成MakeFile文件 +cmake .. + +# 构建可执行文件 +make -j8 + +# 执行可执行文件 +./EightPuzzle-AStar-IDDFS + +# 退回到原来目录下 +cd .. \ No newline at end of file diff --git a/include/cantor_expansion.h b/include/cantor_expansion.h new file mode 100644 index 0000000..92dedd8 --- /dev/null +++ b/include/cantor_expansion.h @@ -0,0 +1,22 @@ +#include +#include + +namespace Cantor { + +typedef long long LL; + +const int MaxSize = 12; +// ***** 由于八数码问题是小于等于9个元素的,因此这里固定了n的大小 +const int n = 10; // 0~9 共十个数字 + +/** +* @idx: 元素在数组中的下标 +* @count: 在数组后面有多少个比当前元素小的元素个数 +*/ +LL get_fact(int idx, int count); + +/** +* @vec: vec数组 +*/ +LL cantor_value(std::vector vec); +} \ No newline at end of file diff --git a/main.cpp b/main.cpp index f571c3b..6e0a8d5 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,9 @@ #include - +#include "include/cantor_expansion.h" +void test_Cantor() { + std::cout< vec) { + LL sum = 0; //记录总的在s之前数目 + int len = vec.size(); + for(int i = 0; i < len; ++i) { + int cnt = 0; + for(int j = i + 1; j < len; ++j) { + if(vec[j] < vec[i]) ++cnt; + } + sum += get_fact(i, cnt); + } + return sum + 1; +} +} \ No newline at end of file