Grid Maze

A maze has been drawn on the graph paper. All walls are either
horizontal or vertical.

[image]

Your task is simple: find the shortest route from the red dot to the
green dot. You can only move horizontally or vertically.

NOTE: We assume that you can move very close to the walls (at distance
0).

Input

The first line contains five numbers: N, X₁, Y₁, X₂, Y₂. Here N
(1 ≤ N ≤ 500) is the number of lines in the maze, (X₁, Y₁) are the
coordinates of the red dot, and (X₂, Y₂) are the coordinates of the
green dot.

Each of the following N lines describe one wall of the maze. A wall is
described as four numbers x₁, y₁, x₂, y₂, where either y₁ = y₂ or
x₁ = x₂.

All coordinates are in range [−2000000000, +2000000000].

Output

Output the length of the shortest route between the two dots. You should
output IMPOSSIBLE if there is no route.

You obtain a route of length 17 by moving very close to the endings of
both inner walls.

Problem information

Author: Eryk Kopczynski

Generation: 2026-01-25T22:14:23.867Z

© Jutge.org, 2006–2026.
https://jutge.org
