zoukankan      html  css  js  c++  java
  • 【Lintcode】033.N-Queens

    题目:

    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

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

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

     

    题解:

      此题需要注意的是对角线因素,不仅不能对角线相邻,而且不能在一条对角线上,中间隔着一个也不行!刚开始没注意,花了一个多小时才明白过来。。。

    Solution 1 ()

    class Solution {
    public:
        void dfs(vector<vector<string>>& res, vector<string>& v, int n, vector<int>& pos, int row) {
             if(row >= n) {
                 res.push_back(v);
                 return;
             }
             for(int col=0; col<n; ++col) {
                if (!isValid(pos, row, col)) {
                    continue;
                }
                v[row][col] = 'Q';
                pos[row] = col;
                dfs(res, v, n, pos, row + 1);
                pos[row] = -1;
                v[row][col] = '.';
             }
        }
        bool isValid(vector<int>& pos, int row, int col) {
            for (int i = 0; i < row; ++i) {
                if (pos[i] == col || abs(row - i) == abs(col - pos[i])) {
                    return false; 
                }
            }
            return true;
        }
        vector<vector<string>> solveNQueens(int n) {
            vector<vector<string>> res;
            vector<string> v(n, string(n, '.'));
            vector<int> pos(n, -1);
            dfs(res, v, n, pos, 0);
            return res;        
        }
    };

     Solution 1.2 () from here

    class Solution {
    private:
        vector<vector<string> > res;
    public:
        vector<vector<string> > solveNQueens(int n) {
            vector<string>cur(n, string(n,'.'));
            helper(cur, 0);
            return res;
        }
        void helper(vector<string> &cur, int row)
        {
            if(row == cur.size())
            {
                res.push_back(cur);
                return;
            }
            for(int col = 0; col < cur.size(); col++)
                if(isValid(cur, row, col))
                {
                    cur[row][col] = 'Q';
                    helper(cur, row+1);
                    cur[row][col] = '.';
                }
        }
         
        //判断在cur[row][col]位置放一个皇后,是否是合法的状态
        //已经保证了每行一个皇后,只需要判断列是否合法以及对角线是否合法。
        bool isValid(vector<string> &cur, int row, int col)
        {
            //
            for(int i = 0; i < row; i++)
                if(cur[i][col] == 'Q')return false;
            //右对角线(只需要判断对角线上半部分,因为后面的行还没有开始放置)
            for(int i = row-1, j=col-1; i >= 0 && j >= 0; i--,j--)
                if(cur[i][j] == 'Q')return false;
            //左对角线(只需要判断对角线上半部分,因为后面的行还没有开始放置)
            for(int i = row-1, j=col+1; i >= 0 && j < cur.size(); i--,j++)
                if(cur[i][j] == 'Q')return false;
            return true;
        }
    };

    Solution 1.3 ()

    class Solution2 {
    public:
        std::vector<std::vector<std::string> > solveNQueens(int n) {
            std::vector<std::vector<std::string> > res;
            std::vector<std::string> nQueens(n, std::string(n, '.'));
            solveNQueens(res, nQueens, 0, n);
            return res;
        }
    private:
        void solveNQueens(std::vector<std::vector<std::string> > &res, std::vector<std::string> &nQueens, int row, int &n) {
            if (row == n) {
                res.push_back(nQueens);
                return;
            }
            for (int col = 0; col != n; ++col)
                if (isValid(nQueens, row, col, n)) {
                    nQueens[row][col] = 'Q';
                    solveNQueens(res, nQueens, row + 1, n);
                    nQueens[row][col] = '.';
                }
        }
        bool isValid(std::vector<std::string> &nQueens, int row, int col, int &n) {
            //check if the column had a queen before.
            for (int i = 0; i != row; ++i)
                if (nQueens[i][col] == 'Q')
                    return false;
            //check if the 45° diagonal had a queen before.
            for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; --i, --j)
                if (nQueens[i][j] == 'Q')
                    return false;
            //check if the 135° diagonal had a queen before.
            for (int i = row - 1, j = col + 1; i >= 0 && j < n; --i, ++j)
                if (nQueens[i][j] == 'Q')
                    return false;
            return true;
        }
    };
  • 相关阅读:
    除草第一季 1 简单日常心得
    [搬家from qzone] 读书笔记 人间情味.半成品
    [搬家from qzone] 读书笔记 最好的告别
    [搬家from qzone] 读书笔记 爱是一种选择
    [搬家from qzone] 读书笔记 童年的王国
    [搬家from qzone] 我不是一个很好的学长,所以毕业前,给学习学妹们写写自己犯的错误吧
    博客重新开张 第一篇灌水
    我眼中的vim
    linux启动kdump失败
    centos7 minimal安装之后要做的事情
  • 原文地址:https://www.cnblogs.com/Atanisi/p/6863598.html
Copyright © 2011-2022 走看看