158.N-Queens
2016-08-26 10:31
113 查看
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
indicate a queen and an empty space respectively.
For example,
There exist two distinct solutions to the 4-queens puzzle:
Subscribe to see which companies asked this question
注意:用for循环自动的可以控制回溯,不需要人为的显示地弹出和压入。
/*
* Step1:定义一个数组nodes,nodes[i]表示第i行的皇后存放的列下标;
* Step2:定义一个函数来整理所有的结点都存放到矩阵之后的结果;
* Step3:定义一个函数来判断(row,j)是否可以存放的目前的结果中;
* Step4:定义一个函数来为row行的皇后找地方,如果找到了则继续为row+1找位置,如果没有找到则退回到row-1,尝试为
* row-1找其当前位置之后的位置。
*/
/*准备要返回的结果*/
List<List<String>> result = new ArrayList<List<String>>();
int[] nodes;//nodes[i]的value值表示标号为i行存储的位置
int num;
public List<List<String>> solveNQueens(int n) {
/*Step1:初始化每行的那个结点即将要尝试的坐标*/
nodes = new int
;
num = n;
/*Step2:开始尝试*/
addNode(0);
/*Step3:返回最后的结果*/
return result;
}
/*往结果集中添加结点,row表示本次准备放的行号*/
private void addNode(int row){
/*如果所有的结点都已经放到均镇中,则整理结果*/
if(row == num){
addResult();
}else{
for(int i=0; i<num; i++){/*如果找到了则继续为row+1找位置,如果没有找到则退回到row-1,尝试为row-1找其当前位置之后的位置。*/
nodes[row] = i;
if(CanAddNode(row , nodes[row])){
addNode(row+1);
}
}
}
}
/*整理当前遍历到的结果,走到这一步说明所有的结点都加入到了list中,只需要利用这个方法进行整理结果即可*/
private void addResult(){
List<String> solution = new ArrayList<String>();
for(int i=0;i<num;i++){
StringBuilder sb = new StringBuilder();
int k =0;
while(k<nodes[i]){
sb.append(".");
k++;
}
sb.append("Q");
k++;
while(k<num){
sb.append(".");
k++;
}
solution.add(sb.toString());
}
result.add(solution);
}
/*判断(row,j)能否加入到当前的结果中*/
private boolean CanAddNode(int row,int j){
for(int k =0;k<row;k++){
if( nodes[k] == j || Math.abs(nodes[k] - j)== row - k){//说明列有重复或者在同一对角线上,要返回false
return false;
}
}
return true;
}
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.."] ]
Subscribe to see which companies asked this question
注意:用for循环自动的可以控制回溯,不需要人为的显示地弹出和压入。
/*
* Step1:定义一个数组nodes,nodes[i]表示第i行的皇后存放的列下标;
* Step2:定义一个函数来整理所有的结点都存放到矩阵之后的结果;
* Step3:定义一个函数来判断(row,j)是否可以存放的目前的结果中;
* Step4:定义一个函数来为row行的皇后找地方,如果找到了则继续为row+1找位置,如果没有找到则退回到row-1,尝试为
* row-1找其当前位置之后的位置。
*/
/*准备要返回的结果*/
List<List<String>> result = new ArrayList<List<String>>();
int[] nodes;//nodes[i]的value值表示标号为i行存储的位置
int num;
public List<List<String>> solveNQueens(int n) {
/*Step1:初始化每行的那个结点即将要尝试的坐标*/
nodes = new int
;
num = n;
/*Step2:开始尝试*/
addNode(0);
/*Step3:返回最后的结果*/
return result;
}
/*往结果集中添加结点,row表示本次准备放的行号*/
private void addNode(int row){
/*如果所有的结点都已经放到均镇中,则整理结果*/
if(row == num){
addResult();
}else{
for(int i=0; i<num; i++){/*如果找到了则继续为row+1找位置,如果没有找到则退回到row-1,尝试为row-1找其当前位置之后的位置。*/
nodes[row] = i;
if(CanAddNode(row , nodes[row])){
addNode(row+1);
}
}
}
}
/*整理当前遍历到的结果,走到这一步说明所有的结点都加入到了list中,只需要利用这个方法进行整理结果即可*/
private void addResult(){
List<String> solution = new ArrayList<String>();
for(int i=0;i<num;i++){
StringBuilder sb = new StringBuilder();
int k =0;
while(k<nodes[i]){
sb.append(".");
k++;
}
sb.append("Q");
k++;
while(k<num){
sb.append(".");
k++;
}
solution.add(sb.toString());
}
result.add(solution);
}
/*判断(row,j)能否加入到当前的结果中*/
private boolean CanAddNode(int row,int j){
for(int k =0;k<row;k++){
if( nodes[k] == j || Math.abs(nodes[k] - j)== row - k){//说明列有重复或者在同一对角线上,要返回false
return false;
}
}
return true;
}
相关文章推荐
- [转]Eight Queens algorithm With ABAP
- LeetCode: N-Queens
- 【leetcode】N-Queens
- leetcode -- N-Queens
- 【second】N-Queens
- [Leetcode] N-Queens (Java)
- 生成N皇后问题所有局面 N-Queens
- N-Queens
- leetcode: N-Queens
- Leetcode N-Queens
- N-Queens
- ACM 158. [USACO Oct07] 奶牛饮水(计数)
- LeetCode :My solution N-Queens
- 【Leetcode】N-Queens (Backtracking)
- LeetCode: N-Queens
- Q9.9 N queens
- LeetCode 51.N-Queens
- N-Queens 1&2
- 【N-Queens】cpp
- [刷题]N-Queens