建图-找入点和出点-dfs判断出点是否可达
代码求调
#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct hole{
ll x;
ll y;
ll z;
ll r;
};
ll n,h,r,T;
ll dist(hole a,hole b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}
bool in(hole a){
if(a.z-a.r<=0){
return true;
}
return false;
}
bool out(hole a){
if(a.z+a.r>=h){
return true;
}
return false;
}
vector<hole> hs;
vector<ll> mp[1001];
void Init(){
for(ll i=0;i<hs.size();i++){
for(ll j=i;j<hs.size();j++){
if(i==j) continue;
ll d=dist(hs[i],hs[j]);
if(d<0){
d=-d;
}
if(d<=r*2){
//两点可达 添加一条边
mp[i].push_back(j);
mp[j].push_back(i);
}
}
}
}
bool bfs(ll s,ll e){
bool vis[1001]={0};
queue<ll> q;
q.push(s);
vis[s]=true;
while(!q.empty()){
ll u=q.front();
q.pop();
for(ll i=0;i<mp[u].size();i++){
ll v=mp[u][i];
if(vis[v]==false){
vis[v]=true;
q.push(v);
}
}
}
return vis[e];
}
bool solve(ll i){
for(ll j=hs.size();j>=i;j--){
if(out(hs[j])){
if(bfs(i,j)){
return true;
}
}
}
return false;
}
bool cmp(hole a,hole b){
return a.z<b.z;
}
int main(){
freopen("hole.out","w",stdout);
cin>>T;
for(ll i=1;i<=T;i++){
hs.clear();
mp->clear();
cin>>n>>h>>r;
for(ll j=0;j<n;j++){
hole tmp;
tmp.r=r;
cin>>tmp.x>>tmp.y>>tmp.z;
hs.push_back(tmp);
}
sort(hs.begin(),hs.end(),cmp);
Init();
bool ans=false;
for(ll i=0;i<hs.size();i++){
if(in(hs[i])){
if(out(hs[i])){
cout<<"Yes"<<endl;
ans=true;
break;
}
if(solve(i)){
cout<<"Yes"<<endl;
ans=true;
break;
}
}
}
if(!ans){
cout<<"No"<<endl;
}
}
}