Problem Description
Given an 8x8 chessboard with several queens and one king, find all queens that can directly attack the king. A queen can attack along eight directions (vertical, horizontal, or diagonal) if no other queen is blocking the path.
Key Insights
- Consider the eight directions from the king (vertical, horizontal, and both diagonals).
- For each direction, move step-by-step until you either reach the board edge or find a queen.
- Only the first queen encountered in each direction can attack the king; any subsequent ones are blocked.
- Use a set for quick look-up of queen positions on the board.
Space and Time Complexity
Time Complexity: O(N) where N is the number of queens, since in the worst-case scenario you'll check each queen's presence in at most 8 possible directions. Space Complexity: O(N) due to storing queen positions in a set for constant time look-up.
Solution
The solution involves simulating the movement from the king's position in the eight possible directions. First, store all queen positions in a set for quick look-up. For each of the eight directions (represented as (dx, dy) deltas), start from the king’s location and move step-by-step. If a queen is found at the current position during this traversal, add it to the result list and stop further movement in that direction. Continue until the board boundary is reached for that direction.