Pick-up sticks
http://poj.org/problem?id=2653
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 14325 Accepted: 5420
Description
Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.
Input
Input consists of a number of cases. The data for each case start with 1 <= n <= 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.
Output
For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.
The picture to the right below illustrates the first case from input.
Sample Input
5
1 1 4 2
2 3 3 1
1 -2.0 8 4
1 4 8 2
3 3 6 -2.0
3
0 0 1 1
1 0 2 1
2 0 3 1
0
Sample Output
Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.
题解:向地上扔棒子,求没被后扔的棒子压住的棒子号码。
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
struct Line { //一个棒子相当于一根线段
double x, y, x1, y1;
};
double Direction(int a, int b, int c, int d, int e, int f) {//以(a,b)为共享端点,判断一条线段相对另一条线段的方向
return (c-a)*(f-b) - (e-a)*(d-b);
}
bool OnSegment(int a, int b, int c, int d, int e, int f) {//判断点是否在另一条线段的坐标范围内
if (min(a, e) <= c && c <= max(a, e) && min(b, f) <= d && d <= max(b, f))
return true;
return false;
}
bool Point(struct Line a, struct Line b) { //判断两个棒子是否相交 ,相交返回true,不相交返回false
double d1, d2, d3, d4;
d1 = Direction(a.x, a.y, b.x, b.y, b.x1, b.y1);
d2 = Direction(a.x1, a.y1, b.x, b.y, b.x1, b.y1);
d3 = Direction(b.x, b.y, a.x, a.y, a.x1, a.y1);
d4 = Direction(b.x1, b.y1, a.x, a.y, a.x1, a.y1);
if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) && ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0)))
return true;
else if (d1 == 0 && OnSegment(a.x, a.y, b.x, b.y, b.x1, b.y1)) //当它们共线时,判断它们是否相交
return true;
else if (d2 == 0 && OnSegment(a.x1, a.y1, b.x, b.y, b.x1, b.y1))
return true;
else if (d3 == 0 && OnSegment(b.x, b.y, a.x, a.y, a.x1, a.y1))
return true;
else if (d4 == 0 && OnSegment(b.x1, b.y1, a.x, a.y, a.x1, a.y1))
return true;
else
return false;
}
int main() {
int n;
while (scanf("%d", &n) == 1 && n) {
struct Line a[n+1];
int num[n+1] = {0};
int temp = 1;
for (int i = 1; i <= n ; i++) { //输入
scanf("%lf %lf %lf %lf", &a[i].x, &a[i].y, &a[i].x1, &a[i].y1);
}
for (int i = 1; i <= n; i++) {
bool flag;
for (int j = i+1; j <= n; j++) {
flag = Point(a[i], a[j]);//判断当前的棒子是否被后来的棒子压住
}
if (!flag) //记录没被压住的棒子的序号
num[temp++] = i;
}
printf("Top sticks:"); //输出
for (int i = 1; i < temp-1; i++)
printf(" %d,", num[i]);
printf(" %d.", num[temp-1]);
printf("\n");
}
return 0;
}