狗狗40题——1041

作者在 2013-06-01 20:44:54 发布以下内容

这是一个成长吧

开个好头 

下面将zoj上面的1041题解题报告写一下:


ZOJ Problem Set - 1041
Transmitters
Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 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.

For each transmitter, the output contains a single line with the maximum number of points that can be contained in some semicircle. 

Example 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


Example output:

3
4
4


这明显是一道计算机和题目,题目的意思就是找出某一状态半圆的中的点数最多有多少个。解题思路可以先将位于圆中的点找出来进行计数,然后将点集排序(根据点到圆心的半径排序),然后遍历一遍。此题再找最大个数的过程之中,利用了向量的叉积,来解决了在某一方向是否存在斜率去确定在以圆心与其中在园内的一点联立而成的直线两边的点的个数,进而进行比较。下面是源代码:


#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
 int a[2];
 int b;
 int nn;
}point[151];
bool cmp(node t,node tt)  //进行快速排序的comp
{
 return t.b>tt.b;
}
double dis(int x,int y,int x1,int y1)  //返回两点之间的距离
{
 return (double)((x1-x)*(x1-x)+(y-y1)*(y-y1));
}
int main()
{
 int x,y,n,i,j,num,temp1,temp2,temp,max;
 double r,w;
 while(cin>>x>>y>>r&&r>=0)
 {
  cin>>n;  //输入点的个数
  for(i=0;i<n;i++)  //初始化
  {
   point[i].b=0;   //此处是一个标记,记录这个点是不是在圆内
   point[i].nn=0;  //nn记录通过这个点可以在半圆中包括的点的个数
  }
  num=0;
  for(i=0;i<n;i++)
  {
   cin>>point[i].a[0]>>point[i].a[1];
   if(dis(x,y,point[i].a[0],point[i].a[1])<=r*r)
   {
    point[i].b=1;
    num++;
   }
  }
  sort(point+0,point+n,cmp);  //将在圆内的点放在结构体数组的前面
  for(i=0;i<num;i++)
  {
   temp1=0;   //统计在这条直线中y小于0的点个数
   temp2=0;  //统计在这条直线中y大于0的点个数
   temp=0;  //统计在这条直线中y等于0的点个数
   if(x==point[i].a[0])  //如果斜率是0,也就是你选的点和圆心的横坐标相等
   {
    for(j=0;j<num;j++)
    {
     if(point[j].a[0]<x)  //在这条直线左侧
      temp1++;
     else if(point[j].a[0]>x)   //在这条直线右侧
      temp2++;
     else   //在这条直线上
      temp++;
    }
   }
   else
   {
    for(j=0;j<num;j++)
    {
     w=(point[j].a[0]*(y-point[i].a[1])+point[j].a[1]*(point[i].a[0]-x)+x*point[i].a[1]-y*point[i].a[0])*(x-point[i].a[0]);


     if(w<0)
      temp1++;
     else if(w>0)
      temp2++;
     else
      temp++;
    }
   }
   if(temp1>temp2)
    point[i].nn=temp1+temp;
   else
    point[i].nn=temp2+temp;
  }
  max=point[0].nn;
  for(i=1;i<num;i++)
   if(max<point[i].nn)
    max=point[i].nn;
  cout<<max<<endl;
 }
 return 0;
}




zoj | 阅读 1596 次
文章评论,共0条
游客请输入验证码
文章分类
最新评论