Hoppers

Time limit1sMemory limit128 MB

Summary
Find the minimum number of hops from S to F on a grid, where each hop changes the velocity by at most 1 per component and lands only on empty squares.
Level

Medium7 of 10

Topics
BFS, Graph, Simulation, Implementation
Solved
No attempts yet

Problem

Hoppers are people on pogo-style jump sticks who leap from one square to another, flying over the squares in between (much like a knight in chess). They can build up speed to make longer hops, but their acceleration each move is limited and they have a maximum speed.

The game of Hoppers is played on a rectangular grid. Each square is either empty or occupied. A hopper may fly over any square, but may only land on an empty square.

At any moment a hopper has a velocity (x,y)(x, y), where xx and yy are the speeds (in squares) along the two grid axes. For example, a velocity of (2,1)(2, 1) is a knight's move (as are (−2,1)(-2, 1) and its six other reflections).

On each hop the hopper first adjusts its velocity, then moves by the new velocity. Each velocity component may change by −1-1, 00, or +1+1. So from velocity (2,1)(2, 1) the hopper can switch to any of (1,0)(1,0), (1,1)(1,1), (1,2)(1,2), (2,0)(2,0), (2,1)(2,1), (2,2)(2,2), (3,0)(3,0), (3,1)(3,1), (3,2)(3,2). Neither component can ever reach a magnitude of 44, so each component always stays in the range −3-3 to 33 inclusive.

The goal is to travel from a start square SS to a finish square FF in as few hops as possible, never landing on an occupied square. A hopper begins at rest with velocity (0,0)(0, 0), and its velocity upon arriving at FF does not matter. Squares the hopper flies over may be occupied; only the squares it lands on must be empty.

Write a program that, given the grid, the start square, and the finish square, reports the minimum number of hops needed to get from SS to FF.

Input

The first line contains the number of test cases NN.

Each test case has the following form:

  • A line with the grid width XX (1≤X≤161 \le X \le 16) and height YY (1≤Y≤161 \le Y \le 16).
  • A line with four integers x1x_1 y1y_1 x2x_2 y2y_2: the start square (x1,y1)(x_1, y_1) and the finish square (x2,y2)(x_2, y_2), both valid grid squares (0≤x1,x2<X0 \le x_1, x_2 < X and 0≤y1,y2<Y0 \le y_1, y_2 < Y).
  • A line with an integer PP, the number of obstacle rectangles.
  • PP lines, each describing one obstacle as four integers x1x_1 x2x_2 y1y_1 y2y_2 (0≤x1≤x2<X0 \le x_1 \le x_2 < X, 0≤y1≤y2<Y0 \le y_1 \le y_2 < Y). Every square (x,y)(x, y) with x1≤x≤x2x_1 \le x \le x_2 and y1≤y≤y2y_1 \le y \le y_2 is occupied.

The start and finish squares are never occupied.

Output

For each test case, print one line.

If the hopper cannot reach the finish square from the start square without landing on an occupied square, print:

No solution.

Otherwise print:

Optimal solution takes N hops.

where N is the minimum number of hops required.

Examples3

  1. Example 1

    Input
    2
    5 5
    4 0 4 4
    1
    1 4 2 3
    3 3
    0 0 2 2
    2
    1 1 0 2
    0 2 1 1
    
    Expected output
    Optimal solution takes 7 hops.
    No solution.
    
  2. Example 2

    Input
    1
    1 1
    0 0 0 0
    0
    
    Expected output
    Optimal solution takes 0 hops.
    
  3. Example 3

    Input
    1
    2 2
    0 0 1 0
    0
    
    Expected output
    Optimal solution takes 1 hops.