Problem · Matrix
Minimum Cleaning Robot Runs
Learn this problemProblem statement
A building plan is represented by a rectangular array of strings. Each field contains one of three symbols:
.is clean floor.*is dirty floor.#is a wall.
A room is a maximal group of non-wall fields connected in the four cardinal directions. In one run, a cleaning robot starts in one room and cleans every dirty field in that room. It cannot cross a wall or leave the plan.
Return the minimum number of robot runs needed to clean every dirty field.
Function
solution(plan: String[]) → intExamples
Example 1
plan = ["..#..",".*#*.","..#.."]return = 2The vertical wall separates the floor into two rooms. Each room contains a dirty field, so the robot needs one run in each room.
Example 2
plan = ["....",".##.",".*#.","...."]return = 1All non-wall fields are connected around the wall block. The single dirty field is therefore cleaned in one run.
Example 3
plan = ["###","#.#","###"]return = 0The only floor field is already clean, so no robot run is required.
Constraints
1 <= plan.lengthand every row has the same positive length.plan.length * plan[0].length <= 200000.- Every character is
.,*, or#.