[Leetcode]N-Queens
2015-12-15 16:55
459 查看
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
For example,
There exist two distinct solutions to the 4-queens puzzle:
class Solution {
public:
/*algorithm: DFS
*/
void addSolution(vector<vector<string>>&result,vector<int>&solution){
int n = solution.size();
vector<string>board;
for(int i = 0;i < n;i++){
string line(n,'.');
line[solution[i]] = 'Q';
board.push_back(line);
}
result.push_back(board);
}
vector<int>getNextCandiates(vector<int>&solution,int n)
{
vector<int>result;
for(int i = 0; i < n;i++){
bool selected = true;
for(int k = 0;k < solution.size();k++){
if(i == solution[k] || //same column or same k (k=(y2-y1)/(x2-x1))
abs(i-solution[k])==abs((int)solution.size()-k)){
selected = false;
break;
}
}
if(selected)result.push_back(i);
}
return result;
}
void dfs(int n,int start,vector<vector<string>>&result,vector<int>&solution){
if(start == n){ //find one solution,reach last line
addSolution(result,solution);
return;
}
{
vector<int>candiates = getNextCandiates(solution,n);
for(int k = 0;k < candiates.size();k++){
solution.push_back(candiates[k]);
dfs(n,start+1,result,solution);
solution.pop_back();
}
}
}
vector<vector<string>> solveNQueens(int n) {
vector<int>solution;
vector<vector<string> >result;
dfs(n,0,result,solution);
return result;
}
};
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.."] ]
class Solution {
public:
/*algorithm: DFS
*/
void addSolution(vector<vector<string>>&result,vector<int>&solution){
int n = solution.size();
vector<string>board;
for(int i = 0;i < n;i++){
string line(n,'.');
line[solution[i]] = 'Q';
board.push_back(line);
}
result.push_back(board);
}
vector<int>getNextCandiates(vector<int>&solution,int n)
{
vector<int>result;
for(int i = 0; i < n;i++){
bool selected = true;
for(int k = 0;k < solution.size();k++){
if(i == solution[k] || //same column or same k (k=(y2-y1)/(x2-x1))
abs(i-solution[k])==abs((int)solution.size()-k)){
selected = false;
break;
}
}
if(selected)result.push_back(i);
}
return result;
}
void dfs(int n,int start,vector<vector<string>>&result,vector<int>&solution){
if(start == n){ //find one solution,reach last line
addSolution(result,solution);
return;
}
{
vector<int>candiates = getNextCandiates(solution,n);
for(int k = 0;k < candiates.size();k++){
solution.push_back(candiates[k]);
dfs(n,start+1,result,solution);
solution.pop_back();
}
}
}
vector<vector<string>> solveNQueens(int n) {
vector<int>solution;
vector<vector<string> >result;
dfs(n,0,result,solution);
return result;
}
};
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析