Robot Vacuum
InterviewTime limit1sMemory limit1024 MB
A robot slides in each commanded direction until a box stops it; count the distinct grid cells it visits, including the start.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation, Array, Brute force
- Solved
- No attempts yet
Problem
A robot vacuum cleans a grid-shaped warehouse where heavy boxes sit on some cells. The vacuum follows a sequence of commands: up ("^"), right (">"), down ("v"), left ("<"). When the robot receives a command, it moves as far as it can in that direction until a box blocks it. Every cell the robot vacuum occupies at any point is cleaned, including the cell it starts on. Given the layout of the warehouse, the robot's starting position, and a sequence of commands, determine how many distinct cells will have been cleaned when the sequence ends.
Input
- The first line contains three integers: () and (), the number of rows and columns of the grid-shaped warehouse, and (), the length of the command sequence.
- The second line contains a string of length consisting of "
^", ">", "v", "<", the command sequence sent to the robot. - The following lines describe the grid-shaped warehouse. The -th of these lines contains characters describing the -th row. Each character is either a period "
." if a cell is empty, a square "#" if the cell contains a box, or "O" if the cell is the robot's starting position. Exactly one cell is guaranteed to contain "O". In addition, every cell on the edge of the grid is guaranteed to be "#".
Output
Print a single integer: the number of distinct cells cleaned by the robot.