1450B.md (2668B)
1 Source: https://codeforces.com/problemset/problem/1450/B 2 3 B. Balls of Steel 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You have $n$ distinct points $(x_1, y_1),\ldots,(x_n,y_n)$ on the plane and a non-negative integer parameter $k$. Each point is a microscopic steel ball and $k$ is the attract power of a ball when it's charged. The attract power is the same for all balls. 22 23 In one operation, you can select a ball $i$ to charge it. Once charged, all balls with Manhattan distance at most $k$ from ball $i$ move to the position of ball $i$. Many balls may have the same coordinate after an operation. 24 25 More formally, for all balls $j$ such that $|x_i - x_j| + |y_i - y_j| \le k$, we assign $x_j:=x_i$ and $y_j:=y_i$. 26 27  An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls. 28 29 Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible. 30 31 Input 32 33 The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. 34 35 The first line of each test case contains two integers $n$, $k$ ($2 \le n \le 100$, $0 \le k \le 10^6$) — the number of balls and the attract power of all balls, respectively. 36 37 The following $n$ lines describe the balls' coordinates. The $i$-th of these lines contains two integers $x_i$, $y_i$ ($0 \le x_i, y_i \le 10^5$) — the coordinates of the $i$-th ball. 38 39 It is guaranteed that all points are distinct. 40 41 Output 42 43 For each test case print a single integer — the minimum number of operations to move all balls to the same position, or $-1$ if it is impossible. 44 45 Example 46 47 Input 48 49 ``` 50 3 51 3 2 52 0 0 53 3 3 54 1 1 55 3 3 56 6 7 57 8 8 58 6 9 59 4 1 60 0 0 61 0 1 62 0 2 63 0 3 64 ``` 65 66 Output 67 68 ``` 69 -1 70 1 71 -1 72 ``` 73 74 Note 75 76 In the first test case, there are three balls at $(0, 0)$, $(3, 3)$, and $(1, 1)$ and the attract power is $2$. It is possible to move two balls together with one operation, but not all three balls together with any number of operations. 77 78 In the second test case, there are three balls at $(6, 7)$, $(8, 8)$, and $(6, 9)$ and the attract power is $3$. If we charge any ball, the other two will move to the same position, so we only require one operation. 79 80 In the third test case, there are four balls at $(0, 0)$, $(0, 1)$, $(0, 2)$, and $(0, 3)$, and the attract power is $1$. We can show that it is impossible to move all balls to the same position with a sequence of operations.