The quickest impossibility proofs: find a quantity that the operation preserves modulo something, and compare the start and the target.
| Invariant | Typical operation |
|---|---|
| parity of the sum | change two elements by each, or add to a pair |
| sum modulo | operations that add a multiple of in total |
| xor of all elements | replace by and , or by |
| gcd of all elements | replace by (Euclid steps) |
| permutation parity | swapping two elements flips it; a 3-cycle keeps it |
| chessboard colour count | dominoes cover one black and one white cell |
| colouring mod | tiles cover each colour class once |
| alternating sum | add 1 to two adjacent elements |
The sliding puzzle (15-puzzle)
An board with tiles and a blank; slide a tile into the blank. The goal is in row-major order with the blank last.
bool slidingSolvable(int R, int C, const vector<int>& g) {
vector<int> t; int blankRow = 0;
for (int i = 0; i < R * C; i++) if (g[i]) t.push_back(g[i]); else blankRow = i / C;
int inv = 0; // O(n^2) is fine; use a BIT for big boards
for (int i = 0; i < sz(t); i++) for (int j = i + 1; j < sz(t); j++) inv += t[i] > t[j];
if (C % 2 == 1) return inv % 2 == 0;
return (inv + blankRow) % 2 == (R - 1) % 2;
}Proof: solvable iff the parity condition holds
Read the tiles in row-major order, skipping the blank. A horizontal move does not change this sequence. A vertical move takes one tile and moves it past exactly others in the sequence, changing the inversion count by steps of , and changes the blank’s row by one. If is odd, is even, so the inversion parity is invariant; the goal has inversions. If is even, every vertical move flips the inversion parity and the blank-row parity together, so is invariant mod 2; the goal has blank row . This proves necessity. Sufficiency (every configuration satisfying the condition is reachable, for ) is the classical theorem: 3-cycles of tiles can be performed by rotating the blank around a block, and 3-cycles generate all even permutations. Verified by BFS for boards , , , , .
Mutilated chessboard and friends
- An board with two opposite corners removed cannot be tiled by dominoes: both removed cells have the same colour, and each domino covers one of each.
- A board cannot be tiled by tiles: colour cell by ; each tile covers each colour once, but the colour counts are .
- Replacing by preserves the parity of the sum, so the parity of the final number is fixed.
Proof: colouring arguments
A tile placed anywhere covers cells whose colours are , which is one cell of each colour class. A tiling therefore uses each colour class equally often, so if the class sizes differ, no tiling exists. For the board, the number of cells with is for .
Related: Invariants and Extremal Arguments, Counting Fundamentals (permutation parity).