HDU

来源:互联网 发布:翻译软件 7.0 编辑:程序博客网 时间:2024/05/16 14:07

How many times

 HDU - 3561 


There are N circles on the plane. Dumbear wants to find a region which is covered by as many circles as possible. But Dumbear is very dumb, so he turns to momodi for help. But momodi is very busy. Can you help him find out the maximum times that a region can be covered? Notice that a point on a circle’s boundary is considered to be covered by this circle.
Input
There are several test cases in the input. 

For each case, the first line contains an integer N (1≤ N ≤ 100) denotes the number of circles. 
For the next N lines, each line contains three integers X, Y, R denotes a circle whose center is (X, Y) (-10000 ≤ X, Y ≤ 10000) and radius is R (0 < R ≤ 5000). 

The input terminates by end of file marker. 
Output
For each test case, output a single line contains only a number denotes the maximum times that a region can be covered.
Sample Input
30 0 11 0 12 0 1
Sample Output
3

思路:找出一个被圆覆盖最多次的点,那么这个点要是圆心,要么就是圆的交点。枚举所有圆两两相交的交点和所有的圆的圆心。

#include<bits/stdc++.h>using namespace std;struct Point{double x,y,r;}node[101];int n;vector<struct Point>p;double dis(const Point& x,const Point& y){return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));}int check(Point x){int sum=0;for(int i=0;i<n;i++){if(dis(x,node[i])<=node[i].r)sum++;}return sum;}int main(){while(cin>>n){p.clear();node[n].x=node[n].y=node[n].r=0;for(int i=0;i<n;i++)cin>>node[i].x>>node[i].y>>node[i].r;for(int i=0;i<n;i++){p.push_back(node[i]);for(int j=i+1;j<n;j++){if(dis(node[i],node[j])>node[i].r+node[j].r)continue;//联立方程组求出两圆的交点 double a=2*node[j].x-2*node[i].x;double b=2*node[j].y-2*node[i].y;double c=dis(node[i],node[n])*dis(node[i],node[n])-dis(node[j],node[n])*dis(node[j],node[n])+node[j].r*node[j].r-node[i].r*node[i].r;double y1,y2,x1,x2;if(b!=0){double A=a*a/(b*b)+1;double B=2*a*node[i].y/b+2*a*c/(b*b)-2*node[i].x;double C=dis(node[i],node[n])*dis(node[i],node[n])+c*c/(b*b)-node[i].r*node[i].r+2*node[i].y*c/b;double D=B*B-4*A*C;x1=(-B-sqrt(D))/(2*A); y1=(a*x1+c)/(-b);x2=(-B+sqrt(D))/(2*A); y2=(a*x2+c)/(-b);}else if(a!=0){double A=b*b/(a*a)+1;double B=b*2*node[i].x/a+2*b*c/(a*a)-2*node[i].y;double C=dis(node[i],node[n])*dis(node[i],node[n])-node[i].r*node[i].r+2*node[i].x*c/a+c*c/(a*a);double D=B*B-4*A*C;y1=(-B-sqrt(D))/(2*A); x1=(c+b*y1)/(-a);y2=(-B+sqrt(D))/(2*A); x2=(c+b*y2)/(-a);}p.push_back((Point){x1,y1,0});p.push_back((Point){x2,y2,0});}}int ans=1;for(int i=0;i<p.size();i++)ans=max(ans,check(p[i]));cout<<ans<<endl;}return 0;}



原创粉丝点击