HDU 1077Catching Fish(簡單計算幾何)

weixin_34120274發表於2013-10-15

Catching Fish

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1113    Accepted Submission(s): 411

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
 

 

Author
Ignatius.L
 


題目大意:給你n個點的橫縱座標,問你用一個單位圓,最多能使得多少點在圓內,包括圓上的點。

      解題思路:開始比較迷茫,不知道用什麼方法來解,後來覺得可以列舉,但又想不清楚怎麼列舉。這樣,我們每次找兩個點,看能否根據這兩點確定一個單位圓,然後看這個圓能包含其它多少點在這個圓內!

       題目地址:Catching Fish

開始是用陣列寫的,時間直接2s開外了!
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdio>
using namespace std;
int n;
struct node
{
    double x;
	double y;
};
node a[305];

double dis(node p1,node p2)
{
	return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}

int cal(int p1,int p2)
{
	node t1,t2,t3,t4;
	t1=a[p1],t2=a[p2];
	double s,tmp,xx,yy;
	tmp=dis(t1,t2);
	s=tmp/2.0;
	s=sqrt(1.0-s*s);   //s為圓心到t1,t2弦長的距離
	int ans1=0,ans2=0,i;
	xx=(t1.y-t2.y)/tmp;
	yy=(t2.x-t1.x)/tmp;   //(xx,yy)相當於與弦長垂直的單位法向量
	t3.x=(t1.x+t2.x)/2.0,t3.y=(t1.y+t2.y)/2.0;
	t4.x=t3.x+s*xx,t4.y=t3.y+s*yy;  //t4為圓心
	for(i=0;i<n;i++)
	{
		if(dis(t4,a[i])<1.0001)
			ans1++;
	}
	t4.x=t3.x-s*xx,t4.y=t3.y-s*yy;  //t4為圓心
	for(i=0;i<n;i++)
	{
		if(dis(t4,a[i])<1.0001)
			ans2++;
	}
	return ans1>ans2?ans1:ans2;
}

int main()
{
	int i,j;
	int tes;
	scanf("%d",&tes);

	while(tes--)
	{
	    scanf("%d",&n);
		for(i=0;i<n;i++)
			scanf("%lf%lf",&a[i].x,&a[i].y);
		int num;
		int res=1;
		for(i=0;i<n;i++)
			for(j=i+1;j<n;j++)
			{
			      if(dis(a[i],a[j])<2.0001)
				  {
						num=cal(i,j);
						if(num>res) res=num;
				  }
			}

		printf("%d\n",res);
	}
    return 0;
}

//2406MS


後來改用陣列寫了,時間終於降到了1s內,Best solutions裡面還是有很多兩三百ms的,Orz!!
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdio>
using namespace std;
int n;
double a[305][2];

double dis(double *b1,double *b2)
{
	return sqrt((b1[0]-b2[0])*(b1[0]-b2[0])+(b1[1]-b2[1])*(b1[1]-b2[1]));
}

int cal(int p1,int p2)
{
	double t1[2],t2[2],t3[2],t4[2];
	t1[0]=a[p1][0],t1[1]=a[p1][1],t2[0]=a[p2][0],t2[1]=a[p2][1];
	double s,tmp,xx,yy;
	tmp=dis(t1,t2);
	s=tmp/2.0;
	s=sqrt(1.0-s*s);   //s為圓心到t1,t2弦長的距離
	int ans1=0,ans2=0,i;
	xx=(t1[1]-t2[1])/tmp;
	yy=(t2[0]-t1[0])/tmp;   //(xx,yy)相當於與弦長垂直的單位法向量
	t3[0]=(t1[0]+t2[0])/2.0,t3[1]=(t1[1]+t2[1])/2.0;
	t4[0]=t3[0]+s*xx,t4[1]=t3[1]+s*yy;  //t4為圓心
	for(i=0;i<n;i++)
	{
		if(dis(t4,a[i])<1.0001)
			ans1++;
	}
	t4[0]=t3[0]-s*xx,t4[1]=t3[1]-s*yy;  //t4為圓心
	for(i=0;i<n;i++)
	{
		if(dis(t4,a[i])<1.0001)
			ans2++;
	}
	return ans1>ans2?ans1:ans2;
}

int main()
{
	int i,j;
	int tes;
	scanf("%d",&tes);

	while(tes--)
	{
	    scanf("%d",&n);
		for(i=0;i<n;i++)
			scanf("%lf%lf",&a[i][0],&a[i][1]);
		int num;
		int res=1;
		for(i=0;i<n;i++)
			for(j=i+1;j<n;j++)
			{
			      if(dis(a[i],a[j])<2.0001)
				  {
						num=cal(i,j);
						if(num>res) res=num;
				  }
			}

		printf("%d\n",res);
	}
    return 0;
}

//984MS  G++


 

相關文章