forked from ineedatext/algorithm_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path123.java
More file actions
60 lines (52 loc) · 1.51 KB
/
123.java
File metadata and controls
60 lines (52 loc) · 1.51 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
package exp4;
import java.util.Random;
public class LV{
static int SIZE = 8;
static Random rnd = new Random(SIZE);
static int[] queen = new int[SIZE];
private static boolean check(int row) {
for (int i = 0; i < queen.length && i != row; i++) {
if (queen[i] == queen[row] || i - queen[i] == row - queen[row] || i + queen[i] == row + queen[row]) {
return false;
}
}
return true;
}
private static boolean queensLV() {
int row = 0;
int count = 1;
while ((row < SIZE) && (count > 0)) {
count = 0;
int j = 0;
for (int column = 0; column < SIZE; column++) {
queen[row] = column;
if (check(row)) {
if (rnd.nextInt(++count) == 0) {
j = column;
}
}
}
if (count > 0) {
queen[row++] = j;
}
}
return (count > 0);
}
public static void nQueen() {
while (!queensLV());
System.out.println("-----½â·¨--------");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (queen[i] == j) {
System.out.print(" Q ");
} else {
System.out.print(" . ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
nQueen();
}
}