-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 .. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |