This page is still under construction.

Parts of this page are still being built. What you see may change.

Unlocking Blocks

Time limit1sMemory limit128 MB

Summary
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 1×11 \times 1 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 N1N_1, N2N_2, and N3N_3, the number of unit squares making up objects 1, 2, and 3 respectively.
  • Next N1N_1 lines: the (x,y)(x, y) coordinate of the south-west (bottom-left) corner of a square belonging to object 1.
  • Next N2N_2 lines: the south-west corner of each square of object 2.
  • Next N3N_3 lines: the south-west corner of each square of object 3.

All coordinates lie in the range 00 to 99, inclusive.

Output

  • Line 1: The minimum number of one-unit slides needed to separate the three objects, or −1-1 if they cannot be separated.

Hint

For example, suppose object 1 is made of 1212 squares, object 2 of 33 squares, and object 3 of 55 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 55 slides total.

Examples1

  1. Example 1

    Input
    12 3 5
    0 0
    1 0
    2 0
    3 0
    3 1
    0 1
    0 2
    0 3
    0 4
    1 4
    2 4
    3 4
    2 1
    2 2
    1 2
    2 3
    3 3
    4 3
    4 4
    4 2
    
    Expected output
    5