I am motivated by the post, Complexity of n-queens-completion. I am interested in completion problem of non-attacking rooks on a chessboard.
Input: Given a chessboard of size × with − rooks placed in non-attacking positions, and certain chessboard diagonals represented by multi-set of integers = { 1, 2, ..., }
Output: Can we place additional rooks such all rooks are non-attacking and each new rook is placed on exactly one diagonal in ?
I suspect that the problem is NP-complete. Is there an efficient (fast) algorithm?
P.S. From comments, the multi-set of diagonals D defines parallel diagonals. For diagonal j, the sum of x and y coordinates of any cell on that diagonal is equal to j. This implies that there are no crossing diagonals. So, Only parallel diagonals (only diagonals, no anti-diagonals).
This is a constraint satisfaction problem, and more particularly a SAT problem, where each boolean variable corresponds to a square on the chess board, indicating whether it gets a rook or not.
The constraints then express that every non-occupied row must have exactly one rook, similarly every non-occupied column must have exactly one rook. The given diagonals must have the number of rooks specified for them. The constraints only need to reference squares that lie on at least one given diagonal and are not on a row/col that already has a rook. The other squares can be ignored, as it is clear they wont get a rook.
There are several packages for python that solve such kinds of problems, like scipy, numpy, and others. Here I will use the python-constraint package.
Here is an example run:
The input represents this chess board and constraints:
There is one pre-placed rook (indicated with R). The diagonals in are colored, and their frequency indicated with a number. For example, the yellow one needs to get 3 rooks and is identified by the number 7 since its squares have coordinates that add up to 7.
A solution to this test case is this:
... and the output of the above example run is: