Under construction

Still being built. Things may change

Teleporters (Small)

Time limit5sMemory limit512 MB

Summary
In 3D L1 space, decide if a sequence of teleporters, each preserving your distance from it, can carry you from one planet to another, and find the fewest hops if so.
Level

Medium7 of 10

Topics
Graph, BFS, Geometry, Math
Solved
No attempts yet

Problem

A short time into the future, in a nearby galaxy, you are the only maker of yarn on the planet Thundera. You want to leave your responsibilities behind for a while and travel to Care-a-Lot, the most relaxing planet there is. The only way to travel is the network of interstellar teleporters.

A teleporter is a small machine floating somewhere in space. You can use it remotely from any point in space, but the conservation of teleportation distance principle means it can only send you to another point whose L1 distance to the teleporter is exactly the L1 distance you were at just before the teleportation. The L1 distance between (x0,y0,z0)(x_0, y_0, z_0) and (x1,y1,z1)(x_1, y_1, z_1) is ∣x0−x1∣+∣y0−y1∣+∣z0−z1∣|x_0 - x_1| + |y_0 - y_1| + |z_0 - z_1|. Your jetpack is broken, so you cannot move on your own and teleporters are your only way to travel. You start at Thundera. You can use one teleporter to go from Thundera to a point p1p_1, then another one to go from p1p_1 to p2p_2, and so on. The last teleportation must land you exactly on Care-a-Lot.

You are given the positions of both planets and of every available teleporter in 3-dimensional space. Decide whether the trip can be made with teleporters alone. If it can, find the minimum number of teleportations needed to reach the destination. Two teleportations count separately even when they use the same teleporter.

Every coordinate in the input is an integer inside a fixed range. The intermediate points you teleport to may have non-integer coordinates, and they are not restricted to that range.

Input

The first line contains the number of test cases, TT. TT test cases follow. Each test case starts with a line containing a single integer NN, the number of available teleporters. Then N+2N + 2 lines follow, each containing three integers XiX_i, YiY_i, and ZiZ_i. The first of these lines gives the coordinates of your home planet Thundera, the second gives the coordinates of the destination planet Care-a-Lot, and each of the remaining NN lines gives the coordinates of one teleporter.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤N≤1001 \le N \le 100
  • −103≤Xi≤103-10^3 \le X_i \le 10^3
  • −103≤Yi≤103-10^3 \le Y_i \le 10^3
  • −103≤Zi≤103-10^3 \le Z_i \le 10^3
  • (Xi,Yi,Zi)≠(Xj,Yj,Zj)(X_i, Y_i, Z_i) \ne (X_j, Y_j, Z_j) for all i≠ji \ne j, so no two of the described objects sit at the same coordinates.

Output

For each test case, print one line containing Case #x: y, where x is the test case number starting from 1, and y is IMPOSSIBLE if you cannot get from Thundera to Care-a-Lot with the available teleporters, or the minimum number of teleportations if you can.

Notes

In the first test case of the sample input, the only teleporter is exactly 3 units away from Thundera, so it can only send you to another point exactly 3 units away from it. From that point it still only reaches points exactly 3 units away from it. Care-a-Lot is 1 unit away from the teleporter, so you can never reach it.

In the second test case, one optimal plan uses the teleporter at (0, 0, 3) to travel to (0, 0, 5), then the teleporter at (0, 0, 0) to travel to (0, 0, -5), then the teleporter at (0, 0, 3) again to travel to (0, 0, 11). The two uses of the teleporter at (0, 0, 3) cover different distances, because you are at a different distance from it each time. They still count as two separate teleportations.

In the third test case, one optimal plan uses the teleporter at (3, 0, 0) to travel to (6, 0, 0), then the teleporter at (6, 1, 0) to travel to (6, 2, 0). There is a teleporter at (6, 0, 0), but standing at the same point as a teleporter does not count as using it.

Examples1

  1. Example 1

    Input
    3
    1
    0 0 0
    0 4 0
    0 3 0
    2
    0 0 1
    0 0 11
    0 0 3
    0 0 0
    3
    0 0 0
    6 2 0
    6 0 0
    3 0 0
    6 1 0
    
    Expected output
    Case #1: IMPOSSIBLE
    Case #2: 3
    Case #3: 2