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; } };
相关文章推荐
- leetcode 51.N-Queens & 52. N-Queens II
- Leetcode 51. N-Queens 52. N-Queens II
- leetcode 52. N-Queens II & leetcode 51 N-Queens (N皇后问题)
- LeetCode-51. N-Queens和LeetCode-52. N-Queens II
- LeetCode-51&52.N-Queens
- 51. N-Queens && 52. N-Queens II
- 【LeetCode】N-Queens && N-Queens II
- Leetcode 51. N-Queens (Hard) (cpp)
- <LeetCode OJ> 52. N-Queens II
- leetcode hard模式专杀之51. N-Queens
- leetcode 51|52. N-Queens 1|2
- 51. N-Queens 52. N-Queens II *HARD*
- 51. N-Queens/52. N-Queens II
- 51.leetcode N-Queens(hard)[递归回溯剪枝]
- leetcode N-Queens I && N-Queens II
- Leetcode 52. N-Queens II (Hard) (cpp)
- 【LeetCode】52.N-Queens II(hard)解题报告
- leetcode解题笔记 51.N-Queens [Hard]
- leetCode 51. N-Queens | 回溯问题(N皇后问题) | hard
- [LeetCode] [N皇后问题] N-Queens & N-Queens II