A board has numRows rows and numColumns columns. A laser at (row, column) destroys every cell in its row and every cell in its column.
A robot starts at (curRow, curColumn). Its starting cell is protected even if a laser covers that cell. Choose one of four directions—up, down, left, or right—and move in a straight line without turning. The robot may enter a cell only if it is inside the board and is not destroyed. Protection of the starting cell does not protect any destination cell, including the first one.
Return the maximum number of cells the robot can safely enter in one direction. Do not count the starting cell. Return 0 if no safe move is possible.
Input and Output
-
Inputs: positive board dimensions
numRows
and
numColumns
; a valid starting coordinate
curRow
,
curColumn
; and
laserCoordinates
, a collection of laser coordinate pairs on the board.
-
Output: one integer, the greatest safe straight-line distance.
-
Coordinates are one-based: rows are
1
through
numRows
, and columns are
1
through
numColumns
.
Example
numRows = 8
numColumns = 8
curRow = 5
curColumn = 3
laserCoordinates = [[1, 6], [2, 8]]
answer = 3
Moving down enters (6, 3), (7, 3), and (8, 3) safely, then reaches the board edge. Moving up can enter only (4, 3) and (3, 3) before row 2, which is destroyed.