The N-Queens problem involves placing N queens on an N × N chessboard such that no two queens threaten each other. This means:
- No two queens should be in the same row.
- No two queens should be in the same column.
- No two queens should be in the same diagonal.
-
Start with an empty board:
Create anN × Nboard initialized with0, where0represents an empty cell and1represents a placed queen. -
Place queens row by row:
Begin placing queens from row 0 to row N-1. -
Check for a safe position:
- Before placing a queen at
board[row][col], check if it is safe using theis_safefunction. - Ensure no queen is present in the same column or diagonal (both left and right).
- Before placing a queen at
-
Recursive Backtracking:
- If a queen can be placed at
board[row][col], mark it as1. - Recursively call the function for the next row.
- If a valid solution is found (i.e., all queens are placed), store it in the
solutionslist. - Backtrack: If placing a queen leads to an invalid state, remove it (
board[row][col] = 0) and try the next column.
- If a queen can be placed at
-
Repeat Until All Solutions are Found:
Once all rows are processed, return the list of valid board configurations.
Solution Output:
Q . . .
. . Q .
. Q . .
. . . Q
. . Q .
Q . . .
. . . Q
. Q . .
Each row contains one queen, and no two queens attack each other.