[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
|
Problemsetter: Md. Kamruzzaman, Member of Elite Problemsetters' Panel
題目描述:
平面上有很多牆壁(線段),以及一個雷射光發射位置,求從這個位置發射最多能打到多少牆壁。
題目解法:
將原點座標平移到雷射光發射位置,每個線段將會對應弧度區間。
對弧度區間查找最大重疊,使用掃描線算法。
#include <stdio.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <functional>
using namespace std;
const double pi = acos(-1);
/*
6
-10 10 10 10
10 10 10 -10
-10 -10 10 -10
-10 -10 -10 10
-5 -5 -15 -15
5 5 15 15
0 0
*/
int main() {
/*freopen("in.txt","r+t",stdin);
freopen("out.txt","w+t",stdout);*/
int n;
int i, j, k;
while(scanf("%d", &n) == 1 && n) {
int sx[505], sy[505], ex[505], ey[505], cx, cy;
for(i = 0; i < n; i++)
scanf("%d %d %d %d", &sx[i], &sy[i], &ex[i], &ey[i]);
scanf("%d %d", &cx, &cy);
vector< pair<double, double> > interval;
for(i = 0; i < n; i++) {
double l = atan2(sy[i] - cy, sx[i] - cx);
double r = atan2(ey[i] - cy, ex[i] - cx);
if(l > r) swap(l, r);
double mid = atan2((sy[i] + ey[i])/2.0 - cy, (sx[i] + ex[i])/2.0 - cx);
if(l <= mid && mid <= r) {
interval.push_back(make_pair(l, r));
interval.push_back(make_pair(l + 2*pi, r + 2*pi));
} else {
interval.push_back(make_pair(r, l + 2*pi));
interval.push_back(make_pair(r + 2*pi, l + 4*pi));
}
}
sort(interval.begin(), interval.end());
priority_queue<double, vector<double>, greater<double> > pQ;
int ret = 0;
for(i = 0; i < interval.size(); i++) {
double deadline = interval[i].first;
while(!pQ.empty() && pQ.top() < deadline)
pQ.pop();
pQ.push(interval[i].second);
ret = max(ret, (int) pQ.size());
}
printf("%d\n", ret);
}
return 0;
}