-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNQueen.java
More file actions
143 lines (116 loc) · 4.27 KB
/
Copy pathNQueen.java
File metadata and controls
143 lines (116 loc) · 4.27 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package codingInterview.Recursion;
public class NQueen {
//Classical Question
//Place Queen such that No Queen kill each Other
//1. Generate combination
//2.check the combination is valid or not?
//Reactive vs Proactive
//Slow vs fast
static int c = 0;
public static void nQueen(boolean chess[][], int qpsf, String asf, int lpq) {
if (qpsf == chess.length) {
// System.out.println(++c + "." + asf);
if (isBoardValid(chess) == true)
System.out.println(++c + "." + asf);
return;
}
for (int i = 1 + lpq; i < chess.length * chess.length; i++) {
int r = i / chess.length;
int c = i % chess.length;
if (chess[r][c] == false) {
chess[r][c] = true;
nQueen(chess, qpsf + 1, asf + "q" + (qpsf + 1) + "b" + i + " , ", i);
chess[r][c] = false;
}
}
return;
}
public static void nQueenProactive(boolean chess[][], int qpsf, String asf, int lpq) {
if (qpsf == chess.length) {
// System.out.println(++c + "." + asf);
//if (isBoardValid(chess) == true)
System.out.println(++c + "." + asf);
return;
}
for (int i = 1 + lpq; i < chess.length * chess.length; i++) {
int r = i / chess.length;
int c = i % chess.length;
if (chess[r][c] == false) {
if (isQueesSave(chess, r, c) == true) {
chess[r][c] = true;
nQueenProactive(chess, qpsf + 1, asf + "q" + (qpsf + 1) + "b" + i + " , ", i);
chess[r][c] = false;
}
}
}
return;
}
public static void nQueenPSubSequence(boolean chess[][], int box, int qpsf, String asf) {
if (qpsf == chess.length) {
// System.out.println(++c + "." + asf);
//if (isBoardValid(chess) == true)
System.out.println(++c + "." + asf);
return;
}
if (box >= chess.length * chess.length)
return;
nQueenPSubSequence(chess, box + 1, qpsf, asf);
int r = box / chess.length;
int c = box % chess.length;
if (isQueesSave(chess, r, c) == true) {
chess[r][c] = true;
nQueenPSubSequence(chess, box + 1, qpsf + 1, asf + "q" + (qpsf + 1) + "b" + box + " ,");
chess[r][c] = false;
}
return;
}
public static boolean isBoardValid(boolean chess[][]) {
for (int r = 0; r < chess.length; r++) {
for (int c = 0; c < chess[r].length; c++) {
if (chess[r][c] == true) {
if (isQueesSave(chess, r, c) == false)
return false;
}
}
}
return true;
}
public static boolean isQueesSave(boolean chess[][], int r, int c) {
int dirs[][] = {
{0, -1},//North
{1, -1},//NE
{1, 0},//East
{1, 1},//SE
{0, 1},//S
{-1, 1},//SW
{-1, 0},//W
{-1, -1},//NW
};
for (int di = 0; di < dirs.length; di++) {
for (int dist = 1; true; dist++) {
int eqcol = c + dist * dirs[di][0];//col r
int eqrow = r + dist * dirs[di][1];//row c
if (eqcol < 0 || eqrow < 0 || eqcol >= chess[0].length || eqrow >= chess.length)
break;
if (chess[eqrow][eqcol] == true)
return false;
}
}
return true;
}
public static void main(String[] args) {
boolean chess[][] = new boolean[4][4];
//permutation(boxes, 6, 0, "");
long st = System.currentTimeMillis();
//nQueen(chess, 0, "", -1);
long end = System.currentTimeMillis();
//nQueenProactive(chess, 0, "", -1);
long end2 = System.currentTimeMillis();
nQueenPSubSequence(chess, 0, 0, "");
long end3 = System.currentTimeMillis();
System.out.println("1" + (end - st));
System.out.println("2" + (end2 - end));
System.out.println("2" + (end3 - end2));
// coinChangeCombination(new int[]{2, 3, 5, 6}, 10, 0, "");
}
}