HDU 1147 Pick-up sticks(计算几何)

题目:
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.
题目链接
将信息输入完之后枚举每一条线段,遍历此线段之后的所有线段,判断是否有线段与枚举线段相交,若没有则按要求输出。
AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+5;
const int mod = 1e9+7;
const double eps = 1e-8;
const double pi = asin(1.0)*2;
const double e = 2.718281828459;
void fre() {
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
}

struct point {
    double x, y;
};

struct edge {
    point a, b;
}p[maxn];

int n;
bool flag;

bool judge(edge a, edge b) {
    if (min(a.a.x, a.b.x) > max(b.a.x, b.b.x) || min(a.a.y, a.b.y) > max(b.a.y, b.b.y) || min(b.a.x, b.b.x) > max(a.a.x, a.b.x) || min(b.a.y, b.b.y) > max(a.a.y, a.b.y)) {
        return 0;
    }
    double h, i, j, k;
    h = (a.b.x - a.a.x) * (b.a.y - a.a.y) - (a.b.y - a.a.y) * (b.a.x - a.a.x);
    i = (a.b.x - a.a.x) * (b.b.y - a.a.y) - (a.b.y - a.a.y) * (b.b.x - a.a.x);
    j = (b.b.x - b.a.x) * (a.a.y - b.a.y) - (b.b.y - b.a.y) * (a.a.x - b.a.x);
    k = (b.b.x - b.a.x) * (a.b.y - b.a.y) - (b.b.y - b.a.y) * (a.b.x - b.a.x);
    return h * i <= eps && j * k <= eps;
}

int main() {
    //fre();
    while (~scanf("%d", &n) && n) {
        flag = 0;
        for (int i = 1; i <= n; ++i) {
            scanf("%lf%lf%lf%lf", &p[i].a.x, &p[i].a.y, &p[i].b.x, &p[i].b.y);
        }
        printf("Top sticks:");
        for (int i = 1; i <= n; ++i) {
            bool ok = 1;
            for (int j = i + 1; j <= n; ++j) {
                if (judge(p[i], p[j])) {
                    ok = 0;
                    break;
                }
            }
            if (ok) {
                if (!flag) {
                    flag = 1;
                    printf(" %d", i);
                }
                else {
                    printf(", %d", i);
                }
            }
        }
        printf(".\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tony5t4rk/article/details/80365584