Programming Contest Question: Counting Polyominos

后端 未结 7 686
醉酒成梦
醉酒成梦 2020-12-15 11:33

Please see my own answer, I think I did it!


Hi,

An example question for a programming contest was to write a program that finds out ho

7条回答
  •  青春惊慌失措
    2020-12-15 11:59

    There are only 4,461 polynominoes of size 10, so we can just enumerate them all.

    Start with a single stone. To expand it by one stone, try add the new stone in at all empty cells that neighbour an existing stone. Do this recursively until reaching the desired size.

    To avoid duplicates, keep a hash table of all polynominoes of each size we've already enumerated. When we put together a new polynomino, we check that its not already in the hash table. We also need to check its 3 rotations (and possibly its mirror image). While duplicate checking at the final size is the only strictly necessary check, checking at each step prunes recursive branches that will yield a new polynomino.

    Here's some pseudo-code:

    polynomino = array of n hashtables
    function find_polynominoes(n, base):
      if base.size == n:
        return
      for stone in base:
        for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
          new_stone.x = stone.x + dx
          new_stone.y = stone.y + dy
          if new_stone not in base:
            new_polynomino = base + new_stone
            is_new = true
            for rotation in [0, 90, 180, 270]:
              if new_polynomino.rotate(rotation) in polynomino[new_polynomino.size]:
                is_new = false
                break
            if is_new:
              polynomino[new_polynomino.size].add(new_polynomino)
    

提交回复
热议问题