Problem
Given the size of a chessboard and the number of rooks n, write a solution function that returns the number of ways to place n rooks on an n × n chessboard such that the following conditions are met:
Conditions:
- The
nrooks cannot attack each other. - No rook can be placed where the row number and column number are the same.
Reference:
- The N-Rook problem involves placing
nrooks on ann × nchessboard such that no two rooks can attack each other. - A rook can attack any other piece along its row or column.
- Generally, the number of ways to place
nrooks on ann × nchessboard without any two rooks attacking each other is equal to .
Constraints
nis a natural number between 2 and 10.
Test Case
| Test Case Number | n | return |
|---|---|---|
| 1 | 2 | 1 |
| 2 | 3 | 2 |
| 3 | 5 | 44 |
Explanation for Test Case 1
- For a
2 × 2chessboard, there is only one way to place 2 rooks such that the given conditions are satisfied.

Explanation for Test Case 2
- For a
3 × 3chessboard, there are 2 ways to place 3 rooks such that the given conditions are satisfied.
Explanation for Test Case 3
- For a
5 × 5chessboard, there are a total of 44 ways to place 5 rooks such that the given conditions are satisfied. One such arrangement is shown below: