Hard · Graphs

Robot Room Cleaner

Given a robot that faces up on an open cell of a hidden room grid (1 = open, 0 = wall) and can be controlled only through move(), turnLeft(), turnRight() and clean(), clean every open cell reachable from its start. move() returns false and leaves the robot in place when a wall or the room edge is ahead. The function returns nothing; the judge prints the room with each cleaned cell set to 2.

Examples

Example 1

{
  "room": [
    [1, 1, 1],
    [1, 0, 1],
    [1, 1, 1]
  ],
  "start": [1, 0]
}

Output: 8 cells cleaned

Example 2

{
  "room": [
    [1, 1, 0, 1],
    [1, 0, 0, 1],
    [1, 1, 1, 1]
  ],
  "start": [2, 0]
}

Output: 9 cells cleaned

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Graphs problems