← 返回 pinterest 的题目列表Robot Straight-Line Reachable Cells on a 2D Grid with Blockers (8 Directions)
类型:online_judge
Problem
You are given a 2D grid (like a chessboard) with dimensions width × height, using coordinates:
0 ≤ x < width
0 ≤ y < height
A robot starts at (x, y).
The robot may move in any of 8 directions (N, S, E, W, NE, NW, SE, SW), but once it chooses a direction it must move in a straight line without turning.
Some cells are blocked by obstacles given as a list blockers of coordinates. When moving in a direction, the robot:
must stay within the grid;
cannot pass through a blocker;
if it encounters a blocker in that direction, all cells beyond it in that direction are unreachable;
can reach every cell from the start up to the boundary or the cell right before the first blocker.
Return a list of all reachable coordinates (x', y') across all 8 directions (order does not matter).
Input
width: integer
height: integer
x, y: integers, start coordinate
blockers: list of (bx, by) coordinates
Output
list (or set) of all reachable coordinates
Constraints
1 ≤ width, height ≤ 1e5
0 ≤ x < width, 0 ≤ y < height
0 ≤ |blockers| ≤ 2e5
blocker coordinates are within the grid and do not include (x, y)
Example
Input:
width = 7, height = 4
start (x, y) = (3, 2)
blockers = [(2, 1)]
Output: all cells reachable from (3,2) along the 8 straight-line directions until the boundary or the first blocker (exclusive).
Example
Input
7 4
3 2
1
2 1
Output
(order may vary)
(4,2) (5,2) (6,2)
(2,2) (1,2) (0,2)
(3,3)
(3,1) (3,0)
(4,3)
(4,1) (5,0)
(2,3)
(2,1 is blocked so not included)
(1,0)