Interview questions · Roblox
Find the Longest Safe Straight-Line Movement on a Laser Grid
Find a robot's longest safe straight-line movement on a laser-covered grid, where only the initial cell is protected and laser rows and columns are blocked.
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
numRowsandnumColumns; a valid starting coordinatecurRow,curColumn; andlaserCoordinates, a collection of laser coordinate pairs on the board. - Output: one integer, the greatest safe straight-line distance.
- Coordinates are one-based: rows are
1throughnumRows, and columns are1throughnumColumns.
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.
Community answers
Nothing here yet.
Join in on CSWork →