Game-XPuzzle(拼图类游戏)(非原创)

出处在一个国外网站,具体地址忘记了。以前学习时记录的。

Game-XPuzzle(拼图类游戏)(非原创)_第1张图片

the 12 gives us 11 inversions (inversions : 1,10,2,7,11,4,5,9,8,6,3)
the 1 gives us none
the 10 gives us 8 inversions (inversions : 2,7,4,5,9,8,6,3)
the 2 gives us none
the 7 gives us 4 inversions
the 11 gives us 6 inversions
the 4 gives us one inversion
the 14 gives us 6
the 5 gives us one
the 9 gives us 3
the 15 gives us 4
the 8 gives us 2
2 from the 13
one from the 6

there are 49 inversions in this example

  • If the grid width is odd, then the number of inversions in a solvable situation is even.
  • If the grid width is even, and the blank is on an even row counting from the bottom (second-last, fourth-last etc), then the number of inversions in a solvable situation is odd.
  • If the grid width is even, and the blank is on an odd row counting from the bottom (last, third-last, fifth-last etc) then the number of inversions in a solvable situation is even.

Formula for determining solvability:

( (grid width odd) && (#inversions even) ) || ( (grid width even) && ((blank on odd row from bottom) == (#inversions even)) )

Why

  1. For a grid of odd width, the polarity of the number of inversions is invariant. That means: all legal moves preserve the polarity of the number of inversions.
  2. For a grid of even width, the following is invariant: (#inversions even) == (blank on odd row from bottom).
  3. If the width is odd, then every solvable state has an even number of inversions.
    If the width is even, then every solvable state has
       an even number of inversions if the blank is on an odd numbered row counting from the bottom;
       an odd number of inversions if the blank is on an even numbered row counting from the bottom;
  4. If a state is such that
     - If the width is odd, then the state has an even number of inversions.
     - If the width is even and the blank is on an odd numbered row counting from the bottom, then the state has an even number of inversions
     - If the width is even and the blank is on an even numbered row counting from the bottom, then the state has an odd number of inversions

你可能感兴趣的:(Game-XPuzzle(拼图类游戏)(非原创))