call(back)
Algorithms & Data StructuresmediumSign in to star problems

Compute Reachable Cells for a Cleaning Robot

A cleaning robot sits in a grid ('.' open, '#' obstacle). When it moves, it picks a direction (up/down/left/right) and slides until it hits a wall or obstacle — it cannot stop mid-slide. It cleans every cell it passes through.

Return [number of cells the robot can clean, number of cells where it can come to rest]. The start cell counts as both cleaned and a rest cell.

. # .        start (0,0): right is blocked; down, then right, then up...
. . .        cleans 5 cells (all but the '#'), can rest at 4

Hints

Hint 1

With sliding movement the search states are rest positions, not cells — a cell you fly through is cleaned but is not a place you can branch from. BFS over rest positions; each expansion simulates the four slides.

Hint 2

During each slide, add every cell passed through to a cleaned set; enqueue only the slide's endpoint (if new). Plain flood fill is the classic wrong answer here — it ignores the physics.

Asked at