The Game
InterviewTime limit1sMemory limit128 MB
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 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:
- It consists of straight segments, each one either horizontal or vertical.
- 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 and (), the width and the height of the board. The next lines describe the contents of the board; each of these lines contains exactly 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 (, ), the coordinates of two game pieces. The upper-left corner has coordinates . 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 ; this situation is not processed.
Output
For each board, first output the line Board #n:, where 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 is the number of the pair (restarting from 1 for every board). Follow it by k segments., where 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.