The N Queens problem is a classic algorithmic challenge of placing N non-attacking queens on an N×N chessboard. This project involves using backtracking to explore and print all possible solutions, helping you strengthen your understanding of recursion and algorithmic optimization.
- ♻️ Backtracking: Use backtracking to explore solution paths and revert when necessary.
- 🔄 Recursion: Leverage recursion to handle repeated function calls and explore solution spaces.
- 📋 List Manipulations: Store and track the position of queens on the chessboard.
- 🖥️ Command Line Arguments: Manage input arguments using Python’s
sysmodule.
- Files must run on Ubuntu 20.04 LTS using
python3(version 3.4.3). - Files should start with
#!/usr/bin/python3. - Code should conform to PEP 8 standards.
- Ensure all files are executable and contain complete documentation.
-
Clone the repository:
git clone https://github.com/Alogyn/alx-interview cd alx-interview/0x05-nqueens -
Run the program:
./0-nqueens.py N
Replace
Nwith the desired board size (an integer ≥ 4).
-
Goal: Write a program to solve the N queens puzzle, ensuring each queen is placed such that no two queens attack each other.
-
Program Usage:
nqueens N
-
Error Handling:
- If the number of arguments is incorrect, print:
Usage: nqueens N. - If
Nis not an integer, print:N must be a number. - If
N< 4, print:N must be at least 4.
- If the number of arguments is incorrect, print:
-
Output: Each solution is printed in a list format, with each list element representing the row and column position of a queen.
$ ./0-nqueens.py 4
[[0, 1], [1, 3], [2, 0], [3, 2]]
[[0, 2], [1, 0], [2, 3], [3, 1]]- Backtracking Algorithms: Solve problems by exploring all solutions and discarding those that fail to meet requirements.
- Recursion: Efficiently explore solution paths with recursive calls.
- Command-Line Input: Handle user input through the command line using Python’s sys module.
This project is licensed under the ALX Software Engineering Program. © 2024 ALX. All rights reserved.