-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheightQueue.java
More file actions
executable file
·57 lines (51 loc) · 1.84 KB
/
eightQueue.java
File metadata and controls
executable file
·57 lines (51 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by Administrator on 2018/3/29 0029.
*/
public class eightQueue {
List<List<Integer>> result=new ArrayList<List<Integer>>();
public static void main(String[] args) {
eightQueue re=new eightQueue();
re.result=re.combine(8,8);//从0到n-1列共选取k皇后
System.out.println(re.result);
// System.out.println(place(Arrays.asList(2)));
}
public List<List<Integer>> combine(int n, int k) {
List<Integer> list=new ArrayList<Integer>();
backtracking(n,k,0,list);
return result;
}
public void backtracking(int n,int k,int start,List<Integer>list){
if(k==0) {
result.add(new ArrayList(list));
return;
}
else{
for(int i=start;i<n;i++){
list.add(i);//尝试性的加入i
//开始回溯
if(place(list)){
backtracking(n,k-1,0,list);
//递归返回后(只有成功才能返回)成功被加到result则需要删除最后成功的节点,然后再试他的旁节点
list.remove(list.size()-1);
}
else {
//停止这里的子树搜索,换父节点的旁节点前,需要先删掉没有用的子节点
list.remove(list.size()-1);
continue;
}
}
}
}
public static boolean place(List<Integer> list){
int size=list.size();
for(int j=0;j<size-1;j++){
if(Math.abs(j-size+1)==Math.abs(list.get(j)-list.get(size-1))||list.get(j)==list.get(size-1))
return false;
}
return true;
}
}