From 28d39a185f60329c9766437a795d90973264fc4c Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 27 Jun 2020 18:29:56 +0800 Subject: [PATCH] optimize HRD_cal --- HRD_cal.cpp | 19 +++++++------------ HRD_cal.h | 3 +-- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/HRD_cal.cpp b/HRD_cal.cpp index 082ec7c..586f8b7 100644 --- a/HRD_cal.cpp +++ b/HRD_cal.cpp @@ -71,7 +71,6 @@ void HRD_cal::init_data() { // 初始化数据结构 List_hash[i].clear(); } List.clear(); - List_step_num.clear(); List_source.clear(); } @@ -80,8 +79,7 @@ void HRD_cal::cal(unsigned long long Code) { // 广搜寻找目标 (输入编 Parse_Code(*start, Code); // 解译输入编码 List.push_back(start); // 加入根节点 List_source.push_back(0); - List_step_num.push_back(0); - List_hash[0xffff & ((*start).code >> 16)].push_back(0); + List_hash[0xffff & ((*start).code >> 16)].push_back(start); now_move = 0; // 设置起始搜索节点编号为0 result = 0; flag = false; // 设置为暂未找到 @@ -97,13 +95,11 @@ void HRD_cal::Add_Case(Case_cal *dat) { // 将找到的布局加入队列中 unsigned int i, x, y; int hash_index = 0xffff & ((*dat).code >> 16); // 取得哈希索引 for (i = 0; i < List_hash[hash_index].size(); i++) { // 遍历索引内容 - if ((*List[List_hash[hash_index][i]]).code == (*dat).code) { // 发现重复 - if (List_step_num[List_hash[hash_index][i]] == List_step_num[now_move] + 1) { // 若找到的目标比多现在一步 - for (x = 0; x < 4; x++) { // 遍历freeze表 - for (y = 0; y < 5; y++) { - if ((*dat).freeze[x][y] == true) { // 将输入表合并到原先的表上 - (*List[List_hash[hash_index][i]]).freeze[x][y] = true; - } + if ((*List_hash[hash_index][i]).code == (*dat).code) { // 发现重复 + for (x = 0; x < 4; x++) { // 遍历freeze表 + for (y = 0; y < 5; y++) { + if ((*dat).freeze[x][y] == true) { // 将输入表合并到原先的表上 + (*List_hash[hash_index][i]).freeze[x][y] = true; } } } @@ -111,10 +107,9 @@ void HRD_cal::Add_Case(Case_cal *dat) { // 将找到的布局加入队列中 return; // 退出 } } - List_hash[hash_index].push_back(List.size()); // 加入哈希索引 List.push_back(dat); // 加入到队列中 List_source.push_back(now_move); // 记录溯源信息 - List_step_num.push_back(List_step_num[now_move] + 1); // 记录最少步数信息 + List_hash[hash_index].push_back(dat); // 加入哈希索引 if (mode == 1) { // 寻解模式 if ((0xF & ((*dat).code >> 32)) == 0xD) { // 2 * 2块在出口位置 flag = true; // 标志已找到目标 diff --git a/HRD_cal.h b/HRD_cal.h index 2fd867d..7913c93 100644 --- a/HRD_cal.h +++ b/HRD_cal.h @@ -21,9 +21,8 @@ class HRD_cal { unsigned long long code; }; vector List; // 主队列 储存每个节点的信息 + vector List_hash[0x10000]; // 哈希表 vector List_source; // 储存上一步编号 用于溯源 - vector List_step_num; // 记录到根节点的最少步数 - vector List_hash[0x10000]; // 哈希表 unsigned int now_move; // 当前正在计算的块的编号 unsigned int result; // 得到的目标编号 unsigned long long target_code;