Lazy Jumping Frog

Time limit3sMemory limit128 MB

Summary
Given a grid with up to 1000 rectangular water regions, find the minimum energy path between two dry cells using a fixed set of twelve weighted jumps.
Level

Medium7 of 10

Topics
Shortest path, Graph, Implementation, Intervals
Solved
No attempts yet

Problem

Mr. Frog lives in a rectangular marsh made up of equally sized square cells; each cell is either dry land or a watery place.

Mr. Frog lives on a dry cell and, as he wanders, can jump only from a dry cell to another dry cell. He wants to visit his girlfriend, Ms. Toad, who also lives on a dry cell in the same marsh. But Mr. Frog is lazy and wants to spend as little energy as possible on his way to Ms. Toad's home.

For any single jump, the cells Mr. Frog can reach and the energy (in calories) he spends are given by the figure below. In the figure, F is Mr. Frog's current cell, and the number written in a cell is the calories spent to jump there in one move. Any cell not shown in the figure cannot be reached in a single jump.

In other words, from his current cell he may jump to any cell whose column offset and row offset are both at most 22 (excluding the cell itself), and the calories spent are:

  • one cell up, down, left, or right (straight distance 11): 22 calories
  • one cell diagonally (11 in each of column and row): 33 calories
  • two cells straight in one direction (horizontally or vertically by 22): 55 calories
  • 22 cells in one direction and 11 in the other (a knight-like move): 66 calories
  • 22 cells in each of column and row (a far diagonal): 77 calories

The target cell of every jump must also be dry. Determine the minimum energy Mr. Frog needs to spend to get from his home to Ms. Toad's home.

Input

The input consists of several test cases.

The first line of a test case contains two integers CC and RR, the number of columns and rows of the marsh (1≤C,R≤10001 \le C, R \le 1000). The second line contains four integers Cf,Rf,Ct,RtC_f, R_f, C_t, R_t, where (Cf,Rf)(C_f, R_f) is Mr. Frog's home and (Ct,Rt)(C_t, R_t) is Ms. Toad's home (1≤Cf,Ct≤C1 \le C_f, C_t \le C and 1≤Rf,Rt≤R1 \le R_f, R_t \le R). The third line contains an integer WW, the number of watery places (0≤W≤10000 \le W \le 1000). Each of the next WW lines contains four integers C1,R1,C2,R2C_1, R_1, C_2, R_2 (1≤C1≤C2≤C1 \le C_1 \le C_2 \le C and 1≤R1≤R2≤R1 \le R_1 \le R_2 \le R), describing a rectangular watery place consisting of all cells whose coordinates (x,y)(x, y) satisfy C1≤x≤C2C_1 \le x \le C_2 and R1≤y≤R2R_1 \le y \le R_2.

The end of input is indicated by C=R=0C = R = 0.

Output

For each test case, print on one line the minimum calories Mr. Frog spends to travel from his home to Ms. Toad's home. If there is no way to reach Ms. Toad's home, print impossible.

Examples4

  1. Example 1

    Input
    4 4
    1 1 4 2
    2
    2 1 3 3
    4 3 4 4
    4 4
    1 1 4 2
    1
    2 1 3 4
    7 6
    4 2 7 6
    5
    4 1 7 1
    5 1 5 5
    2 4 3 4
    7 5 7 5
    6 6 6 6
    0 0
    
    Expected output
    14
    impossible
    12
    
  2. Example 2

    Input
    2 1
    1 1 2 1
    0
    0 0
    
    Expected output
    2
    
  3. Example 3

    Input
    2 2
    1 1 2 2
    0
    0 0
    
    Expected output
    3
    
  4. Example 4

    Input
    5 5
    1 1 5 1
    1
    3 1 3 4
    0 0
    
    Expected output
    9