poj1106 Transmitters (枚舉+叉積運用)

Transmitters
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4328   Accepted: 2295

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

題目大意:就是給定一個圓的圓心和半徑,求某個半圓能包含最多的點的個數;
思路:由於圓心和半徑都確定,又是180度,這裏枚舉過一點的直徑,然後將其他點(也包括自己點)與圓心的連線構成的向量與該直徑做叉積,
就可以用叉積的等於,大於,小於0判斷點在直徑上,右,左。 這裏要記錄直徑兩邊的加直徑上的點的個數,取最大的。

處理方法:在輸入過程中,將所有不在圓外的點刪除,對於某些在圓心上的點(雖然題目說明,任意兩點都不相同,但是沒有說明,不能有點在圓心上)也將其視爲在圓外,
但是枚舉的點數量的初始值應該加上這些點。


代碼如下:
/*
    @author : liuwen
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <climits> //INT_MAX INT_MIN LONG_LONG_MAX LONG_LONG_MIN
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps=1e-10;
struct Point{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    else return x<0?-1:1;
}
double cross(Vector a,Vector b)
{
    return a.x*b.y-a.y*b.x;
}
bool isIn(double x0,double y0,double r,double x,double y)
{
    if((x-x0)*(x-x0)+(y-y0)*(y-y0)<=r*r)    return true;
    else    return false;
}
Point a[1005];
int main()
{
    //freopen("in.txt","r",stdin);
    double x0,y0,r,x,y;
    int T;
    while(cin>>x0>>y0>>r){
        if(r<0) continue;
        cin>>T;
        int cnt=0,ans=0;
        for(int i=0;i<T;i++){
            cin>>x>>y;
            if(isIn(x0,y0,r,x,y)){
                if(dcmp(x0-x)==0&&dcmp(y0-y)==0){
                    ans++;
                }else{
                    a[cnt++]=Point(x,y);
                }
            }
        }
        int init=ans; //初始值
        int lnum=init,rnum=init;
        for(int i=0;i<cnt;i++){
            lnum=rnum=init;
            Vector diamer=Vector(a[i].x-x0,a[i].y-y0);
            for(int j=0;j<cnt;j++){
                int cmp=dcmp(cross(diamer,Vector(a[j].x-x0,a[j].y-y0))); //判斷在直徑中,右,還是左
                if(cmp==0)  rnum++,lnum++;//上
                else if(cmp>0)  rnum++;  //右
                else lnum++;//左
            }
            ans=max(ans,max(lnum,rnum));
        }
        cout<<ans<<endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章