Skip to content

Commit

Permalink
add 康托展开及其代码实现
Browse files Browse the repository at this point in the history
  • Loading branch information
golitter committed May 1, 2024
1 parent 8f69a3b commit 95c2f4d
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/)
19 changes: 19 additions & 0 deletions auto_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# 任何命令返回非零退出码时立即停止执行,并输出相应的错误信息
set -e

mkdir -p build
cd build

# 生成MakeFile文件
cmake ..

# 构建可执行文件
make -j8

# 执行可执行文件
./EightPuzzle-AStar-IDDFS

# 退回到原来目录下
cd ..
22 changes: 22 additions & 0 deletions include/cantor_expansion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>
#include <vector>

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<int> vec);
}
6 changes: 5 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <iostream>

#include "include/cantor_expansion.h"
void test_Cantor() {
std::cout<<Cantor::get_fact(3, 4)<<std::endl;
}
int main(int, char**){
std::cout << "Hello, from EightPuzzle-AStar-IDDFS!\n";
test_Cantor();
}
27 changes: 27 additions & 0 deletions src/cantor_expansion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "../include/cantor_expansion.h"

namespace Cantor {

LL fact[MaxSize] = {1,1,2,6,24,120,720,5040,40320,362880}; // 定义0~9的阶乘表

LL get_fact(int idx, int count) {
if(n - idx - 1 < 0) {
std::cerr<<"ERROR!!!"<<std::endl;
exit(0);
}
return fact[n - idx - 1] * count;
}

LL cantor_value(std::vector<int> 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;
}
}

0 comments on commit 95c2f4d

Please sign in to comment.