#P7473. [NOI Online 2021 入门组] 重力球
[NOI Online 2021 入门组] 重力球
Problem Description
The "Gravity Ball" game is played on an square area. Denote the cell in the -th row from top to bottom and the -th column from left to right as .
There are obstacles in the square area. The -th obstacle occupies position . In addition, everything outside the boundary of the square area is considered an obstacle.
Now there are two balls located at and . In the game, you can perform the following operation:
- Choose one direction from up, down, left, and right, and "switch" the direction of gravity to that direction. Then the two balls will move simultaneously in that direction until they hit an obstacle.
You need to use the minimum number of operations to make the two balls reach the same position.
There are rounds of the game. In each round, only the initial positions of the balls are different, while the obstacle positions remain unchanged. You need to find the minimum number of operations for each round, or report that there is no solution.
Input Format
The first line contains three integers , representing the size of the square area, the number of obstacles, and the number of game rounds.
The next lines each contain two integers , indicating that there is an obstacle at position . The data guarantees that all obstacle positions are distinct.
The next lines each contain four integers , indicating the initial positions of the two balls in a round. It is guaranteed that the initial positions do not contain obstacles.
Output Format
Output lines in total. On the -th line, output one integer representing the minimum number of operations needed for the -th round. If there is no solution, output -1.
4 4 3
2 2
2 4
3 2
4 4
1 3 4 3
2 1 2 1
1 2 3 4
1
0
3
Hint
Sample Explanation
In this sample, the obstacles are distributed as the red crosses in the figure.
In the first query, you only need to change gravity upward (or downward) to make the two balls arrive at the same time.
In the second query, the two balls are already at the same position, so no operation is needed.
In the third query, change the direction of gravity times, in order: left, down, left. The movement paths of the balls are shown as the pink, orange, and brown lines in the figure:

Constraints and Notes
For of the testdata: .
For of the testdata: .
For the other of the testdata: .
For of the testdata: , , .
The testdata is provided by SSerxhs.
The testdata references the construction idea from Problem A of the 2020 ICPC Regional Contest (Nanjing), "Xiaomiaomiao Dislikes Computational Geometry", and we would like to express our thanks here.
Translated by ChatGPT 5