-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1222-QueensThatCanAttackTheKing.java
More file actions
37 lines (31 loc) · 1.27 KB
/
Copy path1222-QueensThatCanAttackTheKing.java
File metadata and controls
37 lines (31 loc) · 1.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
class Solution {
public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
List<List<Integer>> result = new ArrayList<>();
// boolean board 'seen' 을 만들어서 queen들을 다 넣어 놓는다
boolean[][] seen = new boolean[8][8];
for (int[] q : queens) {
seen[q[0]][q[1]] = true;
}
int[][] directions = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 },
{ 1, 1 }, { -1, -1 }, { 1, -1 }, { -1, 1 } };
// king 부터 시작해서 '한 방향씩' 8방향으로 가 보면서 queen이 있으면 result에 넣는다.
for (int[] d : directions) {
int row = king[0] + d[0];
int col = king[1] + d[1];
// seen 보드 안에서만
while (row >= 0 && col >= 0 && row < 8 && col < 8) {
// 만약 queen을 찾았으면 (seen[row][col]이 true면(seen[q[0]][q[1]] = true))
if (seen[row][col]) {
List<Integer> temp = new ArrayList<>();
temp.add(row);
temp.add(col);
result.add(temp);
break;
}
row += d[0];
col += d[1];
}
}
return result;
}
}