Connection
Time limit1sMemory limit128 MB
Place two vertex-disjoint grid paths on an N x M lattice, one joining A1 to A2 and the other B1 to B2, to minimize the total number of unit segments.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, BFS, Implementation
- Solved
- No attempts yet
Problem
When you connect two points with a wire in an electric circuit, a shorter wire is better.
Four points , , , and are given on an empty circuit board. You want to connect to with one wire and to with another wire.
The board is a grid, and each lattice point has coordinates with and . A wire may run only along the unit vertical or horizontal segments of the grid, and it may never leave the board.
The two wires must not touch each other: they may not share any lattice point and may not cross. (It is fine for the two wires to run side by side one cell apart.)
Write a program that finds the minimum possible total length of the two wires.
Input
The first line contains the board dimensions and , separated by a space. ()
Each of the next four lines contains the coordinates of , , , and , in that order. Each coordinate consists of two integers and with and . All four points are at distinct positions.
Output
Print the minimum possible total length of the two wires connecting - and -. If it is impossible to place the two wires under the given conditions, print IMPOSSIBLE.