stylistic:tone, quality:corruption
A type of balanced 0–1 matrix

Consider the problem of assigning values, either zero or one, to the positions of an n × n matrix, with n even, so that each row and each column contains exactly n / 2 zeros and n / 2 ones. We ask how many different assignments there are for a given n {\displaystyle n}. For example, when n = 4, five possible solutions are

There are at least three possible approaches: brute force, backtracking, and dynamic programming.

Brute force consists of checking all assignments of zeros and ones and counting those that have balanced rows and columns (n / 2 zeros and n / 2 ones). As there are 2 n 2 {\displaystyle 2^{n^{2}}} possible assignments and (n n / 2) n {\displaystyle {\tbinom {n}{n/2}}^{n}} sensible assignments, this strategy is not practical except maybe up to n = 6 {\displaystyle n=6}.

Backtracking for this problem consists of choosing some order of the matrix elements and recursively placing ones or zeros, while checking that in every row and column the number of elements that have not been assigned plus the number of ones or zeros are both at least n / 2. While more sophisticated than brute force, this approach will visit every solution once, making it impractical for n larger than six, since the number of solutions is already 116,963,796,250 for n = 8, as we shall see.

Dynamic programming makes it possible to count the number of solutions without visiting them all. Imagine backtracking values for the first row – what information would we require about the remaining rows, in order to be able to accurately count the solutions obtained for each first row value? We consider k × n boards, where 1 ≤ k ≤ n, whose k {\displaystyle k} rows contain n / 2 {\displaystyle n/2} zeros and n / 2 {\displaystyle n/2} ones. The function f to which memoization is applied maps vectors of n pairs of integers to the number of admissible boards (solutions). There is one pair for each column, and its two components indicate respectively the number of zeros and ones that have yet to be placed in that column. We seek the value of f ((n / 2, n / 2), (n / 2, n / 2), … (n / 2, n / 2)) {\displaystyle f((n/2,n/2),(n/2,n/2),\ldots (n/2,n/2))} (n {\displaystyle n} arguments or one vector of n {\displaystyle n} elements). The process of subproblem creation involves iterating over every one of (n n / 2) {\displaystyle {\tbinom {n}{n/2}}} possible assignments for the top row of the board, and going through every column, subtracting one from the appropriate element of the pair for that column, depending on whether the assignment for the top row contained a zero or a one at that position. If any one of the results is negative, then the assignment is invalid and does not contribute to the set of solutions (recursion stops). Otherwise, we have an assignment for the top row of the k × n board and recursively compute the number of solutions to the remaining (k − 1) × n board, adding the numbers of solutions for every admissible assignment of the top row and returning the sum, which is being memoized. The base case is the trivial subproblem, which occurs for a 1 × n board. The number of solutions for this board is either zero or one, depending on whether the vector is a permutation of n / 2 (0, 1) {\displaystyle (0,1)} and n / 2 (1, 0) {\displaystyle (1,0)} pairs or not.

For example, in the first two boards shown above the sequences of vectors would be

The number of solutions (sequence A058527 in the OEIS) is

Links to the MAPLE implementation of the dynamic programming approach may be found among the external links.