hdu1077 catching fish

Problem Description
Ignatius likes catching fish very much. He has a fishnet whose shape is a circle of radius one. Now he is about to use his fishnet to catch fish. All the fish are in the lake, and we assume all the fish will not move when Ignatius catching them. Now Ignatius wants to know how many fish he can catch by using his fishnet once. We assume that the fish can be regard as a point. So now the problem is how many points can be enclosed by a circle of radius one.

Note: If a fish is just on the border of the fishnet, it is also caught by Ignatius.

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case starts with a positive integer N(1<=N<=300) which indicate the number of fish in the lake. Then N lines follow. Each line contains two floating-point number X and Y (0.0<=X,Y<=10.0). You may assume no two fish will at the same point, and no two fish are closer than 0.0001, no two fish in a test case are approximately at a distance of 2.0. In other words, if the distance between the fish and the centre of the fishnet is smaller 1.0001, we say the fish is also caught.

Output
For each test case, you should output the maximum number of fish Ignatius can catch by using his fishnet once.

Sample Input

4
3
6.47634 7.69628
5.16828 4.79915
6.69533 6.20378
6
7.15296 4.08328
6.50827 2.69466
5.91219 3.86661
5.29853 4.16097
6.10838 3.46039
6.34060 2.41599
8
7.90650 4.01746
4.10998 4.18354
4.67289 4.01887
6.33885 4.28388
4.98106 3.82728
5.12379 5.16473
7.84664 4.67693
4.02776 3.87990
20
6.65128 5.47490
6.42743 6.26189
6.35864 4.61611
6.59020 4.54228
4.43967 5.70059
4.38226 5.70536
5.50755 6.18163
7.41971 6.13668
6.71936 3.04496
5.61832 4.23857
5.99424 4.29328
5.60961 4.32998
6.82242 5.79683
5.44693 3.82724
6.70906 3.65736
7.89087 5.68000
6.23300 4.59530
5.92401 4.92329
6.24168 3.81389
6.22671 3.62210

Sample Output

2
5
5
11
題意:

給你好多點的座標,讓你用一個半徑爲1的圓圍進去最多的點

因爲點很少,所以我的方式是根據點對的座標枚舉圓心的位置,讓圓的邊剛好能覆蓋到兩個點。任取兩個點p1(x1,y1),p2(x2,y2) ,可以知道有兩個圓心,並且在p1,p2連線的中垂線上,勾股定理求出p1p2中點到圓心的距離,用參數方程可以求出圓心的座標,然後用圓心的座標去比較和每個點的距離是不是1以內就可以了。
代碼:

#include <iostream>
#include <math.h>
using namespace std;
typedef struct P
{
    double x;
    double y;
};
double dist(P p1,P p2)
{
    return sqrt((p1.y-p2.y)*(p1.y-p2.y)+(p1.x-p2.x)*(p1.x-p2.x));
}
double gett(P p1,P p2)
{
   return  sqrt(1-(dist(p1,p2)/2)*(dist(p1,p2)/2));
}


double getsita(P p1,P p2)
{
    double sita;
    sita=atan(-(p1.x-p2.x)/(p1.y-p2.y));
    return sita;
}


int main()
{
    int n,i,j,ans,num,k;
    double x1,y1,x2,y2,xm,ym,sita,t;
    P temp,p[400],mid;
    int T;
    cin>>T;
    while (T--)
    {
    cin>>n;
    ans=1;
    for (i=1;i<=n;i++) cin>>p[i].x>>p[i].y;

    for (i=1;i<=n;i++)
        for (j=i+1;j<=n;j++)
    if (dist(p[i],p[j])<1.0001*2)
    {
       xm=(p[i].x+p[j].x)/2;
       ym=(p[i].y+p[j].y)/2;
       mid.x=xm;
       mid.y=ym;
       t=gett(p[i],p[j]);
       sita=getsita(p[i],p[j]);
      // cout<<xm<<" "<<ym<<" "<<t<<" "<<sita<<endl;
       x1=xm+cos(sita)*t;
       y1=ym+sin(sita)*t;
       x2=xm-cos(sita)*t;
       y2=ym-sin(sita)*t;
       temp.x=x1;
       temp.y=y1;
       num=0;
     // cout<<temp.x<<" "<<temp.y<<endl;
       for (k=1;k<=n;k++)
        {

            if (dist(temp,p[k])<1.0001) num++;
        }
        if (num>ans) ans=num;


        temp.x=x2;
       temp.y=y2;
       num=0;
     //  cout<<temp.x<<" "<<temp.y<<endl;
       for (k=1;k<=n;k++)
        {

            if (dist(temp,p[k])<1.0001) num++;
        }
        if (num>ans) ans=num;
    }
    cout<<ans<<endl;
    }
    return 0;
}
發佈了44 篇原創文章 · 獲贊 3 · 訪問量 7980
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章