forked from xiaoningning/java-algorithm-2010
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueen.java
More file actions
126 lines (115 loc) · 3.66 KB
/
Queen.java
File metadata and controls
126 lines (115 loc) · 3.66 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.Arrays;
/**
* Solve the 8 queens problem using recursion and backtracing.
* Prints out all solutions.
* <p/>
* Limitations: works for N <= 25, but slows down considerably
* for larger N.
* <p/>
* Remark: this program implicitly enumerates all N^N possible
* placements (instead of N!), but the backtracing prunes off
* most of them, so it's not necessarily worth the extra
* complication of enumerating only permutations.
* <p/>
* <p/>
* % java Queens 3
* <p/>
* % java Queens 4
* * Q * *
* * * * Q
* Q * * *
* * * Q *
* <p/>
* * * Q *
* Q * * *
* * * * Q
* * Q * *
* <p/>
* % java Queens 8
* Q * * * * * * *
* * * * * Q * * *
* * * * * * * * Q
* * * * * * Q * *
* * * Q * * * * *
* * * * * * * Q *
* * Q * * * * * *
* * * * Q * * * *
*/
public class Queen {
/**
* ********************************************************************
* Return true if queen placement q[n] does not conflict with
* other queens q[0] through q[n-1]
* *********************************************************************
*/
public static boolean isConsistent(int[] q, int n) {
for (int i = 0; i < n; i++) {
if (q[i] == q[n]) return false; // same column
if ((q[i] - q[n]) == (n - i)) return false; // same major diagonal
if ((q[n] - q[i]) == (n - i)) return false; // same minor diagonal
}
return true;
}
// since it is unique int array, there is no need to compare column/row
public static boolean isConsistent(int[] q) {
for (int i = 0; i < q.length; i++) {
for (int j = i + 1; j < q.length; j++) {
if (q[i] == q[j]) return false; // same column
if ((q[i] - q[j]) == (j - i)) return false; // same major diagonal
if ((q[j] - q[i]) == (j - i)) return false; // same minor diagonal
}
}
return true;
}
/**
* ********************************************************************
* Print out N-by-N placement of queens from permutation q in ASCII.
* *********************************************************************
*/
public static void printQueens(int[] q) {
int N = q.length;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (q[i] == j) System.out.print("Q ");
else System.out.print("* ");
}
System.out.println();
}
System.out.println();
}
public static void permQueen(String prefix, String s) {
int N = s.length();
if (N == 0) {
int[] tmp = new int[prefix.length()];
for (int i = 0; i < prefix.length(); i++)
tmp[i] = Integer.valueOf(prefix.substring(i, i + 1));
if (isConsistent(tmp)) {
System.out.println(prefix);
printQueens(tmp);
}
return;
}
for (int i = 0; i < N; i++)
permQueen(prefix + s.charAt(i), s.substring(0, i) + s.substring(i + 1));
}
public static void enumerate(int[] q, int n) {
int N = q.length;
if (n == N) {
System.out.println(Arrays.toString(q));
printQueens(q);
} else {
for (int i = 0; i < N; i++) {
q[n] = i;
if (isConsistent(q, n)) enumerate(q, n + 1);
}
}
}
public static void main(String[] args) {
int N = 4;
enumerate(new int[N], 0);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < N; i++)
sb.append(String.valueOf(i));
permQueen("", sb.toString());
}
}