Skip to content

Instantly share code, notes, and snippets.

@Mooophy
Last active April 23, 2016 01:19
Show Gist options
  • Save Mooophy/60ebbc794486d8d0944e49332e2eb2d6 to your computer and use it in GitHub Desktop.
Save Mooophy/60ebbc794486d8d0944e49332e2eb2d6 to your computer and use it in GitHub Desktop.
auto search(std::string const& source, std::string const& goal) -> void
{
// add is_max_depth_reached to stop the for loop
bool is_max_depth_reached = false;
for (max_depth_ = 0; is_max_depth_reached; ++max_depth_)
{
is_max_depth_reached = false;
visited_.clear();
q_.clear();
for (q_.push_front(Node(source, "")); !q_.empty(); max_q_length_ = std::max(max_q_length_, q_.size()))
{
auto curr = q_.front(); q_.pop_front();
visited_.insert(curr.state);
if (goal == curr.state){ final_path_ = curr.path; return; }
if (curr.path.size() >= max_depth_)
{
is_max_depth_reached = true; //condition for next max_depth_
continue
}
for (auto make_child : func_dic_.at(curr.state.find('0')))
{
auto child = make_child(curr);
if (visited_.end() == visited_.find(child.state)) q_.push_front(child);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment