X-Git-Url: http://git.scottworley.com/slidingtile/blobdiff_plain/537a8dc7ce26e6ab5cf3c82ecce388d08566a8b8..ab51c07a99c1927c95a123e6912925adba83714f:/sliding_tile_lib.cc diff --git a/sliding_tile_lib.cc b/sliding_tile_lib.cc index 9f6bfc9..9ac0217 100644 --- a/sliding_tile_lib.cc +++ b/sliding_tile_lib.cc @@ -12,6 +12,7 @@ #include +int Step::count = 0; signed char Step::adjacent[BOARD_SIZE][5] = { 1, 4, -1, -1, -1, 0, 2, 5, -1, -1, @@ -132,13 +133,21 @@ int Board::distance(const Board& o) const { 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); + if (board[i] != 0) { + 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 prev) : board(board), prev(prev) {} +Step::Step(Board board, std::shared_ptr prev) : board(board), prev(prev) { + count++; +} + +Step::~Step() { + count--; +} std::vector> Step::successors(std::shared_ptr shared_this) const { std::vector> suc; @@ -215,7 +224,8 @@ std::shared_ptr find_path(const Board& start, const Board& goal) { seen.emplace(s->board); todo.push(s); if (seen.size() % 10000 == 0) { - std::cerr << "Examined " << seen.size() << " boards. " << todo.size() + std::cerr << "Examined " << seen.size() << " boards. Tracking " + << Step::count << " steps. " << todo.size() << " waiting. Considering paths of length " << todo.top()->cost(invgoal) << std::endl; }