The N-Queens Solver is a C++ program that tackles the classic N-Queens problem, leveraging recursion and backtracking. This problem involves placing N queens on an N x N chessboard in such a way that no two queens threaten each other. The solution employs dynamic memory allocation for a 2D array representing the chessboard.
-
Recursion and Backtracking: The heart of the solution lies in the recursive
N_Queen
function, exploring safe queen placements and backtracking when needed. -
Dynamic Memory Allocation: The program dynamically allocates a 2D array (
arr
) to simulate the chessboard, ensuring efficient memory usage. -
Array Manipulation: The
Is_Safe
function carefully examines rows, columns, and diagonals in the chessboard array (arr
) to determine safe queen placements. -
Input/Output Handling: Users are prompted to input the size of the chessboard (
n
), and the program outputs the resulting configuration when a solution is found. -
Pointer and Memory Management: Pointers are used for dynamic memory allocation, and memory is deallocated at the end to prevent memory leaks.
- Language: C++
- Concepts: Recursion, Backtracking, Dynamic Memory Allocation
-
Compile: Use a C++ compiler to build the executable from the provided source code.
g++ main -o main.cpp
-
Run: Execute the compiled program.
./main
-
Input: Enter the desired size of the chessboard when prompted.
-
Output: View the resulting N-Queens configuration if a solution is found.
Enter the size of the chessboard: 4
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0
Feel free to contribute to this project by opening issues or submitting pull requests. Your feedback and improvements are highly appreciated.