C++求大佬修改个80分代码
查看原帖
C++求大佬修改个80分代码
684890
Rhss楼主2022/7/31 23:47
#include <bits/stdc++.h>
#define nx 1100
using namespace std;
int T;
int n,h,r;
int father[nx];
vector<int> v1;
vector<int> v2;
struct node{
	int x;
	int y;
	int z;
};
node s[nx];
int findfather(int x){
	if(x==father[x]){
		return x;
	}return father[x]=findfather(father[x]);
}
void union_1(int a,int b){
	int fa = findfather(a);
	int fb = findfather(b);
	if(fa!=fb){
		father[fa]=b;
	}
}
bool R(int x1,int y1,int z1,int x2,int y2,int z2){
	return sqrt(pow(x1-x2,2)+pow(y1-y2,2)+pow(z1-z2,2))<=2*r;
}
int main(){
	cin>>T;
	for(int i = 1;i<=T;++i){
		cin>>n>>h>>r;
		v1.clear();
		v2.clear();
		for(int j = 1;j<=n;++j){
			father[j]=j;
		}
		for(int j = 1;j<=n;++j){
			cin>>s[j].x>>s[j].y>>s[j].z;
			if(s[j].z-r<=0){
				v1.push_back(j);
			}
			if(s[j].z+r>=h){
				v2.push_back(j);
			}
		}
		for(int j = 1;j<n;++j){
			for(int k = j+1;k<=n;++k){
				//可以互相抵达
				if(R(s[j].x,s[j].y,s[j].z,s[k].x,s[k].y,s[k].z)){
					union_1(j,k);
				}
			}
		}	
		int judge = 0;
		for(int i = 0;i<v1.size();++i){
			for(int j = 0;j<v2.size();++j){
				if(findfather(v1[i])==findfather(v2[i])){
					judge=1;
					break;
				}
			}
			if(judge==1){
				break;
			}
		}
		if(judge){
			cout<<"Yes"<<endl;
		}else{
			cout<<"No"<<endl;
		}
	}
	return 0;
}

2022/7/31 23:47
加载中...