Simulate a robot vacuum that cleans cells, rotating counterclockwise and moving forward or backward, and count how many cells it cleans before stopping.
Medium4SimulationImplementationArrayMatrixInterviewNo attempts yetTime limit2sMemory limit512 MBGiven a robot vacuum cleaner and the state of a room, write a program that counts the cells the robot cleans.
The room is an N×M rectangle divided into 1×1 square cells. Each cell is either a wall or an empty cell. The robot faces one of four directions: east, west, south, or north. Each cell of the room is written as a coordinate (r,c). The westmost cell of the northmost row is (0,0), and the eastmost cell of the southmost row is (N−1,M−1). In other words, (r,c) is the (c+1)-th cell from the west in the (r+1)-th row from the north. Initially, no empty cell has been cleaned.
The robot works as follows.
If the current cell has not been cleaned yet, clean it.
If none of the 4 cells adjacent to the current cell is an uncleaned empty cell:
If at least one of the 4 cells adjacent to the current cell is an uncleaned empty cell:
The first line contains the room size N and M. (3≤N,M≤50)
The second line contains the coordinates (r,c) of the robot's starting cell and its initial facing direction d. The robot faces north if d is 0, east if d is 1, south if d is 2, and west if d is 3.
Each of the next N lines contains M values describing the cells. The j-th value on the i-th of these lines is the state of cell (i,j). A value of 0 means (i,j) is an uncleaned empty cell, and a value of 1 means (i,j) is a wall. Every cell in the northmost, southmost, westmost, or eastmost row or column is a wall. The robot's starting cell is always empty.
Print the number of cells the robot cleans from the moment it starts until it stops.