![]() |
VOOZH | about |
Given an 8x8 chessboard, the task is to place 8 queens on the board such that no 2 queens threaten each other. Return a matrix of size 8x8, where 1 represents queen and 0 represents an empty position.
Approach:
The idea is to use backtracking to place the queens one by one on the board. Starting from the first row, we try placing queens in different columns and check if it's safe (not under attack from previously placed queens). If a safe position is found, we move to the next row. If at any point no safe position exists in a row, we backtrack to the previous row and try a different column placement. This recursive approach systematically explores all possible configurations until a valid solution is found.
Step by step approach:
1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0
Related Articles: