Road
Time limit1sMemory limit1024 MB
Find the shortest path from A to B along the edges of N axis-aligned rectangles.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Geometry, Sorting
- Solved
- No attempts yet
Problem
An island is divided into plots of land, and each plot is an axis-aligned rectangle. A road must be built from point to point . Because no landowner wants the road to split their plot into several smaller pieces, the road may run only along the plot boundaries (the edges of the rectangles).
Write a program that finds the length of the shortest such road from point to point .
Input
The first line contains the number of plots (). Each of the next lines contains the coordinates , , , of the bottom-left and top-right corners of the rectangle that describes one plot. The next line contains the coordinates , of point , and the last line contains the coordinates , of point .
All coordinates are non-negative integers not exceeding . Points and always lie on the boundary of some plot. All plots together form a single island (which may contain lakes), and a road from to is guaranteed to exist.
Output
Output a single integer — the length of the shortest road from point to point . Because all coordinates are integers and the road follows only axis-aligned segments, is always an integer.