Vacuum Cleaner
Time limit3sMemory limit1024 MB
Given an N by M grid where clearing a row or column resets its dust to zero, find the shortest path between two cells that only crosses cells with dust at most k.
- Level
Hard9 of 10
- Topics
- Graph, Shortest path, Segment tree
- Solved
- No attempts yet
Problem
Paolo works as a janitor in a large storage hall. Two of his tasks are vacuuming and moving objects around the hall. Paolo is fairly pragmatic about cleaning. He waits until there is so much dust that objects can no longer be moved. To make things worse, he recently bought a robot vacuum cleaner and releases it instead of cleaning himself. The robot is broken and cannot turn. It moves in a straight line until it hits a wall and runs out of battery.
The hall is an grid. Rows are numbered 1 to , and columns are numbered 1 to . Every cell starts with 0 units of dust. Then days follow. Each day begins with the dust in every cell increasing by 1. After that, exactly one of three things happens.
- An event of the form means Paolo releases the robot along row , so the dust in every cell of that row becomes 0.
- An event of the form means Paolo releases the robot along column , so the dust in every cell of that column becomes 0.
- An event of the form means Paolo needs to move something from cell to cell . The value is the item's dust tolerance. The item can only be moved across cells with at most units of dust. For example, if or has more than units, the task cannot be completed.
For each event of type 3, output the minimum number of steps Paolo needs to move the item. In one step he moves to a adjacent cell, meaning a cell that shares a side. Cells on the edge have fewer than four neighbors. If the item cannot be moved, output .
Input
The first line contains three integers , , and (, ).
Each of the next lines describes an event. Each line starts with an integer , which is 1, 2, or 3, indicating the type of event. If , the same line contains one more integer (), the row that was chosen. If , the line contains an integer (), the column that was chosen. If , the line contains five more integers , , , , (, , ).
For every event of type 3, the cells and are different. There is at least one event of type 3.
Output
For each event of type 3, print one line with the minimum number of steps needed to move the item. If the item cannot be moved, print instead.