Transmitters

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

Transmitters

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.

输入样例 1

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

输出样例 1

3
4
4

题意:给定雷达T坐标,和N个点。它在半径为r的半圆形区域内广播。雷达可以任意旋转,但不能移动。给定网格上任意位置的N个点,计算发送者的信号可以同时到达的最大点数。

思路:题目给定T的坐标和半径,只要判断点是否在覆盖范围内。因为是半圆,所以还要判断点是否是在半径的同一侧。然后依据给出的点,依次记录比较不同角度所能覆盖的最大点数。

首先我们要明白一个定理:向量a×向量b(×为向量叉乘),若结果小于0,表示向量b在向量a的顺时针方向;若结果大于0,表示向量b在向量a的逆时针方向;若等于0,表示向量a与向量b平行。(顺逆时针是指两向量平移至起点相连,从某个方向旋转到另一个向量小于180度)。如下图:
Transmitters
在上图中,OA×OB = 2 > 0, OB在OA的逆时针方向;OA×OC = -2 < 0,OC在OA的顺势针方向。即叉乘结果大于0,后一个在前一个的逆时针方向;小于零,后一个在前一个的顺时针方向。
因此我们可以以此为根据,判断哪些点在雷达面积的同侧”

#include<cstdio>
#include<math.h>
#include<string>
#include<iostream>
#define eps 1e-8
using namespace std;
struct point{
	int x,y;
}a[200];

int mul(point A,point B,point C){//判断C点在线段AB的左侧还是右侧 
	double k;
	k=A.x*B.y+B.x*C.y+C.x*A.y-A.x*C.y-B.x*A.y-C.x*B.y;
	if(k>eps) return 1;
	else if(k<-eps)  return -1;
	else return 0;
}

double dist(point A,point B){
	double line;
	line=(A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y);
	line=sqrt(line);
	return line;
}

int check(point m,int n,double r){//求以点M和雷达T为半径时,覆盖的点数 
	int i,k,s=0;
	double l;
	for(i=1;i<=n;i++){
		k=mul(a[0],m,a[i]);
		l=dist(a[0],a[i]);
		if(k>=0 && l<=r) s++;
	}
	return s;
}
int main(){
	int n,i,s,max;
	float r;
	cin>>a[0].x>>a[0].y>>r;
	while(r>eps){
		cin>>n;
		for(i=1;i<=n;i++) cin>>a[i].x>>a[i].y;
		max=0;
		for(i=1;i<=n;i++){
			s=check(a[i],n,r);
			if(s>max) max=s;
		}
		printf("%d\n",max);
		cin>>a[0].x>>a[0].y>>r;
	}
	return 0;
}

参考博客:
https://www.cnblogs.com/tuyang1129/p/9390376.html

上一篇:平面内有N个点,如何快速求出距离最近的点对?


下一篇:Gym102483A - Access Points(单调栈)