#P13149. [GCJ 2018 #3] Field Trip
[GCJ 2018 #3] Field Trip
题目描述
people from an elementary school — one teacher and kids — are on a field trip. They are exploring a grassy field that is an infinite two-dimensional grid of unit cells. Each person is currently occupying one of the cells; there may be multiple people in the same cell.
When it is time to go home, the teacher and kids must all gather in one cell; it does not matter which one, since their bus can pick them up anywhere. The kids have been taught an algorithm that makes it easier to gather:
- The teacher is person number 1, and the kids are numbered 2 through .
- An action taken by a person consists of either moving to one of the 8 cells sharing at least one edge or corner with that person's current cell, or choosing to remain in their current cell.
- When the signal for the end of the field trip sounds, the teacher checks to see if all people are in the same cell. If they are, no further action is necessary. Otherwise, the teacher begins a turn:
- First, the teacher takes an action, as described above. It is up to the teacher to decide where, if anywhere, to move.
- Then, each kid takes an action, starting with kid 2, and so on up to kid ; the -th kid does not take their action until the th person has taken their action. The kids' actions are deterministic: the -th kid must choose the option that minimizes the center-to-center distance from their cell to the cell of the th person. This is never ambiguous; one of the 9 options will uniquely minimize that distance.
- Once the turn is complete, the teacher checks again to see if all people are in the same cell. If they are not, another turn begins, and so on, until everyone is in one cell.
If the teacher makes choices that minimize the number of turns, what is that number of turns?
输入格式
The first line of the input gives the number of test cases, . test cases follow. Each begins with one line with an integer : the number of people on the field trip. Then, there are more lines. The -th of these represents the -th person, and has two integers and : the row and column numbers (on the grid) of the cell that the -th person initially occupies.
输出格式
For each test case, output one line containing Case #x: y, where is the test case number (starting from 1) and is the smallest possible number of turns required, as described above.
5
3
3 2
0 2
0 0
3
2 2
2 2
2 2
9
1 1
0 0
0 1
0 2
1 0
1 2
2 0
2 1
2 2
2
8 0
0 8
4
1 0
1 3
2 2
0 2
Case #1: 2
Case #2: 0
Case #3: 1
Case #4: 4
Case #5: 2
提示
Sample Explanation
In Sample Case #1, the teacher is at — that is, row 3 and column 2. Kid 2 is at , and Kid 3 is at . One optimal strategy for the teacher is as follows:
- Turn 1:
- Move to .
- Kid 2 moves to .
- Kid 3 moves to .
- Turn 2:
- Move to .
- Kid 2 remains in place in .
- Kid 3 moves to . Now everyone is in the same cell.
In Sample Case #2, the teacher and the two kids start off in the same cell, so no turns are required.
In Sample Case #3, the teacher can remain in place, and all of the kids will move to the teacher's cell by the end of the first turn.
In Sample Case #4, the teacher should move diagonally four times to reach .
In Sample Case #5, the teacher should begin by moving to ; then kids , and will all move to . Note that even though all the kids are now in the same cell, the teacher is not, and must start another turn. On the second turn, the teacher can move to to join the kids, and the kids will not move.
Limits
- .
Test set 1 (4 Pts, Visible)
- .
- , for all .
- , for all .
Test set 2 (10 Pts, Hidden)
- .
- , for all .
- , for all .