码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode_N-Queens

时间:2015-10-26 07:06:15      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:c++   leetcode   算法题   深度优先搜索   

一.题目

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;
    }
};




四.体会

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


版权所有,欢迎转载,转载请注明出处,谢谢技术分享




版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode_N-Queens

标签:c++   leetcode   算法题   深度优先搜索   

原文地址:http://blog.csdn.net/sheng_ai/article/details/49416353

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!