hdu4643 GSM

#include<stdio.h>
#include<math.h>
#define Max 55
#define eps 1e-8
int n,m;
struct Point
{
    double x,y;
}c[Max],b[Max];
double dis(Point p1,Point p2)
{
    return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
}
int equ(Point p)
{
    int i;
    double x;
    double _Min=dis(p,b[0]);
    int k=0;//¼Ç¼baseϱê
    for(i=1;i<m;i++)
    {
        x=dis(p,b[i]);
        if(x<_Min)
        {
            k=i;
            _Min=x;
        }
    }
    return k;
}
int solve(Point c1,Point c2)
{
    Point tmp;
    int tmp1=equ(c1);
    int tmp2=equ(c2);
    if(tmp1==tmp2)return 0;
    if(dis(c1,c2)<eps)return 1;
    tmp.x=(c1.x+c2.x)/2;
    tmp.y=(c1.y+c2.y)/2;
    return solve(c1,tmp)+solve(tmp,c2);
}
int main()
{
    int i,j;
    int q;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0;i<n;i++)
        scanf("%lf %lf",&c[i].x,&c[i].y);
        for(i=0;i<m;i++)
        scanf("%lf %lf",&b[i].x,&b[i].y);
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d %d",&i,&j);
            Point c1=c[i-1];
            Point c2=c[j-1];
            int ans=solve(c1,c2);
            printf("%d
",ans);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/XDJjy/p/3250060.html