How this calculation works
A rook polynomial counts nonattacking placements on a board, and inclusion–exclusion converts those coefficients into the number of full assignments avoiding forbidden cells. The board input uses one for forbidden and zero for allowed, so the polynomial is built on the forbidden positions. A full assignment is a bijection: every row chooses exactly one column and every column is chosen exactly once.
R_B(x)=Σr_k x^k; assignments avoiding B=Σ_(k=0)^n (−1)^k r_k(n−k)!.A worked example
On a three-by-three board with only cells (1,1) and (2,2) forbidden, the rook polynomial is 1+2x+x². The allowed assignment count is 3!−2×2!+1×1!=6−4+1=3. The intersection correction adds back assignments that violate both restrictions because they were subtracted twice. A rook coefficient is not itself the number of allowed full assignments.
Good to know
Boards must be square, order 1–12, and contain only zero or one. Rows and columns are labeled. Nonattacking rooks share neither row nor column; r₀=1 counts the empty placement. The primary result is an exact count, with no assignment costs, optimization objective, rectangular injections or sampling. An all-forbidden board has zero allowed full assignments.
Reference: SageMath — rook polynomials of boards (opens in a new tab) ↗
Build the polynomial on the forbidden board
A forbidden square describes one bad row-column assignment. Selecting k nonconflicting forbidden squares fixes k distinct row-column pairs, leaving (n−k)! ways to complete a full permutation. If two selected squares share a row or column, no full assignment can contain both, so they do not contribute to the rook coefficient r_k.
The polynomial therefore belongs to the forbidden cells even though the final count concerns allowed cells. Reversing the zero/one convention changes the problem. An all-zero board has polynomial one and n! allowed assignments; an all-one board has many rook placements but zero ways to avoid every forbidden square. These opposite endpoint cases make useful input-convention checks.
The Assignment problem calculator from cost matrix selects a minimum-cost bijection; the rook polynomial instead counts all bijections that avoid specified forbidden cells.
Inclusion–exclusion requires alternating contributions
The first correction subtracts one contribution for each forbidden single assignment. A permutation violating two restrictions is then subtracted twice, so the pair-intersection term adds it back once. Higher-order intersections continue with alternating signs. The table shows each rook coefficient, remaining factorial and signed contribution so the final sum can be checked directly.
In the default example the two forbidden cells lie in different rows and columns, so r₂=1. If they shared a row, r₂ would instead be zero. The number of forbidden cells alone is consequently insufficient to determine the result: their incidence pattern matters. Treating the count as n! minus forbiddenCells×(n−1)! without intersections would fail even on tiny boards.
Row masks count unlabeled rook placements once
The dynamic program processes rows and records which columns already contain a rook. Each row may be skipped or may place one rook on an unused forbidden column. Because rows are visited in a fixed order and no rook identity is attached, a geometric placement is counted once rather than once for every ordering of its rooks.
After the final row, grouping states by their number of used columns gives the polynomial coefficients. Exhaustive small-board placement enumeration verifies those coefficients independently, and enumerating all permutations checks the final allowed count. A minimum-cost assignment solver answers a different question: finding one best permitted assignment does not count every permitted bijection.
A few questions, answered
Does one mean a square is allowed?
No. One marks a forbidden square. Zero means allowed. The rook polynomial is computed on forbidden squares.
Why is r₀ equal to one?
There is one empty rook placement. Its inclusion–exclusion contribution is n!, the number of all unconstrained bijections.