## CCO '96 P6 - Hoppers

View as PDF

Points: 15
Time limit: 1.0s
Memory limit: 16M

Problem type
##### Canadian Computing Competition: 1996 Stage 2, Day 2, Problem 3

Hoppers are people on a jump stick who can jump from one square to the other, without touching the squares in between (a bit like a knight in chess). They can pick up speed and make bigger hops, but their acceleration per move is limited, and they also have a maximum speed. The game of Hoppers is played on a rectangular grid, where each square on the grid is either empty or occupied. While hoppers can fly over any square, they can only land on empty squares. At any point in time, a hopper has a velocity , where and are the speed (in squares) parallel to the grid. Thus, a speed of corresponds to a knight jump, (as does and other speeds).

To determine the hops a hopper can make, we need to know how much speed a hopper can pick up or lose: either , , or square in either or both directions. Thus, while having speed , the hopper can change to speeds and . It is impossible for the hopper to obtain a velocity of in either direction, so the and component will stay between and inclusive.

The goal of Hoppers is to get from start to finish as quickly as possible (i.e. in the least number of hops), without landing on occupied squares. You are to write a program which, given a rectangular grid, a start point S, and a finish point F, determines the least number of hops in which you can get from S to F. A hopper starts with initial speed and does not care about the speed when arriving at F.

#### Input Specification

The first line contains the number of test cases your program has to process. Each test case consists of a first line containing the width () and height () of the grid. Next is a line containing four integers separated by blanks, of which the first two indicate the start point and the last two indicate the end point , where the start and end point are valid points on the grid (that is, , , , and ). The third line of each test case contains an integer indicating the number of obstacles in the grid. Finally, the test case consists of lines, each specifying an obstacle. Each obstacle consists of four integers: , (), meaning that all squares with and are occupied. Neither the start point nor the finish point will ever be occupied.

#### Output Specification

The string No solution. if there is no way the hopper can reach the finish point from the start point without hopping on an occupied square. Otherwise, the text Optimal solution takes N hops., where is the number of hops needed to get from start to finish point.

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

#### Sample Output

Optimal solution takes 7 hops.
No solution.

#### Explanation Of Sample

The example input can be illustrated as follows. Let # denote an occupied square, S denote the start point, F denote the finish point, and * denote the boundary of the grid. Then the sample input can also be given as follows.

2
5 5
*******
*    F*
* ####*
* ####*
*     *
*    S*
*******
3 3
*****
* #F*
*###*
*S# *
*****