hihocoder1307:几何计算+并查集

hihocoder1301

题解:一堆相交圆放在一个集合里,如果它的高>=h,底<=0,那么就不能穿越

错误代码:我也不知道为什么不能AC??

#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
int const N = 1000 + 10;
double const eps = 1e-8;
double const inf = 0x7f7f7f7f;
int T,n,m,fa[N];
double high[N],low[N],w,h;
bool ans;
struct Circle
{
	double x,y,r;
}c[N];
double Distance(int i,int j){  //求圆心之间的距离 
	return sqrt((c[i].x-c[j].x)*(c[i].x-c[j].x)+(c[i].y-c[j].y)*(c[i].y-c[j].y));
}
bool Judge(int i,int j){    //判断两圆是否相交
	if(Distance(i,j) > c[i].r + c[j].r + eps)	return false;
	else 	return true;
}
int find(int x){
	return x == fa[x] ? x : (fa[x] = find(fa[x]));
}
bool Union(int p,int q){
	if(Judge(p,q)){
		int fp = find(p),	fq = find(q);
		if(fp != fq)
			fa[fp] = fq;
			high[fq] = max(high[fq],high[fp]);
			low[fq] = min(low[fq],low[fp]);
			if(low[fq]<=0 and high[fq]>=h){
				return ans = false;
			}
	}
	return true;
}
int main(){
	ios::sync_with_stdio(false);
	cin>>T;
	while(T--){
		cin>>w>>h>>n;
		ans = true;
		for(int i=1;i<=n;i++){
			fa[i] = i;
			cin>>c[i].x>>c[i].y>>c[i].r;
			high[i] = c[i].y + c[i].r,	low[i] = c[i].y - c[i].r;
		}
		bool flag = true;
		for(int i=1;i<=n&&flag;i++)
			for(int j=i+1;j<=n&&flag;j++)
				Union(i,j),flag = false;
		if(ans)		cout<<"YES"<<endl;
		else 		cout<<"NO"<<endl;
	}
}

正确代码:

#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
int const N = 1000 + 10;
double const eps = 1e-8;
double const inf = 0x7f7f7f7f;
int T,n,m,fa[N];
double high[N],low[N],w,h;
bool ans;
struct Circle
{
	double x,y,r;
}c[N];
double Distance(int i,int j){  //求圆心之间的距离 
	return sqrt((c[i].x-c[j].x)*(c[i].x-c[j].x)+(c[i].y-c[j].y)*(c[i].y-c[j].y));
}
bool Judge(int i,int j){    //判断两圆是否相交
	if(Distance(i,j) > c[i].r + c[j].r + eps)	return false;
	else 	return true;
}
int find(int x){
	return x == fa[x] ? x : (fa[x] = find(fa[x]));
}
bool Union(int p,int q){
	if(Judge(p,q)){
		int fp = find(p),	fq = find(q);
		if(fp != fq)
			fa[fp] = fq;
	}
	return true;
}
int main(){
	ios::sync_with_stdio(false);
	cin>>T;
	while(T--){
		cin>>w>>h>>n;
		ans = true;
		for(int i=1;i<=n;i++){
			fa[i] = i;
			cin>>c[i].x>>c[i].y>>c[i].r;
			high[i] = c[i].y + c[i].r,	low[i] = c[i].y - c[i].r;
		}
		for(int i=1;i<=n;i++)
			for(int j=i+1;j<=n;j++)
				Union(i,j);
		for(int i=1;i<=n;i++){
			int u = find(i);
			high[u] = max(high[u],high[i]);
			low[u] = min(low[u],low[i]);
			if(high[i]>=h and low[i] <=0){
				ans = false;
				break;
			}
		}
		if(ans)		cout<<"YES"<<endl;
		else 		cout<<"NO"<<endl;
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值