ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

(kuangbin带你飞--计算几何)Pick-up sticks

2019-08-23 10:40:24  阅读:488  来源: 互联网

标签:sticks point s2 s1 up Pick e1 e2


原题目:

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<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
const double EPS=1e-6;
int n;
struct point
{
    double  x, y;
};
struct line
{
    point p1,p2;
    int flag;
} line1[100005];

double multi(point p0,point p1,point p2)//叉积
{
    return(p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

bool IsIntersected(point s1,point e1,point s2,point e2) //线段相交
{
    return (max(s1.x,e1.x)>=min(s2.x,e2.x))&&
           (max(s2.x,e2.x)>=min(s1.x,e1.x))&&
           (max(s1.y,e1.y)>=min(s2.y,e2.y))&&
           (max(s2.y,e2.y)>=min(s1.y,e1.y))&&
           (multi(s1,s2,e1)*multi(s1,e1,e2)>0)&&
           (multi(s2,s1,e2)*multi(s2,e2,e1)>0);
}

int num[100005];

int main()
{
    while(~scanf("%d",&n)&&n)
    {
        for(int i=1; i<=n; i++)
            scanf("%lf%lf%lf%lf",&line1[i].p1.x,&line1[i].p1.y,&line1[i].p2.x,&line1[i].p2.y);

        int f=0,k=0;
        for(int i=1; i<n; i++)
        {
            f=0;
            for(int j=i+1; j<=n; j++)
            {
                if(IsIntersected(line1[i].p1,line1[i].p2,line1[j].p1,line1[j].p2))
                {
                        f=1;
                        break;
                }
            }
            if(!f)  num[k++]=i;
        }
        num[k]=n;
        printf("Top sticks:");
        for(int i=0; i<=k; i++)
        {
            if(i==0)
                printf(" %d",num[i]);
            else
                printf(", %d",num[i]);
        }
        printf(".\n");
    }
    return 0;
}

套模板吧。。。

标签:sticks,point,s2,s1,up,Pick,e1,e2
来源: https://blog.csdn.net/weixin_43791787/article/details/100032126

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有