前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ 2653--Pick-up sticks(判断线段相交)

POJ 2653--Pick-up sticks(判断线段相交)

作者头像
Enterprise_
发布2019-02-20 10:38:01
3740
发布2019-02-20 10:38:01
举报
文章被收录于专栏:小L的魔法馆小L的魔法馆

Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 14568 Accepted: 5510 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. Hint

Huge input,scanf is recommended.

  1. 按顺序随机扔木棒,后扔的木棒才可以在之前的木棒的上面,所以用线段相交去和之后的木棒判断相交,如果相交,则在下面,否则就在上面
  2. Code
代码语言:javascript
复制
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int MAX = 100000;
typedef struct point {
    double x;
    double y;
    point() {

    }
    point(double a, double b) {
        x = a;
        y = b;
    }
}point;
typedef struct edge {
    point start;
    point end;
    edge() {

    }
    edge(point a, point b) {
        start = a;
        end = b;
    }
}edge;
edge line[MAX], tmp;
point p;
int n;
double x, y, x2, y2;
inline double max(double a, double b) { return a > b ? a : b; }
inline double min(double a, double b) { return a < b ? a : b; }
double multi(point p1, point p2, point p0) {
    return (p2.y - p0.y)*(p1.x - p0.x) - (p2.x - p0.x)*(p1.y - p0.y);
}
bool Across(edge v1, edge v2) {
    if (max(v1.start.x, v1.end.x) >= min(v2.start.x, v2.end.x) &&
        max(v2.start.x, v2.end.x) >= min(v1.start.x, v1.end.x) &&
        max(v1.start.y, v1.end.y) >= min(v2.start.y, v2.end.y) &&
        max(v2.start.y, v2.end.y) >= min(v1.start.y, v1.end.y) &&
        multi(v1.start, v2.end, v2.start)*multi(v2.end, v1.end, v2.start) > 0 &&
        multi(v2.start, v1.end, v1.start)*multi(v1.end, v2.end, v1.start) > 0
        )
        return true;
    return false;
}
int main(void) {
    while (~scanf("%d", &n) && n) {
        for (int i = 0; i < n; i++) {
            scanf("%lf%lf%lf%lf", &x, &y, &x2, &y2);
            line[i] = edge(point(x, y), point(x2, y2));
        }
        printf("Top sticks:");
        for (int i = 0; i < n-1; i++) {     //最后一根木棒必定在上面
            tmp = line[i];
            int flag = 0;
            for (int j = i + 1; j < n; j++) {
                if (Across(tmp, line[j]))   //和之后的木块相交,一定在下面
                {
                    flag = 1;
                    break;
                }
            }
            if (flag != 1) {
                printf(" %d,", i + 1);
            }
        }
        printf(" %d.\n", n);
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年07月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档