Robot Vacuum
Time limit1sMemory limit256 MB
Find the minimum number of moves for a robot to visit and clean all dirty cells in a grid with furniture, or report -1 if some are unreachable.
- Level
Medium7 of 10
- Topics
- BFS, Shortest path, Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
You want to clean a rectangular room with a robot vacuum, and you can decide the path the robot takes yourself.
The room is divided into square cells, and the robot vacuum is also in size. Each cell is either clean or dirty; when the robot passes through a dirty cell, that cell becomes clean.
Some cells contain a piece of furniture of size , and the robot cannot move onto a cell that holds furniture.
In a single move the robot can go to an orthogonally adjacent cell (up, down, left, or right), and it may pass through the same cell any number of times.
Given the layout of the room, write a program that computes the minimum number of moves needed to make every dirty cell clean.
Input
The input consists of several test cases.
The first line of each test case contains the width and the height of the room (). The next lines describe the room, each line containing characters. Only the following four characters are used:
.: a clean cell*: a dirty cellx: furnitureo: the starting position of the robot vacuum
The number of dirty cells is at most , and there is always exactly one robot vacuum.
The last line of the input contains two zeros separated by a space; this line is not processed.
Output
For each test case, print on its own line the minimum number of moves required to make every dirty cell clean. If any dirty cell cannot be reached, print .