X-Git-Url: http://git.scottworley.com/slidingtile/blobdiff_plain/cada47bfe401f2084c4248df10cdf0488bc8750a..33bb98f505ab425f693f1e8a6a1bb694a83dff92:/sliding_tile_lib.cc?ds=inline diff --git a/sliding_tile_lib.cc b/sliding_tile_lib.cc index 5037c8e..aee8380 100644 --- a/sliding_tile_lib.cc +++ b/sliding_tile_lib.cc @@ -1,5 +1,6 @@ #include "sliding_tile_lib.h" +#include #include #include #include @@ -84,3 +85,24 @@ signed char Board::hole() const { } 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; +}