...

/

Solution: Number of Spaces Cleaning Robot Cleaned

Solution: Number of Spaces Cleaning Robot Cleaned

Let’s solve the Number of Spaces Cleaning Robot Cleaned problem using the Matrices pattern.

Statement

You are given a 0-indexed 2D binary matrix, room, representing a room of size m×nm \times n. In this matrix, 00 represents an empty space, while 11 represents a space occupied by an object. The top-left corner of the room is always empty in all test cases.

A cleaning robot starts at the top-left corner of the room, facing right. It moves straight until it either reaches the edge of the room or encounters an object. When this happens, it turns 90 degrees clockwise and continues moving. The robot cleans the starting space and every space it visits.

The robot continues moving indefinitely. If it visits a space again while facing the same direction, return the number of unique spaces it has cleaned.

Constraints:

  • m==m == room.length

  • n==n == ...