On a 0-indexed8 x 8 chessboard, there can be multiple black queens and one white king.
You are given a 2D integer array queens where queens[i] = [xQueeni, yQueeni] represents the position of the ith black queen on the chessboard.
You are also given an integer array king of length 2 where king = [xKing, yKing] represents the position of the white king.
Return the coordinates of the black queens that can directly attack the king. You may return the answer in any order.

Input: queens =[[0,1],[1,0],[4,0],[0,4],[3,3],[2,4]], king =[0,0]Output: [[0,1],[1,0],[3,3]]Explanation: The diagram above shows the three queens that can directly attack the king and the three queens that cannot attack the king(i.e., marked with red dashes).

Input: queens =[[0,0],[1,1],[2,2],[3,4],[3,5],[4,4],[4,5]], king =[3,3]Output: [[2,2],[3,4],[4,4]]Explanation: The diagram above shows the three queens that can directly attack the king and the three queens that cannot attack the king(i.e., marked with red dashes).
For each of the 8 directions a queen can attack, move step by step from the king’s position and stop at the first queen encountered. This ensures only the closest queen in each direction is considered.
classSolution {
public List<List<Integer>>queensAttacktheKing(int[][] queens, int[] king) {
Set<String> s =new HashSet<>();
for (int[] q : queens) s.add(q[0]+","+ q[1]);
int[][] dirs = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
List<List<Integer>> ans =new ArrayList<>();
for (int[] d : dirs) {
int x = king[0], y = king[1];
while (x + d[0]>= 0 && x + d[0]< 8 && y + d[1]>= 0 && y + d[1]< 8) {
x += d[0]; y += d[1];
if (s.contains(x +","+ y)) { ans.add(Arrays.asList(x, y)); break; }
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funqueensAttacktheKing(queens: Array<IntArray>, king: IntArray): List<List<Int>> {
val s = queens.map { it[0] to it[1] }.toSet()
val dirs = listOf(-1 to -1, -1 to 0, -1 to 1, 0 to -1, 0 to 1, 1 to -1, 1 to 0, 1 to 1)
val ans = mutableListOf<List<Int>>()
for ((dx, dy) in dirs) {
var x = king[0]; var y = king[1]
while (x+dx in0..7&& y+dy in0..7) {
x += dx; y += dy
if (x to y in s) { ans.add(listOf(x, y)); break }
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
from typing import List
classSolution:
defqueensAttacktheKing(self, queens: List[List[int]], king: List[int]) -> List[List[int]]:
s = set(map(tuple, queens))
ans = []
for dx, dy in [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]:
x, y = king
while0<= x+dx <8and0<= y+dy <8:
x += dx; y += dy
if (x, y) in s:
ans.append([x, y])
breakreturn ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnqueens_attackthe_king(queens: Vec<Vec<i32>>, king: Vec<i32>) -> Vec<Vec<i32>> {
let s: std::collections::HashSet<(i32, i32)>= queens.iter().map(|q| (q[0], q[1])).collect();
let dirs = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)];
letmut ans =vec![];
for (dx, dy) in dirs {
let (mut x, mut y) = (king[0], king[1]);
while0<= x+dx && x+dx <8&&0<= y+dy && y+dy <8 {
x += dx; y += dy;
if s.contains(&(x, y)) { ans.push(vec![x, y]); break; }
}
}
ans
}
}