-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNQueens.java
More file actions
105 lines (88 loc) · 2.71 KB
/
NQueens.java
File metadata and controls
105 lines (88 loc) · 2.71 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
/*
THIS CODE WAS MY OWN WORK, IT WAS WRITTEN WITHOUT CONSULTING CODE WRITTEN BY OTHER STUDENTS. Melanie Zhao
*/
import java.util.Stack;
public class NQueens {
//***** fill in your code here *****
//feel free to add additional methods as necessary
static Stack<Integer> s;
//finds and prints out all solutions to the n-queens problem
public static boolean issafe(int row, int col) {
for (int i = 0; i < row; i++) {
if ((s.get(i) == col) // they are in the same column
|| (Math.abs(i-row) == Math.abs(s.get(i)-col))) {//in the same diagonal
return false;
}
}
return true;
}
public static int solve(int n) {
//***** fill in your code here ****
s = new Stack<Integer>();
int solution = 0;
for (int i = 0; i < n; i ++) {
for (int j = 0; j < n; j++) {
if (issafe(i, j)) {
s.push(j);
if(i == n-1) {
solution ++;
printSolution(s);
s.pop();
}
else {
break;
}
}
if (j >= n-1) {
if(s.isEmpty()) {
break;
}
else {
i--;
j = s.get(i);
s.pop();
while(j >= n-1) {
if(i == 0) {
return solution;
}
i--;
j = s.get(i);
s.pop();
}
}
}
}
}
//update the following statement to return the number of solutions found
return solution;
}//solve()
//this method prints out a solution from the current stack
//(you should not need to modify this method)
private static void printSolution(Stack<Integer> s) {
for (int i = 0; i < s.size(); i ++) {
for (int j = 0; j < s.size(); j ++) {
if (j == s.get(i))
System.out.print("Q ");
else
System.out.print("* ");
}//for
System.out.println();
}//for
System.out.println();
}//printSolution()
// ----- the main method -----
// (you shouldn't need to change this method)
public static void main(String[] args) {
int n = 8;
// pass in parameter n from command line
if (args.length == 1) {
n = Integer.parseInt(args[0].trim());
if (n < 1) {
System.out.println("Incorrect parameter");
System.exit(-1);
}//if
}//if
int number = solve(n);
System.out.println("There are " + number + " solutions to the " + n + "-queens problem.");
}//main()
}