POJ 1106:Transmitters(计算几何)
题目链接:POJ 1106
Description
In a wireless network with multiple transmitters sending on the same frequencies, it is often a requirement that signals don’t overlap, or at least that they don’t conflict. One way of accomplishing this is to restrict a transmitter’s coverage area. This problem uses a shielded transmitter that only broadcasts in a semicircle.
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.
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
Input consists of information for one or more independent transmitter problems. Each problem begins with one line containing the (x,y) coordinates of the transmitter followed by the broadcast radius, r. The next line contains the number of points N on the grid, followed by N sets of (x,y) coordinates, one set per line. The end of the input is signalled by a line with a negative radius; the (x,y) values will be present but indeterminate. Figures 1 and 2 represent the data in the first two example data sets below, though they are on different scales. Figures 1a and 2 show transmitter rotations that result in maximal coverage.
Output
For each transmitter, the output contains a single line with the maximum number of points that can be contained in some semicircle.
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
题解:题目大意说,有一个半径已知可以旋转的半圆,要求半圆能够覆盖的点最多
于是可以采用暴力遍历n个点,以当前遍历的点与圆心相连的直线为半径所在的直线,然后利用叉积来判断其他点在该直线的左边还是右边,进而可以理解为左半圆与右半圆,于是通过不断判断获得最大能覆盖点的数目。
代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
/**每个点的结构*/
struct Node
{
double x,y;
}node[1010];
int main()
{
double sx,sy,r; //分别表示圆心横坐标、纵坐标、圆半径
int n; //点的数目
while(~scanf("%lf%lf%lf",&sx,&sy,&r))
{
if(r<0)
break;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&node[i].x,&node[i].y);
}
int maxNum=-1; //记录能覆盖的点的最大数目
for(int i=0;i<n;i++)
{/**直接暴力*/
int temp=0;
for(int j=0;j<n;j++)
{
double x1=node[i].x-sx;
double y1=node[i].y-sy;
double x2=node[j].x-sx;
double y2=node[j].y-sy;
if(x1*y2-x2*y1>=0)
{
if(x2*x2+y2*y2<=r*r) //保证圆能覆盖到该点
temp++;
}
}
if(temp>maxNum)
maxNum=temp;
}
printf("%d\n",maxNum);
}
return 0;
}