-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNQueen.java
147 lines (113 loc) · 4.39 KB
/
NQueen.java
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Created by jkeys on 9/28/2015.
*/
import java.lang.Math;
import java.util.*;
public class NQueen {
/* private static boolean checkConflict(Position QPos, LinkStack<Position> QCols) {
Integer currCol;
for(int i = 0; i < QCols.size(); i++) {
currCol = QCols.pop();
if(QPos.colid == currCol)
return false;
}
return true;
}*/
private static boolean checkConflict(Position QPos, LinkStack<Position> S)
{
if(S.size() == 0)
return false;
//printStack(S);
LinkStack<Position> temp = new LinkStack<Position>();
Position tempPos;
boolean conflict = false;
//copy stuff from S to temp
while((tempPos = S.pop()) != null) {
temp.push(tempPos);
if( (QPos.colid == tempPos.colid) || (QPos.rowid == tempPos.rowid) ||
(Math.abs(QPos.colid - tempPos.colid) == Math.abs(QPos.rowid - tempPos.rowid)) ) {
//System.out.println("conflict = true, qPos.colid == " + QPos.colid + " and QPos.rowid == " + QPos.rowid);
//System.out.println("..............., tempPos.colid == " + tempPos.colid + " and tempPos.rowid == " + tempPos.rowid);
//System.out.println((Math.abs(QPos.colid - tempPos.colid) == Math.abs(QPos.rowid - tempPos.rowid)));
conflict = true;
break;
}
}
/* while(!temp.isEmpty()) {
Position top = temp.pop();
//pop out the top
//push top to temp
//if QPos and top conflict, break
}*/
while((tempPos = temp.pop()) != null) {
S.push(tempPos);
}
//put everything back in original stack
//abs(qpos.rowid - top.rowid) == abs(qpos.colid - top.colid);
//return conflict/not conflict
//printStack(S);
return conflict;
}
private static void printStack(StackInterface<Position> S) {
Position tempPos;
LinkStack<Position> temp = new LinkStack<Position>();
while((tempPos = S.pop()) != null) {
System.out.println(tempPos);
temp.push(tempPos);
}
while((tempPos = temp.pop()) != null) {
S.push(tempPos);
}
}
public static void main(String[] args) {
//NQueen p1 = new NQueen();
Position QPos = new Position(1, 1);
LinkStack<Position> S = new LinkStack<Position>();
boolean solutionExists = true;
//S.push(QPos);
System.out.println("Please enter single integer for size of board (N rows, N cols, N queens): ");
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
while(S.size() < N) {
System.out.println("S.size() == " + S.size());
System.out.println("In main loop, QPos.colid == " + QPos.colid + " and QPos.rowid == " + QPos.rowid);
boolean conflict = checkConflict(QPos, S);
if(!conflict) {
System.out.println("No conflict, pushing QPos: " + QPos);
S.push(QPos);
printStack(S);
QPos = new Position(1, S.size() + 1);
}
else if(conflict && QPos.colid < N) {
QPos.colid++;
} else if(conflict && QPos.colid == N) {
System.out.println("Conflict exists and no room to move rightward, QPos.colid == " + QPos.colid + ", S.size() == " + S.size());
System.out.println("S.top() == " + S.top());
printStack(S);
while(S.top() != null && S.top().colid == N) {
//System.out.println("In the while loop");
S.pop();
}
if(S.size() != 0)
S.top().colid++;
System.out.println("Stack after while loop: ");
printStack(S);
}
if(QPos.colid > N) {
solutionExists = false;
break;
}
}
if(!solutionExists) {
System.out.println("No solution exists with N = " + N);
} else {
System.out.println("A solution has been found: ");
printStack(S);
}
}
/*
public static void main(String[] args) {
LinkStack<Integer> data = new LinkStack<Integer>();
Position QPos = new Position(1, 1);
}*/
}