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;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值