Grid Maze X75212


Statement
 

pdf   zip

html

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

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, X1, Y1, X2, Y2. Here N (1 ≤ N ≤ 500) is the number of lines in the maze, (X1, Y1) are the coordinates of the red dot, and (X2, Y2) 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 x1, y1, x2, y2, where either y1 = y2 or x1 = x2.

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.

Public test cases
  • Input

    6 1 6 9 7
    0 0 10 0
    0 0 0 10
    10 0 10 10
    0 10 10 10
    2 3 2 10
    8 0 8 8
    

    Output

    17
    
  • Information
    Author
    Eryk Kopczynski
    Language
    English
    Official solutions
    Unknown. This problem is being checked.
    User solutions
    C++