我正在尝试基于 BFS 算法编写 rubick 的立方体求解器。如果完成了一次洗牌(移动了一堵墙),它就会找到方法。当我做更复杂的随机播放时,内存有问题。
我写了立方体,所以可以在上面移动,所以移动效果很好。我正在通过将立方体与新立方体进行比较(而不是随机播放)来检查立方体是否已解决。我知道它并不完美,但无论如何它应该可以工作......
有一些代码:
void search(Node &problem)
{
int flag;
Node *curr;
Node* mvs[12]; //moves
std::vector<Cube> explored; //list of position cube's already been in
std::queue<Node*> Q; //queue of possible ways
if (problem.isgoal() == true) return problem.state;
Q.push(&problem);
explored.push_back(problem.state);
while (!Q.empty())
{
flag = 0;
curr = Q.front();
if (curr->isgoal() == true)
{
return curr->state;
}
if (std::find(explored.begin(), explored.end(), curr->state)!=explored.end()) //checking if cube's been in curr position
{
flag = 1;
break;
}
if (flag == 1) break;
explored.push_back(Q.front()->state);
for (int i = 0; i < 12; i++) {
Q.push(curr->expand(i)); //expand is method that
//spread tree of possible moves from curr node
}
Q.pop();
}
}