程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVA 10535 - Shooter(掃描+幾何+最大重疊區間)

UVA 10535 - Shooter(掃描+幾何+最大重疊區間)

編輯:C++入門知識

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 
#include 
#include 
#include 
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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved