Codeforces Global Round 12 B. Balls of Steel 曼哈顿距离

You have n distinct points (x1,y1),…,(xn,yn) 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.

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.

More formally, for all balls j such that |xi−xj|+|yi−yj|≤k, we assign xj:=xi and yj:=yi.

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.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.

Input
The first line contains a single integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains two integers n, k (2≤n≤100, 0≤k≤106) — the number of balls and the attract power of all balls, respectively.

The following n lines describe the balls’ coordinates. The i-th of these lines contains two integers xi, yi (0≤xi,yi≤105) — the coordinates of the i-th ball.

It is guaranteed that all points are distinct.

Output
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.

Example
inputCopy
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
outputCopy
-1
1
-1
Note
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.

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.

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.

无脑打暴力,直接起飞了。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <vector>

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>

#define inf 0x3f3f3f3f
#define cha 1e-6
#define ll long long
using namespace std;
const int maxn =1e6+5;
const int maxx=2e5+5;
struct node{
    int x,y;
}a[150];
int n,k;
int vis[200];
void solve(){
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        cin>>n>>k;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i].x>>a[i].y;
        }
        int sign=0;
        for(int i=1;i<=n;i++)
        {
            sign=0;
            for(int j=1;j<=n;j++)
            {
                if(i==j)
                {
                    if(i!=n) continue;
                    else sign=1;
                }
                if(fabs(a[i].x-a[j].x)+fabs(a[i].y-a[j].y)>k)
                {
                    break;
                }
                if(j==n)
                {
                    sign=1;
                }
            }
            if(sign==1)
            {
                break;
            }
        }
        if(sign){
            cout<<1<<endl;
        }
        else{
            cout<<-1<<endl;
        }
    }
}
}
int main()
{
    ios::sync_with_stdio(false);
    solve();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值