Algorithm to identify a unique free polyomino (or polyomino hash)

前端 未结 6 545
心在旅途
心在旅途 2021-02-02 16:13

In short: How to hash a free polyomino?

This could be generalized into: How to efficiently hash an arbitrary collection of 2D integer coordinate

6条回答
  •  梦如初夏
    2021-02-02 16:36

    Well, I came up with a completely different approach. (Also thanks to corsiKa for some helpful insights!) Rather than hashing / encoding the squares, encode the path around them. The path consists of a sequence of 'turns' (including no turn) to perform before drawing each unit segment. I think an algorithm for getting the path from the coordinates of the squares is outside the scope of this question.

    This does something very important: it destroys all location and orientation information, which we don't need. It is also very easy to get the path of the flipped object: you do so by simply reversing the order of the elements. Storage is compact because each element requires only 2 bits.

    It does introduce one additional constraint: the polyomino must not have fully enclosed holes. (Formally, it must be simply connected.) Most discussions of polyominos consider a hole to exist even if it is sealed only by two touching corners, as this prevents tiling with any other non-trivial polyomino. Tracing the edges is not hindered by touching corners (as in the single heptomino with a hole), but it cannot leap from one outer loop to an inner one as in the complete ring-shaped octomino:

    enter image description here

    It also produces one additional challenge: finding the minumum ordering of the encoded path loop. This is because any rotation of the path (in the sense of string rotation) is a valid encoding. To always get the same encoding we have to find the minimal (or maximal) rotation of the path instructions. Thankfully this problem has already been solved: see for example http://en.wikipedia.org/wiki/Lexicographically_minimal_string_rotation.

    Example:

    If we arbitrarily assign the following values to the move operations:

    • No turn: 1
    • Turn right: 2
    • Turn left: 3

    Here is the F pentomino traced clockwise:

    enter image description here

    An arbitrary initial encoding for the F pentomino is (starting at the bottom right corner):

    2,2,3,1,2,2,3,2,2,3,2,1
    

    The resulting minimum rotation of the encoding is

    1,2,2,3,1,2,2,3,2,2,3,2
    

    With 12 elements, this loop can be packed into 24 bits if two bits are used per instruction or only 19 bits if instructions are encoded as powers of three. Even with the 2-bit element encoding can easily fit that in a single unsigned 32 bit integer 0x6B6BAE:

       1- 2- 2- 3- 1- 2- 2- 3- 2- 2- 3- 2
    = 01-10-10-11-01-10-10-11-10-10-11-10
    = 00000000011010110110101110101110
    = 0x006B6BAE
    

    The base-3 encoding with the start of the loop in the most significant powers of 3 is 0x5795F:

        1*3^11 + 2*3^10 + 2*3^9 + 3*3^8 + 1*3^7 + 2*3^6 
      + 2*3^5  + 3*3^4  + 2*3^3 + 2*3^2 + 3*3^1 + 2*3^0
    = 0x0005795F
    

    The maximum number of vertexes in the path around a polyomino of order n is 2n + 2. For 2-bit encoding the number of bits is twice the number of moves, so the maximum bits needed is 4n + 4. For base-3 encoding it's:

    Base 3 Encoded max bits

    Where the "gallows" is the ceiling function. Accordingly any polyomino up to order 9 can be encoded in a single 32 bit integer. Knowing this you can choose your platform-specific data structure accordingly for the fastest hash comparison given the maximum order of the polyominos you'll be hashing.

提交回复
热议问题