LeetCode_N-Queens

一.题目

N-Queens

My Submissions
Total Accepted: 42296  Total Submissions: 160923 Difficulty: Hard

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.

For example,
There exist two distinct solutions to the 4-queens puzzle:

[
 [".Q..",  // Solution 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // Solution 2
  "Q...",
  "...Q",
  ".Q.."]
]
Show Tags
Show Similar Problems
Have you met this question in a real interview? 
Yes
 
No

Discuss























二.解题技巧

    这道题是一道典型的图的深度优先搜索算法,只不过要考虑到N皇后的限制条件,即不能有一个皇后与另外一个皇后在同一横线、竖线和对角线上,因此,在放置每一个皇后时,都要考虑是否与已经放置的皇后冲突,这个可以通过使用一个辅助矩阵来记录当前可以放置的位置,在放置每一个皇后的时候,更新这个辅助矩阵,即将与该皇后同行、同列和同一对角线的位置都设置为不可放置的状态,然后再进行下一步,在进行回溯时,要清空这些标志。


三.实现代码

#include <iostream>
#include <string>
#include <vector>

using std::string;
using std::vector;

class Solution
{
private:
    void DFS(int n_Index, vector<vector<int>> &State, vector<string>&Path,
             vector<vector<string>> &Result)
    {
        if (n_Index == State.size() - 1)
        {
            for (int Index = 0; Index < State.size(); ++Index)
            {
                if (State[n_Index][Index] == 1)
                {
                    Path[n_Index][Index] = 'Q';
                    Result.push_back(Path);
                    Path[n_Index][Index] = '.';
                    break;
                }
            }
            return;
        }

        for (int Index = 0; Index < State.size(); ++Index)
        {
            if (State[n_Index][Index] == 1)
            {
                Path[n_Index][Index] = 'Q';
                SetStatue(n_Index, Index, 1, State);
                DFS(n_Index + 1, State, Path, Result);
                SetStatue(n_Index, Index, -1, State);
                Path[n_Index][Index] = '.';
            }
        }

    }

    void SetStatue(int n_Index, int Index, int Value, vector<vector<int>> &State)
    {
        // col
        for (int ColIndex = Index; ColIndex < State.size(); ++ColIndex)
        {
            State[n_Index][ColIndex] += Value;
        }

        // row
        for (int RowIndex = n_Index; RowIndex < State.size(); ++RowIndex)
        {
            State[RowIndex][Index] += Value;
        }

        int RowIndex = n_Index + 1;
        int ColIndex = Index - 1;
        while(RowIndex < State.size() && ColIndex >= 0)
        {
            State[RowIndex][ColIndex] += Value;
            RowIndex++;
            ColIndex--;
        }

        RowIndex = n_Index + 1;
        ColIndex = Index + 1;
        while (RowIndex < State.size() && ColIndex < State.size())
        {
            State[RowIndex][ColIndex] += Value;
            RowIndex++;
            ColIndex++;
        }

    }

public:
    vector<vector<string>> solveNQueens(int n)
    {
        string TmpString(n, '.');
        vector<string> Path(n, TmpString);

        vector<vector<string>> Result;

        vector<int> TmpStatues(n, 1);
        vector<vector<int>> State(n, TmpStatues);

        if (n == 0)
        {
            return Result;
        }

        DFS(0, State, Path, Result);
        return Result;
    }
};




四.体会

    这道题的难度在于更新和恢复辅助矩阵上面,这个地方很有值得深入研究的地方。


版权所有,欢迎转载,转载请注明出处,谢谢微笑




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值