【搜索】C000_LG_奶酪(bfs)

如果两个空洞相切或是相交,则Jerry可以从其中一个空洞跑到另一个空洞,特别地,如果一个空洞与下表面相切或是相交,Jerry则可以从奶酪下表面跑进空洞;如果一个空洞与上表面相切或是相交,Jerry则可以从空洞跑到奶酪上表面。
位于奶酪下表面的Jerry想知道,在不破坏奶酪的情况下,能否利用已有的空洞跑到奶酪的上表面去? 
数据范围
1≤n≤1000,
1≤h,r≤109,
T≤20,
坐标的绝对值不超过109

输入样例:
3 
2 4 1 
0 0 1 
0 0 3 
2 5 1 
0 0 1 
0 0 4 
2 5 2 
0 0 2 
2 0 4
输出样例:
Yes
No
Yes

方法一:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1005;
struct pos{
    ll x,y,z;
} A[N];

double dist(pos& A, pos& B) {
    double x1=A.x,y1=A.y,z1=A.z;
    double x2=B.x,y2=B.y,z2=B.z;
    return sqrt(pow(x1-x2, 2)+pow(y1-y2, 2)+pow(z1-z2, 2));
}
bool bfs(int n, int h, int r) {
    queue<pos> q; 
    bool vis[N]; memset(vis, false, sizeof vis);
    for (int i=0; i<n; i++) if (A[i].z<=r) {
        q.push(A[i]), vis[i]=1;
    }
    while (!q.empty()) {
        pos t=q.front(); q.pop();
        if (t.z+r>=h) return true;
        for (int i=0; i<n; i++)  {
            if (!vis[i] && dist(t, A[i]) <= 2*r) {
                q.push(A[i]);
                vis[i]=1;
            }
        }
    }
    return false;
}
int main() {
    std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t; cin>>t;
    while (t--) {
        ll n,h,r; cin>>n>>h>>r;
        for (int i=0; i<n; i++) cin>>A[i].x>>A[i].y>>A[i].z;
        cout << (bfs(n,h,r) ? "Yes" : "No") << '\n';
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wdt1/p/13643840.html