-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueensAttackII.java
More file actions
163 lines (143 loc) · 6.17 KB
/
QueensAttackII.java
File metadata and controls
163 lines (143 loc) · 6.17 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
static int queensAttack(int n, int k, int queenRow, int queenCol, int[][] obstacles) {
if(n <= 1) return 0;
//obstacle coordinates. we will store closest obstacle only
//initially all values are initialized to max i.e. edge of board + 1
int topRow = n + 1, topCol = queenCol;
int leftRow = queenRow, leftCol = 0;
int rightRow = queenRow, rightCol = n + 1;
int bottomRow = 0, bottomCol = queenCol;
int leftDistance = queenCol - 1,
rightDistance = n - queenCol,
topDistance = n - queenRow,
bottomDistance = queenRow - 1;
int topLeftRow, topLeftCol;
if(leftDistance <= topDistance) {
topLeftRow = queenRow + leftDistance + 1;
topLeftCol = (queenCol - leftDistance) - 1;
}
else {
topLeftRow = queenRow + topDistance + 1;
topLeftCol = (queenCol - topDistance) - 1;
}
int topRightRow, topRightCol;
if(rightDistance <= topDistance) {
topRightRow = queenRow + rightDistance + 1;
topRightCol = queenCol + rightDistance + 1;
}
else {
topRightRow = queenRow + topDistance + 1;
topRightCol = queenCol + topDistance + 1;
}
int bottomLeftRow, bottomLeftCol;
if(bottomDistance <= leftDistance) {
bottomLeftRow = (queenRow - bottomDistance) - 1;
bottomLeftCol = (queenCol - bottomDistance) - 1;
}
else {
bottomLeftRow = (queenRow - leftDistance) - 1;
bottomLeftCol = (queenCol - leftDistance) - 1;
}
int bottomRightRow, bottomRightCol;
if(bottomDistance <= rightDistance) {
bottomRightRow = (queenRow - bottomDistance) - 1;
bottomRightCol = queenCol + bottomDistance + 1;
}
else {
bottomRightRow = (queenRow - rightDistance) - 1;
bottomRightCol = queenCol + rightDistance + 1;
}
//now we have our limits set
//next we need to loop through all obstacles
//check if obstacle is lined up with queen
//if it is then compare with saved coords
//if its distance is less than saved, update saved
//at the end we will have closest obstacles coordinates
//and if no obstacle then maximum values (edge of board)
for (int i = 0; i < k; i++) {
int obstacleRow = obstacles[i][0];
int obstacleCol = obstacles[i][1];
//top left
if(obstacleRow > queenRow && (queenRow + queenCol) == (obstacleRow + obstacleCol)) {
int obstacleDistance = obstacleRow - queenRow;
int savedDistance = topLeftRow - queenRow;
if(obstacleDistance < savedDistance) {
topLeftRow = obstacleRow;
topLeftCol = obstacleCol;
}
}
//top right
else if(obstacleRow > queenRow && (queenRow - queenCol) == (obstacleRow - obstacleCol)) {
int obstacleDistance = obstacleCol - queenCol;
int savedDistance = topRightCol - queenCol;
if(obstacleDistance < savedDistance) {
topRightRow = obstacleRow;
topRightCol = obstacleCol;
}
}
//bottom left
else if(obstacleRow < queenRow && (queenRow - queenCol) == (obstacleRow - obstacleCol)) {
int obstacleDistance = queenCol - obstacleCol;
int savedDistance = queenCol - bottomLeftCol;
if(obstacleDistance < savedDistance) {
bottomLeftRow = obstacleRow;
bottomLeftCol = obstacleCol;
}
}
//bottom right
else if(obstacleRow < queenRow && (queenRow + queenCol) == (obstacleRow + obstacleCol)) {
int obstacleDistance = obstacleCol - queenCol;
int savedDistance = bottomRightCol - queenCol;
if(obstacleDistance < savedDistance) {
bottomRightRow = obstacleRow;
bottomRightCol = obstacleCol;
}
}
//top
else if(obstacleRow > queenRow && obstacleCol == queenCol) {
int obstacleDistance = obstacleRow - queenRow;
int savedDistance = topRow - queenRow;
if(obstacleDistance < savedDistance) {
topRow = obstacleRow;
topCol = obstacleCol;
}
}
//bottom
else if(obstacleRow < queenRow && obstacleCol == queenCol) {
int obstacleDistance = queenRow - obstacleRow;
int savedDistance = queenRow - bottomRow;
if(obstacleDistance < savedDistance) {
bottomRow = obstacleRow;
bottomCol = obstacleCol;
}
}
//left
else if(obstacleRow == queenRow && obstacleCol < queenCol) {
int obstacleDistance = queenCol - obstacleCol;
int savedDistance = queenCol - leftCol;
if(obstacleDistance < savedDistance) {
leftRow = obstacleRow;
leftCol = obstacleCol;
}
}
//right
else if(obstacleRow == queenRow && obstacleCol > queenCol) {
int obstacleDistance = obstacleCol - queenCol;
int savedDistance = rightCol - queenCol;
if(obstacleDistance < savedDistance) {
rightRow = obstacleRow;
rightCol = obstacleCol;
}
}
}
int count = 0;
count += topLeftRow - queenRow; //top left
count += topRightRow - queenRow; //top right
count += topRow - queenRow; //top
count += queenCol - leftCol; //left
count += rightCol - queenCol; //right
count += bottomRightCol - queenCol; //bottom right
count += queenCol - bottomLeftCol; //bottomLeft
count += queenRow - bottomRow; //bottom
count -= 8;
return count;
}