-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueensBoard.java
More file actions
56 lines (53 loc) · 1.4 KB
/
QueensBoard.java
File metadata and controls
56 lines (53 loc) · 1.4 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
package datastructure.chapter4_stackandqueue;
import java.util.Scanner;
public class QueensBoard {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of Queens >> ");
int n = sc.nextInt();
GenericStack<Point> board = new GenericStack<>(n);
int i = 0;
int j = 0;
int count = 0;
while (i < n) {
for (; j < n; j++) {
int i2 = 0;
for (; i2 < i; i2++) {// 앞에 놓인 것이랑 충돌 확인
Point p = board.get(i2);
int x = p.getX();
int y = p.getY();
if (y == j || Math.abs(x - i) == Math.abs(y - j)) {// 충돌하면 다음 위치 찾아봐야지
break;
}
}
if (i2 == i) {// 충돌 안 했다
Point pnew = new Point(i, j);
board.push(pnew);
i++;
j = -1;
if (board.isFull()) {// 정답 찾음
printAnswer(board);
count++;
}
}
}
if (i > 0) {// 앞으로 돌아감
Point p = board.pop();
i--;
j = p.getY() + 1;
} else {
break;
}
}
System.out.println("The number of answers: " + count);
sc.close();
}
public static void printAnswer(GenericStack<Point> board) {
int n = board.size();
for (int i = 0; i < n; i++) {
int col = board.get(i).getY();
System.out.println("○ ".repeat(col) + "● " + "○ ".repeat(n - col - 1));
}
System.out.println();
}
}