您的位置:首页 > 产品设计 > UI/UE

Leetcode 51. N-Queens && 52. N-Queens II(Hard)

2016-11-15 00:21 465 查看

Problem:

51.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.

52.Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

Example:

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

51.

[

[“.Q..”, // Solution 1

“…Q”,

“Q…”,

“..Q.”],

[“..Q.”, // Solution 2

“Q…”,

“…Q”,

“.Q..”]

]

52.

2

Algorithm:

这两道题目为经典的n皇后问题,51题为列出所有的可行解,52题为求可行解的总数。
关于n皇后问题的基本解法是DFS。即遍历棋盘所有的行i,对每一行枚举所有的列j,判断第i个皇后放在(i,j)位置时是否会和前面i-1行的i-1个皇后发生冲突,若不冲突,则继续向下进行DFS。
判断冲突的方法是:判断(i,j)所在的行、列和两条斜线(4个数组)上是否已经有别的皇后,若是,则发生冲突。否则,没有冲突,并把(i,j)所在的行、列和两条斜线标记为已经有皇后。
对于时间复杂度的粗略计算为:第一行枚举n种情况,第二行n-1种情况……因此总的时间复杂度为O(n),但是上大部分情况都可排除,因此时间复杂度会更小。


Code:

这里只提供51的代码,52可通过改变返回类型获得

#include<iostream>
#include<vector>
#include<string>
using namespace std;

const int max_board=30;
int total_num=0;
vector<vector<string> > ans;

class Queens
{
public:
Queens(int size);
bool is_solved()const{
return count==board_size;
}
void print() const{
//cout << "下面是解决" << board_size << "皇后问题的第"<<total_num<<"组解" << endl;
vector<string> tv;
string ts;
for(int i=0;i<count;++i){
for(int j=0;j<count;++j){
if(j==queen_in_row[i]) {
//cout<<"Q";
ts.append("Q");
}
else {
//cout<<".";
ts.append(".");
}
//                  if(j==queen_in_row[i]){
//                      if(i==count-1) cout<<j+1;
//                      else cout<<j+1<<" ";
//                  }
}
//cout<<endl;
tv.push_back(ts);
ts.clear();
}
//cout<<endl;
ans.push_back(tv);
}
bool unguarded(int col)const;
void insert(int col);
void remove(int col);
void solve_from(Queens &configuration);
int board_size;

private:
int count;
bool col_free[max_board];
bool upward_free[2*max_board-1];
bool downward_free[2*max_board-1];
int queen_in_row[max_board];
};

Queens::Queens(int size)
{
board_size=size;
count=0;
for(int i=0;i<board_size;++i) col_free[i]=true;
for(int i=0;i<(2*board_size-1);++i){
upward_free[i]=true;
downward_free[i]=true;
}
}

void Queens::insert(int col)
{
queen_in_row[count]=col;
col_free[col]=false;
upward_free[count+col]=false;
downward_free[count-col+board_size-1]=false;
count++;
}

void Queens::remove(int col)
{
count--;
col_free[col]=true;
upward_free[count+col]=true;
downward_free[count-col+board_size-1]=true;
}

bool Queens::unguarded(int col)const
{
return col_free[col]&&upward_free[count+col]&&downward_free[count-col+board_size-1];
}

void Queens::solve_from(Queens &configuration)
{
if(configuration.is_solved()){
total_num++;
configuration.print();
}
else
{
for(int col = 0;col < configuration.board_size;++col)
{
if(configuration.unguarded(col))
{
configuration.insert(col);   //在(count,col)处放置皇后
solve_from(configuration);   //递归,继续求解
configuration.remove(col);   //去掉先前在(count,col)处放置的皇后
}
}
}
}

class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
Queens configuration(n);
configuration.solve_from(configuration);
vector<vector<string>> as = ans;
ans.clear();
return as;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: