Ricochet Robots
Time limit1sMemory limit256 MB
Slide up to four robots on a grid with walls and use the fewest moves within the given limit to stop robot 1 on the target cell.
- Level
Medium5 of 10
- Topics
- BFS, Simulation
- Solved
- No attempts yet
Problem
A team of at most four robots delivers parts on a factory floor. The floor is a rectangular grid of cells, and each robot occupies one cell. The robots are numbered from 1 to 4 and move in the four orthogonal directions (left, right, up, down). Once a robot starts moving it slides in a straight line and stops only when the next cell ahead of it is a wall, lies outside the floor, or holds another robot. The robots never move at the same time: exactly one robot moves per step.
Find the shortest move sequence that leaves robot 1 on the target cell. Reaching the target may require moving other robots out of the way, or parking a robot so that robot 1 bounces off it.
The target cell does not stop a robot. Only walls, the edges of the floor, and other robots do, so robot 1 has to finish a move on the target instead of sliding across it.
Given the floor plan, the starting cell of every robot, and the target cell, compute the smallest total number of moves that brings robot 1 to the target.
Input
The first line contains the number of robots , the width and the height of the floor in cells, and an upper bound on the number of moves to search.
Each of the next lines holds one row of the floor as exactly characters:
Wa cell occupied by a wallXthe single target cell1,2,3,4the starting cell of a robot.an empty cell
Output
Print the smallest number of moves that brings robot 1 to the target cell. If no sequence of at most moves does that, print NO SOLUTION.
Constraints
- , ,