Unlocking Blocks
Time limit1sMemory limit128 MB
Given three connected polyomino shapes on a small grid, find the minimum total number of unit slides that makes their bounding boxes pairwise non-overlapping, or -1 if impossible.
- Level
Hard8 of 10
- Topics
- BFS, Simulation, Brute force, Geometry
- Solved
- No attempts yet
Problem
A little-known fact about cows is that they love puzzles! There is a mechanical puzzle made of three solid objects, each built from unit squares glued together. Each object is a "connected" shape: from any square of the object you can reach any other square of the object by stepping north, south, east, or west through squares that belong to it.
An object can be moved by repeatedly sliding it one unit north, south, east, or west. The goal is to separate the objects, so that their bounding boxes (the smallest rectangle enclosing each object) no longer share any positive-area overlap with one another. Given the shapes and locations of the three objects, determine the minimum number of individual one-unit slides required to separate them.
Input
- Line 1: Three space-separated integers , , and , the number of unit squares making up objects 1, 2, and 3 respectively.
- Next lines: the coordinate of the south-west (bottom-left) corner of a square belonging to object 1.
- Next lines: the south-west corner of each square of object 2.
- Next lines: the south-west corner of each square of object 3.
All coordinates lie in the range to , inclusive.
Output
- Line 1: The minimum number of one-unit slides needed to separate the three objects, or if they cannot be separated.
Hint
For example, suppose object 1 is made of squares, object 2 of squares, and object 3 of squares. Sliding object 3 east by one unit, then object 2 north by one unit, then object 1 west by three units leaves the three bounding boxes with no overlap, separating them in slides total.