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

(Java)LeetCode-51. N-Queens

2016-10-01 21:51 399 查看
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.

For example,

There exist two distinct solutions to the 4-queens puzzle:
[
[".Q..",  // Solution 1
"...Q",
"Q...",
"..Q."],

["..Q.",  // Solution 2
"Q...",
"...Q",
".Q.."]
]


著名的八皇后问题,早有耳闻,今日终得一见,难得难得。
最典型的回溯算法,逐行来找能放皇后的位置,若到最后没有皇后可以放,就回溯去掉前面的状态。
自己写完后,去看了别人的代码,都差不多,就是有一个地方和别人不太一样,就是如何判断一个位置是否能放一个皇后,别人的做法的是用一个数组存储前面已经放下的皇后的位置,然后再来判断,这样写起来比较简单,省地方,我是直接的找到这个点的上下左右几个方向上看有没有皇后。这个地方不太成熟,要向别人学习。不过还是贴上我自己写的代码,毕竟写了蛮久的。

代码如下:

public List<List<String>> solveNQueens(int n) {
List<List<String>> result = new ArrayList<List<String>>();
List<String> line = new ArrayList<String>();
char[][] cube = new char

;
for(int i = 0; i < cube.length; i++){
for(int j = 0; j < cube[i].length; j++){
cube[i][j] = '.';
}
}
boolean[] flags = new boolean
;
solveNQueens(result, cube, flags, n, 0);
return result;
}

private void solveNQueens(List<List<String>> result, char[][] cube,boolean[] flags,int n, int cnt) {
// TODO Auto-generated method stub
if(cnt == n){
List<String> list = new ArrayList<String>();
for(char[] ch : cube){
list.add(new String(ch));
}
result.add(list);
return ;
}
for(int i = 0; i < flags.length; i++){
if(flags[i] == false){
List<Integer> temp = aPosition(cube, i);
if(temp.size() == 0){
break;
}
for(int t : temp){
cube[i][t] = 'Q';
flags[i] = true;
solveNQueens(result, cube, flags, n , cnt+1);
cube[i][t] = '.';
flags[i] = false;
}
break;
}
}
}

private List<Integer> aPosition(char[][] cube, int num) {
// TODO Auto-generated method stub
List<Integer> result = new ArrayList<Integer>();
int n = cube.length;
boolean flag = false;
for(int i = 0; i < n; i++){ //the direction is "--"
if(cube[num][i] == 'Q'){
return result;
}
}

for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){ //the direction is "|"
if(cube[j][i] == 'Q'){
flag = true;
break;
}
}
if(flag == false){
if(num >= i){ // the direction is "\"
int dif = num - i;
for(int j = 0; j + dif <= n-1; j++){
if(cube[j+dif][j] == 'Q'){
flag = true;
break;
}
}
}else{
int dif = i - num;
for(int j = 0; j + dif <= n-1; j++){
if(cube[j][j+dif] == 'Q'){
flag = true;
break;
}
}
}
if(flag == false){
if(num + i <= n-1){ //the direction is "/"
int sum = i + num;
for(int j = 0; sum >= j; j++ ){
if(cube[j][sum-j] == 'Q'){
flag = true;
break;
}
}
}else{
int sum = i + num;
for(int j = n-1; sum-j <= n-1; j--){
if(cube[sum-j][j] == 'Q'){
flag = true;
break;
}
}
}
}
}
if(flag == false){
result.add(i);
}else{
flag = false;
}
}
return result;
}

public static void main(String[] args){
Solution sol = new Solution();
System.out.println("start");
List<List<String>> re = sol.solveNQueens(4);
for(List<String> l : re){
System.out.println(l);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  backtracking