-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathQueensThatCanAttackKing.java
163 lines (143 loc) · 5.19 KB
/
QueensThatCanAttackKing.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*https://leetcode.com/problems/queens-that-can-attack-the-king/*/
class Solution {
int B = 8;
public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
HashSet<Integer>[] rows = new HashSet[B], cols = new HashSet[B];
for (int i = 0; i < B; ++i)
{
rows[i] = new HashSet<Integer>();
cols[i] = new HashSet<Integer>();
}
for (int[] queen : queens)
{
rows[queen[0]].add(queen[1]);
cols[queen[1]].add(queen[0]);
}
List<List<Integer>> result = new ArrayList<List<Integer>>();
int min = Integer.MIN_VALUE, max = Integer.MAX_VALUE;
//same row as the king
for (Integer col : rows[king[0]])
{
if (col < max && col > king[1])
max = col;
if (col > min && col < king[1])
min = col;
}
if (max != Integer.MAX_VALUE) result.add(Arrays.asList(king[0],max));
if (min != Integer.MIN_VALUE) result.add(Arrays.asList(king[0],min));
// System.out.println(result);
min = Integer.MIN_VALUE; max = Integer.MAX_VALUE;
//same column as the king
for (Integer row : cols[king[1]])
{
if (row < max && row > king[0])
max = row;
if (row > min && row < king[0])
min = row;
}
if (max != Integer.MAX_VALUE) result.add(Arrays.asList(max,king[1]));
if (min != Integer.MIN_VALUE) result.add(Arrays.asList(min,king[1]));
// System.out.println(result);
//principal diagonal
int minIndex = -1, maxIndex = -1;
min = Integer.MIN_VALUE; max = Integer.MAX_VALUE;
for (int i = 0; i < king[0]; ++i)
{
int temp = king[1]-king[0]+i;
if (rows[i].contains(temp))
{
if (temp > min)
{
min = temp;
minIndex = i;
}
}
}
for (int i = king[0]+1; i < B; ++i)
{
int temp = king[1]-king[0]+i;
if (rows[i].contains(temp))
{
if (temp < max)
{
max = temp;
maxIndex = i;
}
}
}
if (max != Integer.MAX_VALUE) result.add(Arrays.asList(maxIndex,max));
if (min != Integer.MIN_VALUE) result.add(Arrays.asList(minIndex,min));
// System.out.println(result);
//other diagonal
minIndex = -1; maxIndex = -1;
min = Integer.MIN_VALUE; max = Integer.MAX_VALUE;
for (int i = 0; i < king[0]; ++i)
{
int temp = king[1]+king[0]-i;
if (rows[i].contains(temp))
{
if (temp < max)
{
max = temp;
maxIndex = i;
}
}
}
for (int i = king[0]+1; i < B; ++i)
{
int temp = king[1]+king[0]-i;
if (rows[i].contains(temp))
{
if (temp > min)
{
min = temp;
minIndex = i;
}
}
}
if (max != Integer.MAX_VALUE) result.add(Arrays.asList(maxIndex,max));
if (min != Integer.MIN_VALUE) result.add(Arrays.asList(minIndex,min));
// System.out.println(result);
return result;
}
}
class Solution {
int B = 8;
private static final int[][] DIRECTIONS = {{0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,-1},{-1,1}};
public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
// create 8-directions.
// keep a visited boolean array with queens.
// keep a resultList with queens that can attack the quin.
List<List<Integer>> queensInAttackPosition = new ArrayList<>();
boolean[][] visitedQueens = new boolean[8][8];
for (int[] queen : queens) {
visitedQueens[queen[0]][queen[1]] = true;
}
// start at kings position.
// for every possible direction (8).
for (int[] dir : DIRECTIONS) {
// go in the same direction until find a queen or hit out of bound.
int row = king[0] + dir[0];
int col = king[1] + dir[1];
while (!isOutOfBound(row, col)) {
// if a queen found in this direction added and stop searching.
if (visitedQueens[row][col]) {
queensInAttackPosition.add(Arrays.asList(row, col));
break;
}
// continue down in the same direction.
row = row + dir[0];
col = col + dir[1];
}
}
// return the list with the queens.
return queensInAttackPosition;
}
// provides whether is this cell is out of bound.
private boolean isOutOfBound(int row, int col) {
return row < 0 || row >= B || col < 0 || col >= B;
}
}
// king can be attacked at most -> 8 sides.
// 4 - directional.
// 4 - diagonal.