POJ 1418 Viva Confetti

Description

Do you know confetti? They are small discs of colored paper, and people throw them around during parties or festivals. Since people throw lots of confetti, they may end up stacked one on another, so there may be hidden ones underneath. 

A handful of various sized confetti have been dropped on a table. Given their positions and sizes, can you tell us how many of them you can see? 

The following figure represents the disc configuration for the first sample input, where the bottom disc is still visible. 

POJ 1418 Viva Confetti_第1张图片

Input

The input is composed of a number of configurations of the following form. 


x1 y1 r1 
x2 y2 r2 
... 
xn yn rn 

The first line in a configuration is the number of discs in the configuration (a positive integer not more than 100), followed by one line descriptions of each disc : coordinates of its center and radius, expressed as real numbers in decimal notation, with up to 12 digits after the decimal point. The imprecision margin is +/- 5 x 10^(-13). That is, it is guaranteed that variations of less than +/- 5 x 10^(-13) on input values do not change which discs are visible. Coordinates of all points contained in discs are between -10 and 10. 

Confetti are listed in their stacking order, x1 y1 r1 being the bottom one and xn yn rn the top one. You are observing from the top. 

The end of the input is marked by a zero on a single line. 

Output

For each configuration you should output the number of visible confetti on a single line.

Sample Input

3
0 0 0.5
-0.9 0 1.00000000001
0.9 0 1.00000000001
5
0 1 0.5
1 1 1.00000000001
0 2 1.00000000001
-1 1 1.00000000001
0 -0.00001 1.00000000001
5
0 1 0.5
1 1 1.00000000001
0 2 1.00000000001
-1 1 1.00000000001
0 0 1.00000000001
2
0 0 1.0000001
0 0 1
2
0 0 1
0.00000001 0 1
0

Sample Output

3
5
4
2
2
 
   
 
   
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <algorithm>
using namespace std;
#define PI acos(-1)
#define eps 5e-13
int N;
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
};
struct Circle
{
    Point c;
    double r;
    Circle(){}
    Circle(Point c,double r):c(c),r(r){}
    Point point(double a)
    {
        return Point(c.x+r*cos(a),c.y+r*sin(a));
    }
}Cir[110];


int dcmp(double x)
{
    if(fabs(x)<eps)return 0;
    else return x<0?-1:1;
}
Point operator+(Point A,Point B){   return Point(A.x+B.x,A.y+B.y);  }
Point operator-(Point A,Point B){   return Point(A.x-B.x,A.y-B.y);  }
Point operator*(Point A,double p){  return Point(A.x*p,A.y*p);      }
Point operator/(Point A,double p){  return Point(A.x/p,A.y/p);   }
bool operator<(const Point&a,const Point&b){  return a.x<b.x||(a.x==b.x&&a.y<b.y);  }
bool operator == (const Point&a,const Point&b){  return dcmp(a.x-b.x)==0&&dcmp(a.x-b.x)==0;  }
double Dot(Point A,Point B){return A.x*B.x+A.y*B.y;}
double Cross(Point A,Point B){return A.x*B.y-A.y*B.x;}
double Length(Point A){return sqrt(Dot(A,A));}
double angle(Point A){return atan2(A.y,A.x);}
int  getCircleCircleIntersection(Circle C1,Circle C2,vector<double> &sol)
{
    double d=Length(C1.c-C2.c);
    if(dcmp(d)==0)
    {
        if(dcmp(C1.r-C2.r)==0)return -1;
        else return 0;
    }
    if(dcmp(C1.r+C2.r-d)<0)return 0;
    if(dcmp(fabs(C1.r-C2.r)-d)>0)return 0;
    double a=angle(C2.c-C1.c);
    double da=acos((C1.r*C1.r+d*d-C2.r*C2.r)/(2*C1.r*d));
    sol.push_back(a-da);
    if(dcmp(da))
    {
        sol.push_back(a+da);
        return 2;
    }
    else return 1;
}
int topmost(Point p)
{
    int ans=-1;
    for(int i=N-1;i>=0;i--)
        if(Length(p-Cir[i].c)<Cir[i].r)
        {
           ans=i;
           break;
        }
    return ans;
}
int main()
{
    while(cin>>N&&N!=0)
    {
        for(int i=0;i<N;i++)
            cin>>Cir[i].c.x>>Cir[i].c.y>>Cir[i].r;
        set<int> ret;
        for(int i=0;i<N;i++)
        {
            vector<double> sol;
            for(int j=0;j<N;j++)
               getCircleCircleIntersection(Cir[i],Cir[j],sol);
            sol.push_back(0);
            sol.push_back(2.0*PI);
            sort(sol.begin(),sol.end());
            int size=sol.size();
            for(int j=0;j<size;j++)
            {
                double mid=(sol[j]+sol[j+1])/2;
                for(int d=-1;d<=1;d=d+2)
                {
                    double r2=Cir[i].r+eps*d;
                    Point p=Point(Cir[i].c.x+r2*cos(mid),Cir[i].c.y+r2*sin(mid));
                    int t=topmost(p);
                    if(t>=0)ret.insert(t);
                }
            }
        }
        cout<<ret.size()<<endl;
    }
    return 0;
}

 
   
 
   
 
   
 
   
 
   
 
  

你可能感兴趣的:(POJ 1418 Viva Confetti)