P3958 奶酪

点击查看代码
#include<bits/stdc++.>
using namespace std;

//使用long long 防溢出
typedef long long ll;

struct Point {
    ll x, y, z;
};

int n;      // 空洞数量
ll h, r;    // 高度,半径
Point holes[1005]; // 存储所有空洞坐标
bool vis[1005];    // 记录是否访问过,防止死循环

//判断两个球是否相交,使用公式即可
bool can_connect(int i, int j) {
    ll dx = holes[i].x - holes[j].x;
    ll dy = holes[i].y - holes[j].y;
    ll dz = holes[i].z - holes[j].z;
    // 距离的平方
    ll dist_sq = dx * dx + dy * dy + dz * dz;
    // 阈值的平方 (2r)^2 = 4*r*r
    return dist_sq <= 4 * r * r;
}

bool bfs() {
    //初始化与启动
    queue<int> q;
    memset(vis, 0, sizeof(vis));

    // 依旧是初始化,把和地面相交的球体放入
    for (int i = 1; i <= n; i++) {
        if (holes[i].z <= r) { 
            q.push(i);
            vis[i] = true;
        }
    }

    //开始循环
    while (!q.empty()) {
        //取出队头
        int u = q.front();
        q.pop();

        //终点判断
        if (holes[u].z + r >= h) { 
            return true;
        }

        //观察邻居
        for (int v = 1; v <= n; v++) {
            // 判断与入队
            if (!vis[v] && can_connect(u, v)) {
                vis[v] = true; // 标记
                q.push(v);     // 入队
            }
        }
    }

    //结束
    return false;
}

void solve() {
    cin >> n >> h >> r;
    for (int i = 1; i <= n; i++) {
        cin >> holes[i].x >> holes[i].y >> holes[i].z;
    }

    if (bfs()) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}

int main() {
    // 读入优化
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
posted @ 2026-01-14 00:15  AnoSky  阅读(10)  评论(0)    收藏  举报