Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
大幅提高速度
Browse files Browse the repository at this point in the history
  • Loading branch information
xxzl0130 committed Feb 6, 2020
1 parent 336079d commit e4ac056
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
2 changes: 1 addition & 1 deletion ChipDataWindow/ChipDataWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void ChipDataWindow::recvChipJsonObject(const QJsonObject& object)
t.no = i;
t.level = 20;
t.calcValue();
gridChips[t.gridID].push_back(t);
gridChips[t.color][t.gridID].push_back(t);
}
this->tableModel_->setChips(chips);
}
Expand Down
47 changes: 24 additions & 23 deletions ChipSolver/ChipSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ void ChipSolver::run()
chipUsed_.resize(CodeX::instance()->chips.size(), false);
solutions.clear();
t0_ = clock();
tmpChips_.resize(8, 0);
for(auto i = 0;i < plans.configs.size();++i)
{
// 计算当前进度百分比
Expand All @@ -169,17 +170,18 @@ void ChipSolver::run()
emit solvePercentChanged(percent);
}

// 当前配置方案
tmpConfig_ = plans.configs[i];
tmpMaxValue_ = configs_[targetConfigName_].maxValue;
tmpColor_ = configs_[targetConfigName_].color;

if(!satisfyConfig(plans.configs[i]))
{
continue;
}
// 临时记录方案
tmpSolution_.chips.resize(8, ChipPuzzleOption());

// 当前配置方案
tmpConfig_ = plans.configs[i];
tmpMaxValue_ = configs_[targetConfigName_].maxValue;
tmpColor_ = configs_[targetConfigName_].color;
solutionSet_.clear();
findSolution(0);
emit solveNumberChanged(lastSolveNumber_ = solutions.size());
Expand All @@ -201,7 +203,7 @@ bool ChipSolver::satisfyConfig(const Config& config)
}
for(const auto& it : require.keys())
{
ans = ans && (CodeX::instance()->gridChips[it].size() >= require[it]);
ans = ans && (CodeX::instance()->gridChips[tmpColor_][it].size() >= require[it]);
}
return ans;
}
Expand All @@ -222,9 +224,9 @@ void ChipSolver::findSolution(int k)
return;
if (k >= tmpConfig_.size())
{
if (solutionSet_.count(chipUsed_) > 0)
if (solutionSet_.count(tmpChips_) > 0)
return;
solutionSet_.insert(chipUsed_);
solutionSet_.insert(tmpChips_);
auto t = tmpSolution_.chips.size();
tmpSolution_.chips.resize(k);
tmpSolution_.totalValue.no = 0;
Expand Down Expand Up @@ -257,30 +259,29 @@ void ChipSolver::findSolution(int k)
return;
}
//获取当前所需型号的芯片列表
const auto& chips = CodeX::instance()->gridChips[tmpConfig_[k].id];
auto& chips = CodeX::instance()->gridChips[tmpColor_][tmpConfig_[k].id];
// 先保存这一步的芯片配置,后续更新id
tmpSolution_.chips[k] = tmpConfig_[k];
for (const auto& chip : chips)
for (auto& chip : chips)
{
if((chip.color != tmpColor_) // 颜色不同
|| (chip.locked && !useLocked_)// 已锁定且不使用已锁定
if (chip.squad & 0x8000) //已使用
continue;
if ((chip.locked && !useLocked_)// 已锁定且不使用已锁定
|| (chip.squad && !useEquipped_))// 已装备且不使用已装备
{
continue;
}
if (!chipUsed_[chip.no])
// 溢出了不满足要求
if (checkOverflow(tmpTarget_, tmpSolution_.totalValue + chip))
{
// 溢出了不满足要求
if (checkOverflow(tmpTarget_, tmpSolution_.totalValue + chip))
{
continue;
}
chipUsed_[chip.no] = true;
tmpSolution_.totalValue += chip;
tmpSolution_.chips[k].id = chip.no; // 更新id
findSolution(k + 1);
chipUsed_[chip.no] = false;
tmpSolution_.totalValue -= chip;
continue;
}
chip.squad |= 0x8000; //符号位置1,反正小队号都是正数
tmpSolution_.totalValue += chip;
tmpSolution_.chips[k].id = chip.no; // 更新id
tmpChips_[k] = chip.no;
findSolution(k + 1);
chip.squad &= ~0x8000; // 符号位置0
tmpSolution_.totalValue -= chip;
}
}
8 changes: 4 additions & 4 deletions ChipSolver/ChipSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,8 @@ public slots:
Config tmpConfig_;
// 当前重装的最大值
GFChip tmpMaxValue_;
// 记录芯片使用
std::vector<bool> chipUsed_;
// 去重用set
std::set<std::vector<bool>> solutionSet_;
std::set<std::vector<int>> solutionSet_;
// 颜色
int tmpColor_;
// 上一次上报的求解数量
Expand All @@ -122,9 +120,11 @@ public slots:
bool useLocked_;
// 运行标志,用于停止解算
bool running_;
// 当前芯片
std::vector<int> tmpChips_;

//检查当前芯片数量是否满足该拼法最低需要
static bool satisfyConfig(const Config& config);
bool satisfyConfig(const Config& config);
// 检查芯片方案是否满足目标的格数溢出要求,只计算上溢,溢出返回true
bool checkOverflow(const TargetBlock& target, const GFChip& chips) const;
// 递归求解方案
Expand Down
4 changes: 2 additions & 2 deletions CodeX/CodeX.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class CodeX : public QMainWindow

// 芯片列表
Chips chips;
// 按grid编号分类的芯片,保存强制+20的属性
QMap<int, QList<GFChip>> gridChips;
// 外层按颜色分类,内层按grid编号分类的芯片,保存强制+20的属性
std::map<int,std::map<int, std::vector<GFChip>>> gridChips;
ChipSolver* solver_;

protected slots:
Expand Down

0 comments on commit e4ac056

Please sign in to comment.