Learn
Exam

N Rook Problem

Difficulty
INTERMEDIATE

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 n rooks 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 n rooks on an n × n chessboard 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 n rooks on an n × n chessboard without any two rooks attacking each other is equal to n!n!.

Constraints

  • n is 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 × 2 chessboard, there is only one way to place 2 rooks such that the given conditions are satisfied.

Explanation for Test Case 2

  • For a 3 × 3 chessboard, there are 2 ways to place 3 rooks such that the given conditions are satisfied.

Explanation for Test Case 3

  • For a 5 × 5 chessboard, there are a total of 44 ways to place 5 rooks such that the given conditions are satisfied. One such arrangement is shown below: