Browse Source

update HRD_cal

master
Dnomd343 4 years ago
parent
commit
85ae837c38
  1. 54
      src/engine/HRD_cal.cpp
  2. 15
      src/engine/HRD_cal.h

54
src/engine/HRD_cal.cpp

@ -3,15 +3,19 @@
#include <string>
#include "HRD_cal.h"
vector <unsigned long long> HRD_cal::Calculate_All(unsigned long long Code) { // 寻找整个群的全部元素
HRD_cal::~HRD_cal() {
delete[] List_hash;
}
vector <unsigned long long> HRD_cal::Calculate_All(unsigned long long code) { // 寻找整个群的全部元素
unsigned int i;
vector <unsigned long long> data;
init_data(); // 初始化
mode = 0; // 设置模式为全集计算
if (!Check_Code(Code)) { // 编码错误
if (!Check_Code(code)) { // 编码错误
return data; // 返回空序列
}
cal(Code); // 进行广搜
cal(code); // 进行广搜
for (i = 0; i < List.size(); i++) {
data.push_back(List[i]->code); // 储存计算结果
}
@ -19,19 +23,19 @@ vector <unsigned long long> HRD_cal::Calculate_All(unsigned long long Code) { //
return data;
}
vector <unsigned long long> HRD_cal::Calculate(unsigned long long Code, unsigned long long target) { // 寻找到target的最短路径
vector <unsigned long long> HRD_cal::Calculate(unsigned long long code, unsigned long long target) { // 寻找到target的最短路径
vector <unsigned long long> temp;
if (!(Check_Code(Code) && Check_Code(target))) { // 编码错误
if (!(Check_Code(code) && Check_Code(target))) { // 编码错误
return temp; // 返回空序列
}
if (Code == target) { // 若输入为target
temp.push_back(Code);
if (code == target) { // 若输入为target
temp.push_back(code);
return temp;
}
init_data(); // 初始化
mode = 2; // 设置模式为寻找特定目标
target_code = target;
cal(Code); // 进行广搜
cal(code); // 进行广搜
if (flag) { // 若找到目标
return Get_Path(result);
} else { // 未找到目标
@ -40,18 +44,18 @@ vector <unsigned long long> HRD_cal::Calculate(unsigned long long Code, unsigned
}
}
vector <unsigned long long> HRD_cal::Calculate(unsigned long long Code) { // 寻找最少步解
vector <unsigned long long> HRD_cal::Calculate(unsigned long long code) { // 寻找最少步解
vector <unsigned long long> temp;
if (!Check_Code(Code)) { // 编码错误
if (!Check_Code(code)) { // 编码错误
return temp; // 返回空序列
}
if ((Code >> 32) == 0xD) { // 若输入已经为解
temp.push_back(Code);
if ((code >> 32) == 0xD) { // 若输入已经为解
temp.push_back(code);
return temp;
}
init_data(); // 初始化
mode = 1; // 设置模式为寻解
cal(Code); // 进行广搜
cal(code); // 进行广搜
if (flag) { // 若找到解
return Get_Path(result);
} else { // 无解
@ -86,17 +90,17 @@ void HRD_cal::init_data() { // 初始化数据结构
List_source.clear();
}
bool HRD_cal::Check_Code(unsigned long long Code) { // 检查编码: 错误 -> false / 正确 -> true
bool HRD_cal::Check_Code(unsigned long long code) { // 检查编码: 错误 -> false / 正确 -> true
Case_cal dat;
return Parse_Code(dat, Code);
return Parse_Code(dat, code);
}
void HRD_cal::cal(unsigned long long Code) { // 广搜寻找目标
void HRD_cal::cal(unsigned long long code) { // 广搜寻找目标
Case_cal *start = new Case_cal;
now_move = 0; // 设置起始搜索节点编号为0
result = 0;
flag = false; // 设置为暂未找到
if (!Parse_Code(*start, Code)) { // 若输入编码错误 退出
if (!Parse_Code(*start, code)) { // 若输入编码错误 退出
delete start;
return;
}
@ -371,10 +375,10 @@ void HRD_cal::Get_Code(Case_cal &dat) { // 获取编码并存储在dat.code 输
dat.code &= 0xFFFFFFFFF; // 清除高28位内容
}
bool HRD_cal::Parse_Code(Case_cal &dat, unsigned long long Code) { // 解析编码 返回false表示编码有误
bool HRD_cal::Parse_Code(Case_cal &dat, unsigned long long code) { // 解析编码 返回false表示编码有误
unsigned char range[16]; // dat低32位分16组
int i, x, y, num, space_num = 0;
dat.code = Code;
dat.code = code;
for (x = 0; x < 4; x++) { // 初始化status和freeze
for (y = 0; y < 5; y++) {
dat.status[x][y] = 0xFF;
@ -386,16 +390,16 @@ bool HRD_cal::Parse_Code(Case_cal &dat, unsigned long long Code) { // 解析编
}
num = 0;
for (i = 15; i >= 0; i--) { // 载入排列到range
range[i] = Code & 0x3;
range[i] = code & 0x3;
if (range[i] == 0) {num++;}
Code >>= 2;
code >>= 2;
}
if (num < 2) {return false;} // 0的个数低于两个出错
if (Code > 14) {return false;} // 排除越界情况
if (Code % 4 == 3) {return false;}
if (code > 14) {return false;} // 排除越界情况
if (code % 4 == 3) {return false;}
dat.type[0] = 0; // 载入2 * 2方块
x = Code % 4;
y = Code / 4;
x = code % 4;
y = code / 4;
dat.status[x][y] = dat.status[x + 1][y] = dat.status[x][y + 1] = dat.status[x + 1][y + 1] = 0;
num = x = y = 0;
for (i = 0; i < 16; i++) {

15
src/engine/HRD_cal.h

@ -7,12 +7,13 @@ using namespace std;
class HRD_cal {
public:
~HRD_cal();
unsigned long long Change_int(char str[10]);
string Change_str(unsigned long long dat);
bool Check_Code(unsigned long long Code);
vector <unsigned long long> Calculate(unsigned long long Code);
vector <unsigned long long> Calculate_All(unsigned long long Code);
vector <unsigned long long> Calculate(unsigned long long Code, unsigned long long target);
bool Check_Code(unsigned long long code);
vector <unsigned long long> Calculate(unsigned long long code);
vector <unsigned long long> Calculate_All(unsigned long long code);
vector <unsigned long long> Calculate(unsigned long long code, unsigned long long target);
private:
struct Case_cal {
@ -22,7 +23,7 @@ class HRD_cal {
unsigned long long code;
};
vector <Case_cal *> List; // 主队列 储存每个节点的信息
vector <Case_cal *> List_hash[0x10000]; // 哈希表
vector <Case_cal *> *List_hash = new vector <Case_cal *> [0x10000]; // 哈希表
vector <unsigned int> List_source; // 储存上一步编号 用于溯源
unsigned int now_move; // 当前正在计算的块的编号
unsigned int result; // 得到的目标编号
@ -30,13 +31,13 @@ class HRD_cal {
unsigned char mode; // 0 -> Calculate_All / 1 -> Calculate_Solution / 2 -> Calculate_Target
bool flag; // 判断是否已找到目标
bool Parse_Code(Case_cal &dat, unsigned long long Code);
bool Parse_Code(Case_cal &dat, unsigned long long code);
void Get_Code(Case_cal &dat);
void Find_Sub_Case(Case_cal &dat, int &num, int x, int y, bool addr[4][5]);
void Build_Case(Case_cal &dat, int &num, int x, int y, bool addr[4][5]);
void Find_Next_Case(Case_cal &dat_raw);
void Add_Case(Case_cal *dat);
void cal(unsigned long long Code);
void cal(unsigned long long code);
void init_data();
vector <unsigned long long> Get_Path(unsigned int result_num);
};

Loading…
Cancel
Save