]> git.scottworley.com Git - slidingtile/blobdiff - sliding_tile_lib.cc
Keep a count of live Steps
[slidingtile] / sliding_tile_lib.cc
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..51aeded95e8ded018c2bff508c9d62daa3ac0fad 100644 (file)
@@ -0,0 +1,235 @@
+#include "sliding_tile_lib.h"
+
+#include <cstdlib>
+#include <istream>
+#include <memory>
+#include <ostream>
+#include <queue>
+#include <set>
+#include <sstream>
+#include <stdexcept>
+#include <vector>
+
+#include <iostream>
+
+int Step::count = 0;
+signed char Step::adjacent[BOARD_SIZE][5] = {
+  1,   4,   -1,  -1,  -1,
+  0,   2,   5,   -1,  -1,
+  1,   3,   6,   -1,  -1,
+  2,   7,   -1,  -1,  -1,
+  0,   5,   8,   -1,  -1,
+  1,   4,   6,   9,   -1,
+  2,   5,   7,   10,  -1,
+  3,   6,   11,  -1,  -1,
+  4,   9,   12,  -1,  -1,
+  5,   8,   10,  13,  -1,
+  6,   9,   11,  14,  -1,
+  7,   10,  15,  -1,  -1,
+  8,   13,  -1,  -1,  -1,
+  9,   12,  14,  -1,  -1,
+  10,  13,  15,  -1,  -1,
+  11,  14,  -1,  -1,  -1,
+};
+
+bool Board::is_valid() const {
+  bool seen[BOARD_SIZE];
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    seen[i] = false;
+  }
+
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    if (board[i] < 0 || board[i] >= BOARD_SIZE || seen[board[i]]) {
+      return false;
+    }
+    seen[board[i]] = true;
+  }
+
+  // Redundant because pigeon-hole-principle, but check anyway
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    if (!seen[i]) {
+      return false;
+    }
+  }
+
+  return true;
+}
+
+bool Board::operator==(const Board& o) const {
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    if (board[i] != o.board[i]) {
+      return false;
+    }
+  }
+  return true;
+}
+
+bool Board::operator!=(const Board& o) const {
+  return !operator==(o);
+}
+
+bool Board::operator<(const Board& o) const {
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    if (board[i] < o.board[i]) {
+      return true;
+    } else if (board[i] > o.board[i]) {
+      return false;
+    }
+  }
+  return false;
+}
+
+std::istream& operator>>(std::istream& is, Board& board) {
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    if (!is.good()) {
+      is.setstate(std::istream::failbit);
+      break;
+    }
+    if (i > 0 && is.get() != ',') {
+      is.setstate(std::istream::failbit);
+      break;
+    }
+    int numeric;
+    is >> numeric;
+    board.board[i] = numeric;
+  }
+  if (!board.is_valid()) {
+    is.setstate(std::istream::failbit);
+  }
+  return is;
+}
+
+std::ostream& operator<<(std::ostream& os, const Board& board) {
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    if (i > 0) {
+      os << " ";
+    }
+    os << int(board.board[i]);
+  }
+  return os;
+}
+
+signed char Board::hole() const {
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    if (board[i] == 0) {
+      return i;
+    }
+  }
+  throw std::runtime_error("Board with no hole");
+}
+
+InvertedBoard Board::invert() const {
+  InvertedBoard inv;
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    inv.pos[board[i]] = i;
+  }
+  return inv;
+}
+
+int Board::distance(const Board& o) const {
+  return distance(o.invert());
+}
+
+int Board::distance(const InvertedBoard& invo) const {
+  int dist = 0;
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    dist += std::abs(i % BOARD_DIM - invo.pos[board[i]] % BOARD_DIM) +
+            std::abs(i / BOARD_DIM - invo.pos[board[i]] / BOARD_DIM);
+  }
+  return dist;
+}
+
+Step::Step(Board board, std::shared_ptr<Step> prev) : board(board), prev(prev) {
+  count++;
+}
+
+Step::~Step() {
+  count--;
+}
+
+std::vector<std::shared_ptr<Step>> Step::successors(std::shared_ptr<Step> shared_this) const {
+  std::vector<std::shared_ptr<Step>> suc;
+  signed char hole_pos = board.hole();
+  for (int i = 0; adjacent[hole_pos][i] >= 0; i++) {
+    suc.emplace_back(std::make_shared<Step>(board, shared_this));
+    std::swap(suc.back()->board.board[hole_pos], suc.back()->board.board[adjacent[hole_pos][i]]);
+  }
+  return suc;
+}
+
+int Step::length() const {
+  if (prev.get() == nullptr) {
+    return 0;
+  }
+  return 1 + prev->length();
+}
+
+int Step::cost(const InvertedBoard& invgoal) const {
+  return length() + board.distance(invgoal) / 2;
+}
+
+std::ostream& operator<<(std::ostream& os, const Step& step) {
+  if (step.prev != nullptr) {
+    os << *step.prev;
+    signed char this_hole = step.board.hole();
+    signed char prev_hole = step.prev->board.hole();
+    os << int(step.board.board[prev_hole]) << " ";
+    switch (this_hole - prev_hole) {
+      case -1:         os << "right";    break;
+      case  1:         os << "left";     break;
+      case -BOARD_DIM: os << "down";     break;
+      case BOARD_DIM:  os << "up";       break;
+      default:         os << "somehow!"; break;
+    }
+    os << std::endl;
+  }
+  return os;
+}
+
+std::shared_ptr<Step> find_path(const std::string& start, const std::string& goal) {
+  std::istringstream iss_start{start}, iss_goal{goal};
+  Board board_start, board_goal;
+  iss_start >> board_start;
+  iss_goal >> board_goal;
+  if (iss_start.fail() || !iss_start.eof()) {
+    throw std::runtime_error("Could not parse the start board: " + start);
+  }
+  if (iss_goal.fail() || !iss_goal.eof()) {
+    throw std::runtime_error("Could not parse the goal board: " + goal);
+  }
+  return find_path(board_start, board_goal);
+}
+
+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::set<Board> seen;
+
+  seen.emplace(start);
+  todo.push(std::make_shared<Step>(start, nullptr));
+  while (!todo.empty()) {
+    if (todo.top()->board == goal) {
+      return todo.top();
+    }
+    std::vector<std::shared_ptr<Step>> successors = todo.top()->successors(todo.top());
+    for (const std::shared_ptr<Step>& s : successors) {
+      if (seen.find(s->board) == seen.end()) {
+        seen.emplace(s->board);
+        todo.push(s);
+        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;
+        }
+      }
+    }
+    todo.pop();
+  }
+  throw std::runtime_error("No path from start to goal");
+}