n-Queen’s problem

The N-Queens problem is a classic problem in computer science that involves placing N queens on an NxN chessboard such that no two queens threaten each other. In other words, no two queens should share the same row, column, or diagonal. The problem can be solved using a backtracking algorithm. Here's the pseudocode for solving the N-Queens problem:

NQueens(n):
    board = empty NxN chessboard
    return SolveNQueens(board, n, 0)

SolveNQueens(board, n, row):
    if row = n then
        return board

    for col = 0 to n-1 do
        if IsSafe(board, n, row, col) then
            board[row][col] = queen
            result = SolveNQueens(board, n, row+1)
            if result is not null then
                return result
            board[row][col] = empty

    return null

IsSafe(board, n, row, col):
    for i = 0 to row-1 do
        if board[i][col] = queen then
            return false

    i = row-1
    j = col-1
    while i >= 0 and j >= 0 do
        if board[i][j] = queen then
            return false
        i = i - 1
        j = j - 1
    i = row-1
    j = col+1
    while i >= 0 and j < n do
        if board[i][j] = queen then
            return false
        i = i - 1
        j = j + 1

    return true

Let's go through the pseudocode step by step:

1. The `NQueens` function initializes an empty NxN chessboard and calls the `SolveNQueens` function to solve the N-Queens problem.

2. The `SolveNQueens` function is a recursive function that tries to place queens on each row of the chessboard, starting from the first row (`row = 0`) up to the last row (`row = n-1`).

3. For each row, it iterates through each column (`col`) and checks if it is safe to place a queen at position (`row`, `col`) using the `IsSafe` function.

4. If it is safe, it marks that position as a queen, and then recursively calls `SolveNQueens` for the next row (`row+1`).

5. If the recursive call returns a non-null value, it means a solution has been found, so it returns the resulting board configuration.

6. If the recursive call returns null, it means no solution was found with the current placement of queens, so it backtracks by removing the queen from the current position (`board[row][col] = empty`) and continues with the next column.

7. The `IsSafe` function checks if it is safe to place a queen at position (`row`, `col`) by checking if there are no queens on the same column, same diagonal, or same anti-diagonal in the preceding rows.

8. It checks for conflicts by traversing the preceding rows and comparing the positions of the queens with the current position (`row`, `col`).

9. If it finds a conflict, it returns false, indicating that it is not safe to place a queen at the current position.

10. If no conflicts are found, it returns true, indicating that it is safe to place a queen at the current position.


The `queen` and `empty` represent the presence or absence of a queen on a particular cell.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext