题目链接:http://poj.org/problem?id=1106
题目:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4606 | Accepted: 2463 |
Description
A transmitter T is located somewhere on a 1,000 square meter grid. It broadcasts in a semicircular area of radius r. The transmitter may be rotated any amount, but not moved. Given N points anywhere on the grid, compute the maximum number of points that can be simultaneously reached by the transmitter's signal. Figure 1 shows the same data points with two different transmitter rotations.
![](https://i-blog.csdnimg.cn/blog_migrate/ca2326027acb017a5c2257e43a0606d6.gif)
All input coordinates are integers (0-1000). The radius is a positive real number greater than 0. Points on the boundary of a semicircle are considered within that semicircle. There are 1-150 unique points to examine per transmitter. No points are at the same location as the transmitter.
Input
Output
Sample Input
25 25 3.5 7 25 28 23 27 27 27 24 23 26 23 24 29 26 29 350 200 2.0 5 350 202 350 199 350 198 348 200 352 200 995 995 10.0 4 1000 1000 999 998 990 992 1000 999 100 100 -2.5
Sample Output
3 4 4
Source
题目大意:给出一个半圆的圆心和半径,然后再给N个点的坐标,半圆可以绕半径旋转任意角度,求半圆最多可以覆盖的点的个数是多少。
解题思路:因为圆心是固定的,就很简单了。先把在圆的覆盖范围内的点找出来,再对这些点循环去找对于每个点来说,跟它在同一侧的点的个数,同侧的点判断就用叉积就可以,当叉积>=0的时候就是同一侧的。 最后找出半圆最多可以覆盖的点的个数。
参考代码:
#include<stdio.h>
#include<math.h>
#define max(a,b) ((a)>(b)?(a):(b))
#define eps 1e-8
int dcmp(double x) {
return x < -eps ? -1 : x > eps; //如果x<-eps 那么就返回-1 不然就判断x是否大于eps 是就返回1 不然返回0
struct point{ //定义一个点的结构体
double x,y;
}p[155];
double distance(point a,point b){ //两点间的距离
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double chacheng(point a,point b,point c){ //向量叉乘的结果
return ((b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y));
}
int main(){
point o;
double r;
int n;
while(scanf("%lf%lf%lf",&o.x,&o.y,&r)!=EOF,r>=0){
scanf("%d",&n);
point s;
int m = 0;
for(int i=0;i<n;i++){
scanf("%lf%lf",&s.x,&s.y);
if(dcmp(distance(s,o)-r)<=0){ //先把在半圆内的点存储到数组p里面
p[m++]=s;
}
}
int ans=0;
for(int i=0;i<m;i++){
int num = 0;
for(int j=0;j<m;j++){
if(dcmp(chacheng(p[i],p[j],o))>=0){
num++;
}
ans = max(ans,num);
}
}
printf("%d\n",ans);
}
return 0;
}
已AC。
欢迎广大读者指正~!