Problem E
Shooter
Input:Standard Input
Output:Standard Output
Time Limit:5 Seconds
The shooter is in a great problem. He is trapped in2DMaze with a laser gun and can use it once. the gun is very powerful and the laser ray, it emanates can travel Erse infinite distance in its direction. in the maze the targets are some Wils (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 Wals, he can destroy with a single shot. the shooter will never stand on a wall.
Input
The input file contains100Sets 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 Wils. In next few lines there will be4 * NIntegers 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[-].
Input is terminated by a case whereN = 0. This case shoshould not be processed.
Output
For each set of input print the maximum number of Wals, 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 |
Q: There are n walls. A single shooter can shoot at most a few walls at a time.
Idea: The radians of each wall correspond to one interval, and the problem is converted to the maximum overlap interval. Note: If the Interval Difference exceeds π, split it into two intervals.
Code:
#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;}