UVA 10535 - Shooter(扫描+几何+最大重叠区间)

Problem E
Shooter
Input: 
Standard Input

Output: Standard Output

Time Limit: 5 Seconds

 

The shooter is in a great problem. He is trapped in a 2D maze with a laser gun and can use it once. The gun is very powerful and the laser ray, it emanates can traverse infinite distance in its direction. In the maze the targets are some walls (Here this is line segments). If the laser ray touches any wall or intersects it, that wall will be destroyed and the ray will advance ahead. The shooter wants to know the maximum number of walls, he can destroy with a single shot. The shooter will never stand on a wall.

 

Input

The input file contains 100 sets which needs to be processed. The description of each set is given below:

 

Each set starts with a postive integer, N(1<=N<=500) the number of walls. In next few lines there will be 4*N integers indicating two endpoints of a wall in cartesian co-ordinate system. Next line will contain (x, y) the coordinates of the shooter. All coordinates will be in the range[-10000,10000].

 

Input is terminated by a case where N=0. This case should not be processed.

 

Output

For each set of input print the maximum number of walls, he can destroy by a single shot with his gun in a single line.

 

Sample Input                               Output for Sample Input

3
0 0 10 0
0 1 10 1
0 2 10 2
0 -1
3
0 0 10 0
0 1 10 1
0 3 10 3
0 2
0

3

2

 

 


题意:有n面墙,一个射手在一个点上,问一枪最多打穿几面墙。

思路:每面墙能打算的弧度对应一个区间,然后问题就转化为了最大重叠区间的问题了。注意如果区间差超过π,要拆成两个区间来考虑

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
const int N = 505;
const double pi = acos(-1.0);

int n, en;
double x, y;
struct Seg{
	double x1, y1, x2, y2;
}s[N];

struct E {
	double t;
	int v;
}e[4 * N];

bool eq(double a, double b) {
	return fabs(a - b) < 1e-9;
}

bool cmp(E a, E b) {
	if (!eq(a.t, b.t)) return a.t < b.t;
	return a.v > b.v;
}

void build() {
	en = 0;
	for (int i = 0; i < n; i++) {
		double r1 = atan2(s[i].y1 - y, s[i].x1 - x);
		double r2 = atan2(s[i].y2 - y, s[i].x2 - x);
		if (r1 > r2) swap(r1, r2);
		if (r2 - r1 >= pi) {
			e[en].t = -pi; e[en++].v = 1;
			e[en].t = r1; e[en++].v = -1;
			r1 = r2; r2 = pi;
		}
		e[en].t = r1; e[en++].v = 1;
		e[en].t = r2; e[en++].v = -1;
	}
	sort(e, e + en, cmp);
}

void init() {
	for (int i = 0; i < n; i++)
		scanf("%lf%lf%lf%lf", &s[i].x1, &s[i].y1, &s[i].x2, &s[i].y2);
	scanf("%lf%lf", &x, &y);
	build();
}

int solve() {
	int ans = 0, num = 0;
	for (int i = 0; i < en; i++) {
		num += e[i].v;
		ans = max(ans, num);
	}
	return ans;
}

int main() {
	while (~scanf("%d", &n) && n) {
		init();
		printf("%d\n", solve());
	}
	return 0;
}



UVA 10535 - Shooter(扫描+几何+最大重叠区间)

上一篇:在TS码流中出现不连续指示时,对于不连续的处理


下一篇:史上最全的iOS面试题及答案