]> git.scottworley.com Git - slidingtile/commitdiff
Switch from priority_queue to multimap
authorScott Worley <scottworley@scottworley.com>
Sun, 10 Jan 2016 06:35:20 +0000 (22:35 -0800)
committerScott Worley <scottworley@scottworley.com>
Sun, 10 Jan 2016 06:35:20 +0000 (22:35 -0800)
sliding_tile_lib.cc

index 9ac0217c4a305247ccb5f3de5664546a48e61dfb..551bc1f018735e373fd03f0211ab7856a2625120 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <cstdlib>
 #include <istream>
 
 #include <cstdlib>
 #include <istream>
+#include <map>
 #include <memory>
 #include <ostream>
 #include <queue>
 #include <memory>
 #include <ostream>
 #include <queue>
@@ -204,34 +205,31 @@ std::shared_ptr<Step> find_path(const std::string& start, const std::string& goa
 
 std::shared_ptr<Step> find_path(const Board& start, const Board& goal) {
   InvertedBoard invgoal = goal.invert();
 
 std::shared_ptr<Step> find_path(const Board& start, const Board& goal) {
   InvertedBoard invgoal = goal.invert();
-  auto heap_greater = [invgoal](const std::shared_ptr<Step>& a, const std::shared_ptr<Step>& b) {
-    return a->cost(invgoal) > b->cost(invgoal);
-  };
-  std::priority_queue<std::shared_ptr<Step>,
-                      std::vector<std::shared_ptr<Step>>,
-                      decltype(heap_greater)> todo(heap_greater);
+  std::multimap<int, std::shared_ptr<Step>> todo;
   std::set<Board> seen;
 
   std::set<Board> seen;
 
-  seen.emplace(start);
-  todo.push(std::make_shared<Step>(start, nullptr));
+  seen.insert(start);
+  auto start_step = std::make_shared<Step>(start, nullptr);
+  todo.emplace(start_step->cost(invgoal), start_step);
   while (!todo.empty()) {
   while (!todo.empty()) {
-    if (todo.top()->board == goal) {
-      return todo.top();
+    auto cur = todo.begin()->second;
+    todo.erase(todo.begin());
+    if (cur->board == goal) {
+      return cur;
     }
     }
-    std::vector<std::shared_ptr<Step>> successors = todo.top()->successors(todo.top());
+    std::vector<std::shared_ptr<Step>> successors = cur->successors(cur);
     for (const std::shared_ptr<Step>& s : successors) {
       if (seen.find(s->board) == seen.end()) {
     for (const std::shared_ptr<Step>& s : successors) {
       if (seen.find(s->board) == seen.end()) {
-        seen.emplace(s->board);
-        todo.push(s);
+        seen.insert(s->board);
+        todo.emplace(s->cost(invgoal), s);
         if (seen.size() % 10000 == 0) {
           std::cerr << "Examined " << seen.size() << " boards.  Tracking "
                     << Step::count << " steps.  " << todo.size()
                     << " waiting.  Considering paths of length "
         if (seen.size() % 10000 == 0) {
           std::cerr << "Examined " << seen.size() << " boards.  Tracking "
                     << Step::count << " steps.  " << todo.size()
                     << " waiting.  Considering paths of length "
-                    << todo.top()->cost(invgoal) << std::endl;
+                    << cur->cost(invgoal) << std::endl;
         }
       }
     }
         }
       }
     }
-    todo.pop();
   }
   throw std::runtime_error("No path from start to goal");
 }
   }
   throw std::runtime_error("No path from start to goal");
 }