LeetCode 51 | 52. N-Queens (回溯)

本文详细介绍了N皇后问题的经典解决方案,使用回溯算法找到所有可能的放置方式使得N个皇后在N×N的棋盘上互不攻击。通过C++实现,展示了如何检查安全性、放置皇后及移除皇后的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经典回溯问题~~~

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

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

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.

Example:


Input: 4
Output: [
 [".Q..",  // Solution 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // Solution 2
  "Q...",
  "...Q",
  ".Q.."]
]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.

AC代码:

class Solution {
public:
    vector<vector<string>> solveNQueens(int n) {
         vector<vector<string> > res;
        vector<string> sol;
        
        //initialize the sol
        string s = "";
        for(int i = 0; i < n; i++)
            s += ".";
        for(int i = 0; i < n; i++)
            sol.push_back(s);
        
        backTracking(sol, n, 0, res);
        return res;
    }
    
    // void print(vector<string> &sol){
    //     for(int i = 0; i < sol.size(); i++)
    //         cout << sol[i] << endl;
    // }
    
    void backTracking(vector<string>& sol, int n, int column, vector<vector<string> > & res){
        if(column == n){   // no queen left
            res.push_back(sol);
            //print(sol);
            return;
        }
        
        for(int row = 0; row < n; row++){
            if(isSafe(row, column, n, sol)){
                place(row, column, sol);
                backTracking(sol, n, column + 1, res);
                remove(row, column,sol);
            }
        }
        return;  //如果这一列的每一行都无法放一个Queen,就会自动return了。
    }
    
    bool isSafe(int row, int column, int n, vector<string> & sol){
        for(int i = 0; i < n; i++){  //同行或同列
            if(sol[row][i] == 'Q' || sol[i][column] == 'Q')
                return false;
        }
        for(int i = row, j = column; i < n && j < n; i++,j++){   //对角线方向1
            if(sol[i][j] == 'Q')
                return false;
        }
        for(int i = row, j = column; i >= 0 && j >= 0; i--,j--){ //对角线方向2
            if(sol[i][j] == 'Q')
                return false;
        }
        for(int i = row, j = column; i < n && j >= 0; i++,j--){ //对角线方向3
            if(sol[i][j] == 'Q')
                return false;
        }
        for(int i = row, j = column; i >=0 && j < n; i--,j++){ //对角线方向4
            if(sol[i][j] == 'Q')
                return false;
        }
        
        return true;
    }
    
    void place(int row, int column, vector<string> & sol){
        sol[row][column] = 'Q';
    }
    
    void remove(int row, int column, vector<string> & sol){
        sol[row][column] = '.';
    }
};

52N-Queens II

52就是统计个个数,直接改一改好了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值