D - Go Stone Puzzle Editorial by en_translator
If \(S\) and \(T\) have different number of W and B, then the answer is No.  Now we assume that they are equal.  Let \(W\) be the number of W and \(B\) of B.
Since the empty squares are always adjacent to each other, there are \(\frac{(N+1)!}{B!W!}=O(\sqrt{N}2^N)\) possible states of the cells.  (The analysis is a bit complicated, so we omit it here.)
This takes on the maximum value \(51480\) for \(N=14\) and \(B=W=7\).  For each state, there are \((N-1)\) possible moves.
Since the number of states and the next moves are both small enough, the problem can be solved with BFS.
The total complexity is (expected) \(O(N^{1.5}2^N)\) (using a hash map).
Similar problem: 8 puzzle (AOJ)
				posted:
				
				
				last update: