您的位置:首页 > 其它

Poj-2653-Pick-up sticks

2015-11-15 21:47 239 查看
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.

题目的大概意思就是给你n个木棒,然后扔在地上,给出每个木棒的两个端点坐标,问你哪些木棒是在最上面(如果两条木棒相交,则后者在上,前者在下),输入n为零时结束。

思路:这是一道简单的线段相交问题,只需枚举当前木棒是否与之后的木棒是否相交即可,用一个数组表示是否有木棒在其上面。最后对数组遍历输出即可,此题暴力即可解决。主要是判断线段是否相交,用叉乘判断即可,假如有线段AB和线段CD,我们需要判断其相交的条件是A点和B点在直线CD两边或其上同时且C点和D点在直线AB两边或其上。本题貌似没有两条线段重合的数据。

代码如下:

#include <algorithm>

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <math.h>

using namespace std;

struct note{

    double x1;

    double y1;

    double x2;

    double y2;

};

struct note a[100010],b;

int book[100010];

//判断是否相交

int chacheng(note p,note q)

{

    if(((p.x2-p.x1)*(q.y1-p.y1)-(q.x1-p.x1)*(p.y2-p.y1))*((p.x2-p.x1)*(q.y2-p.y1)-(q.x2-p.x1)*(p.y2-p.y1))<=0&&((q.x2-q.x1)*(p.y1-q.y1)-(p.x1-q.x1)*(q.y2-q.y1))*((q.x2-q.x1)*(p.y2-q.y1)-(p.x2-q.x1)*(q.y2-q.y1))<=0)

        return 1;

    else

        return 0;

}

int main()

{

    int n;

    while(scanf("%d",&n)&&n!=0)

    {

        for(int i=1;i<=n;i++)

        {

            scanf("%lf %lf %lf %lf",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);

            //标记当前线段没有与任何线段相交

            book[i]=0;

        }

        //对当前线段进行枚举(最后一个线段一定在最上面,所以不讨论)

        for(int i=1;i<n;i++)

        {

            for(int j=i+1;j<=n;j++)

            {

                //判断是否相交

                if(chacheng(a[i],a[j]))

                {

                    //如果有线段在当前线段之上,则进行标记,结束循环

                    book[i]=1;

                    break;

                }

            }

        }

        //遍历数组,输出

        printf("Top sticks:");

        for(int i=1;i<n;i++)

            if(book[i]==0)

                printf(" %d,",i);

        printf(" %d.\n",n);

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: