Suppose you are only interested in solving the puzzles X * Y, where X, Y <= 16. I would imagine the state of the puzzle as an array of X * Y bytes, in which each byte gives a square value in the puzzle. Using bytes instead of BigInteger in a strange database is likely to give you faster access and modification time, because bit arithmetic tends to be slow and probably not a good general approach in terms of memory / speed exchange.
template<unsigned X, unsigned Y> class PuzzleState { unsigned char state[X*Y]; public: void set(unsigned x, unsigned y, unsigned char v) { state[x+X*y] = v; } unsigned get(unsigned x, unsigned y) { return state[x+X*y]; } bool operator<(const PuzzleState<X, Y>& other) const { return memcmp(state, other.state, X*Y) < 0; } };
And then just use std::set<PuzzleState<8,8 >
with the insert
and find
methods. After testing, if performance still does not fit, you can use the hash table instead of std::set
with a simple hash function like Rolling hash .
source share