Hướng dẫn thử thách
1 / 1
Implement the N-Queens Algorithm
The N-Queens problem asks you to place N queens on an N×N chessboard so that no two queens attack each other (no two share a row, column, or diagonal).
For example, if there is a 4x4 board, one valid arrangement is:
```md
[1, 3, 0, 2]
```
That means that in row 0, the queen is placed in column 1; in row 1, the queen is placed in column 3; in row 2, the queen is placed in column 0; and in row 3, the queen is placed in column 2.
Visually, this arrangement looks like:
```md
. Q . .
. . . Q
Q . . .
. . Q .
```
Where `Q` represents a queen and `.` represents an empty square.
In this lab, you will implement the N-Queens problem solver using the depth-first search approach.
**Objective**: Fulfill the user stories below and get all the tests to pass to complete the lab.
**User Stories:**
1. You should have a function named `dfs_n_queens`.
2. The function should accept exactly one argument: an integer `n`.
3. If `n` is less than `1`, the function should return an empty list (`[]`).
4. The function should return a list of solutions; each solution is itself a list of length `n`, where the element at index `i` is the column index (0-based) of the queen in row `i`.
Vượt qua bài kiểm tra hiện tại để mở khóa bài tiếp theo.
main.py
UTF-8 • Tab Size: 2Kiểm tra bài:⌘↵