zoukankan      html  css  js  c++  java
  • LeetCode_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.
    

      nqueue

    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.."]
    ]
    

     分析: The classic recursive problem.
    1. Use a int vector to store the current state,  A[i]=j refers that the ith row and jth column is placed a queen.
    2. Valid state:  not in the same column, which is A[i]!=A[current], not in the same diagonal direction: abs(A[i]-A[current]) != r-i

    3. Recursion: 
           Start:   placeQueen(0,n)
            if current ==n then print result
            else
                for each place less than n,
                     place queen
                    if current state is valid, then place next queen   place Queen(cur+1,n)
               end for
            end if

    class Solution {
    public:
     void record(vector<int> row)
        {
            vector<string> temp;
            for(int i = 0; i< n ; i++)
            {
                string str(n,'.');
                str[row[i]] = 'Q';
                temp.push_back(str);
            }
            res.push_back(temp) ;
        }
        bool isValid(vector<int> row, int curRow)
        {
            for(int i = 0; i< curRow; i++)
              if(row[i] == row[curRow] || abs(row[i] - row[curRow]) == curRow - i)
                  return false;
                  
            return true;
        }
        void nqueue(vector<int> row,int curRow)
        {
            if(curRow == n)
            {
                record(row);
                return ;
            }
            for(int i = 0; i< n ;i++)
            {
                row[curRow] = i;
                if(isValid(row,curRow))
                       nqueue(row,curRow+1);
            }
        }
        vector<vector<string> > solveNQueens(int n) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            res.clear();
            if( n < 1 ) return res;
            this->n = n;
             vector<int> row(n,-1);
            nqueue(row, 0);
            return res;
        }
    private:
        int n;
        vector<vector<string> > res;
    };

    http://yucoding.blogspot.com/2013/01/leetcode-question-59-n-queens.html

  • 相关阅读:
    对于使用secureFX上传文件到centos7 的时候,以及不同的用户解压文件,对于文件操作权限的实验
    搭建分布式hadoop环境的前期准备---需要检查的几个点
    mvc EF
    查询数据库的相关信息
    SQL中PIVOT 行列转换
    sql server 取日期
    c# 类型拷贝
    EF没有同步更新(转)
    怎么计算两个经纬度之间的距离.
    简单日志记录
  • 原文地址:https://www.cnblogs.com/graph/p/3226728.html
Copyright © 2011-2022 走看看