The Indian Engineer

Problem 52 N-Queens II

Posted on 4 mins

Backtracking

Problem Statement

Link - Problem 52

Question

The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.

Given an integer n, return the number of distinct solutions to the n-queens puzzle.

Example 1:

Input: n = 4
Output: 2
Explanation: There are two distinct solutions to the 4-queens puzzle as shown.

Example 2:

Input: n = 1
Output: 1

Constraints:

Solution

class Solution {
public:
    bool canplacequeen(int row,int col,vector<string>&board){
        for(int i=row-1;i>=0;i--){
            if(board[i][col]=='Q')
                return false;
        }
        for(int i=row-1,j=col-1;i>=0&&j>=0;i--,j--){
            if(board[i][j]=='Q')
                return false;
        }
        for(int i=row-1,j=col+1;i>=0&&j<board.size();i--,j++){
            if(board[i][j]=='Q')
                return false;
        }
        return true;
    }
    void solvequeen(int row,vector<string> &board,int &count){
        if(row==board.size()){
            count++;
            return;
        }
        for(int col=0;col<board.size();col++){
            if(canplacequeen(row,col,board)){
                board[row][col]='Q';
                solvequeen(row+1,board,count);
                board[row][col]='.';
            }
        }
    }
    int totalNQueens(int n) {
        int count=0;
        vector<string> board(n,string(n,'.'));
        solvequeen(0,board,count);
        return count;
    }
};

Complexity Analysis

| Algorithm    | Time Complexity | Space Complexity |
| ------------ | --------------- | ---------------- |
| Backtracking | O(n!)           | O(n)             |

Explanation

Intial Thoughts

To approach the n-queens puzzle, we should first understand the rules of placing queens on a chessboard, considering that no two queens can attack each other. This involves thinking about how each queen affects the placement of others. We can start by placing queens one by one, ensuring that each placement does not lead to any conflicts. Key points to consider are: think of the problem as placing queens row by row, consider the constraints of a chessboard, think about how each queen placement affects the next, and visualize the problem to understand the queen’s movement patterns.

Intuitive Analysis

Intuitively solving the n-queens puzzle involves breaking it down into manageable steps. This can be achieved by focusing on one row at a time and using a method similar to trial and error, but in a systematic way. Key intuitive strategies include: using backtracking to explore all possible solutions, visualizing the queen’s movement to anticipate conflicts, dividing the problem into smaller sub-problems, considering symmetry to reduce the search space, and using a recursive approach to methodically explore all possible configurations.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Hard difficulty.


Similar Questions:

Title URL Difficulty
N-Queens https://leetcode.com/problems/n-queens Hard