This page is still under construction.

Parts of this page are still being built. What you see may change.

The Game

Interview

Time limit1sMemory limit128 MB

Summary
For each pair of pieces on a grid, decide if an orthogonal path can join them without crossing others, and give the minimum number of straight segments.
Level

Medium6 of 10

Topics
BFS, Graph, Matrix, Shortest path
Solved
No attempts yet

Problem

One morning you wake up and think: "I am such a good programmer. Why not make some money?" So you decide to write a computer game.

The game takes place on a rectangular board of w×hw \times h squares. Each square may or may not contain a game piece.

An important part of the game is deciding whether two game pieces can be connected by a path that satisfies both of the following properties:

  1. It consists of straight segments, each one either horizontal or vertical.
  2. It does not cross any other game piece.

The path is allowed to leave the board temporarily.

For example, two pieces can be connected when some orthogonal path between them avoids every other piece, and they cannot be connected when every such path is forced to cross at least one other piece.

The part you have to write now decides whether two given game pieces can be connected under the rules above and, if so, the minimum number of straight segments needed.

Input

The input contains descriptions of several different game situations.

The first line of each description contains two integers ww and hh (1≤w,h≤751 \le w, h \le 75), the width and the height of the board. The next hh lines describe the contents of the board; each of these lines contains exactly ww characters: an X where there is a game piece, and a space where there is none.

Each board description is followed by several lines containing four integers x1,y1,x2,y2x_1, y_1, x_2, y_2 (1≤x1,x2≤w1 \le x_1, x_2 \le w, 1≤y1,y2≤h1 \le y_1, y_2 \le h), the coordinates of two game pieces. The upper-left corner has coordinates (1,1)(1, 1). The two game pieces are always different, and both squares contain a game piece. The list of piece pairs for a board is terminated by a line containing 0 0 0 0.

The whole input is terminated by a game situation with w=h=0w = h = 0; this situation is not processed.

Output

For each board, first output the line Board #n:, where nn is the number of the board (counting from 1). Then output one line for each pair of game pieces of that board. Each such line starts with Pair m: , where mm is the number of the pair (restarting from 1 for every board). Follow it by k segments., where kk is the minimum number of straight segments of a path connecting the two pieces, or by impossible. if the two pieces cannot be connected as described.

Print one blank line between the outputs of two consecutive boards.

Examples4

  1. Example 1

    Input
    5 4
    XXXXX
    X   X
    XXX X
     XXX 
    2 3 5 3
    1 3 4 4
    2 3 3 4
    0 0 0 0
    0 0
    
    Expected output
    Board #1:
    Pair 1: 4 segments.
    Pair 2: 3 segments.
    Pair 3: impossible.
    
  2. Example 2

    Input
    2 1
    XX
    1 1 2 1
    0 0 0 0
    0 0
    
    Expected output
    Board #1:
    Pair 1: 1 segments.
    
  3. Example 3

    Input
    3 3
    XXX
    XXX
    XXX
    1 1 3 3
    0 0 0 0
    0 0
    
    Expected output
    Board #1:
    Pair 1: 4 segments.
    
  4. Example 4

    Input
    2 2
    X 
     X
    1 1 2 2
    0 0 0 0
    0 0
    
    Expected output
    Board #1:
    Pair 1: 2 segments.