You are given a chessboard of size 8x8. There are several queens on the board, each at a unique position given by the array queens
, where each element is a list [x, y]
representing a queen's row and column. There is also a king at position king = [x, y]
.
A queen can attack the king if they are on the same row, column, or diagonal, and there are no other queens blocking the path between the queen and the king. Your task is to find all queens that can attack the king and return their positions as a list of lists.
Constraints:
The problem asks us to find all queens that can attack the king directly, considering chess movement rules and possible blocking by other queens. The brute-force way would be to check every queen and see if it can attack the king without being blocked. However, since the board is small, we can optimize by only considering the 8 possible directions (horizontal, vertical, and diagonals) from the king's position and looking for the nearest queen in each direction.
Instead of checking every queen against the king, we can "radiate out" from the king in each direction, step by step, and stop as soon as we find a queen. This ensures we only find queens that are not blocked by others and makes our solution more direct and clear.
Let's break down the solution step by step:
(dx, dy)
.This approach is clear, avoids unnecessary checks, and leverages the chessboard's properties for efficient scanning.
Sample Input:
queens = [[0,1],[1,0],[4,0],[0,4],[3,3],[2,4]]
king = [0,0]
Step-by-step:
[0,0]
(the king).[0,1]
→ found queen, add to result.[1,0]
→ found queen, add to result.[1,1]
, then [2,2]
, etc. No queen found.[[0,1],[1,0]]
Brute-force Approach:
The optimized approach is efficient and leverages the fixed size of the chessboard.
In this problem, we efficiently identify all queens that can attack the king by scanning outward from the king in all eight directions, stopping at the first queen encountered in each. Using a set for fast lookup and simulating movement directionally ensures we only find unblocked queens. The solution is both elegant and practical, making good use of the chessboard's constraints and movement rules.
class Solution:
def queensAttacktheKing(self, queens, king):
queen_set = set(tuple(q) for q in queens)
directions = [(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)]
res = []
for dx, dy in directions:
x, y = king
while 0 <= x+dx < 8 and 0 <= y+dy < 8:
x += dx
y += dy
if (x, y) in queen_set:
res.append([x, y])
break
return res
class Solution {
public:
vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {
set<pair<int, int>> queenSet;
for (auto &q : queens)
queenSet.insert({q[0], q[1]});
vector<vector<int>> res;
vector<pair<int, int>> directions = {
{-1,-1}, {-1,0}, {-1,1},
{0,-1}, {0,1},
{1,-1}, {1,0}, {1,1}
};
for (auto d : directions) {
int x = king[0], y = king[1];
while (x + d.first >= 0 && x + d.first < 8 && y + d.second >= 0 && y + d.second < 8) {
x += d.first;
y += d.second;
if (queenSet.count({x, y})) {
res.push_back({x, y});
break;
}
}
}
return res;
}
};
class Solution {
public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
Set<String> queenSet = new HashSet<>();
for (int[] q : queens) {
queenSet.add(q[0] + "," + q[1]);
}
int[][] directions = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
List<List<Integer>> res = new ArrayList<>();
for (int[] dir : directions) {
int x = king[0], y = king[1];
while (x + dir[0] >= 0 && x + dir[0] < 8 && y + dir[1] >= 0 && y + dir[1] < 8) {
x += dir[0];
y += dir[1];
if (queenSet.contains(x + "," + y)) {
res.add(Arrays.asList(x, y));
break;
}
}
}
return res;
}
}
var queensAttacktheKing = function(queens, king) {
const queenSet = new Set(queens.map(q => q[0] + ',' + q[1]));
const directions = [
[-1,-1], [-1,0], [-1,1],
[0,-1], [0,1],
[1,-1], [1,0], [1,1]
];
const res = [];
for (const [dx, dy] of directions) {
let [x, y] = king;
while (x + dx >= 0 && x + dx < 8 && y + dy >= 0 && y + dy < 8) {
x += dx;
y += dy;
if (queenSet.has(x + ',' + y)) {
res.push([x, y]);
break;
}
}
}
return res;
};