首页 > 其他 > 详细

hdu_1147 Pick_up sticks

时间:2014-03-21 23:44:18      阅读:785      评论:0      收藏:0      [点我收藏+]

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.

bubuko.com,布布扣
 

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.

分析:

      解题思路比较简单——每放一根棍子,都判断一下它与它前面的且在顶端的棍子是否相交,相交的话则将相应的棍子从解空间中除去(当前这根暂时是在解空间中的)

      但是,如果直接用数组做会超时,然后用链表做(刚开始自己写了个链表,但是写坏掉了,哎,基础啊……),于是最后还是用了list

      除数据结构外,最主要的算法就是判断两直线相交了,直接用了模板


代码:

//hdu 1147
#include <iostream>
#include <cstdio>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <list>
using namespace std;
const double eps=1e-8;

int sgn(double x)
{
    if(fabs(x)<eps) return 0;
    else return x<0 ? -1:1;
}

struct point
{
    double x,y;
    point(){}
    point(double _x,double _y)  //带参构造函数
    {
                x = _x;y = _y;
    }
    point operator -(const point &b) const
    {
          return point(x-b.x,y-b.y);
    }

    double operator ^(const point &b) const
    {
          return x*b.y-y*b.x;
    }
};

struct line
{
    point s,e;
};

struct node{
   line t;
   int no;
};

bool inter(line l1,line l2)
{
    return
    max(l1.s.x,l1.e.x) >=min(l2.s.x,l2.e.x) &&
    max(l2.s.x,l2.e.x) >=min(l1.s.x,l1.e.x) &&
    max(l1.s.y,l1.e.y) >=min(l2.s.y,l2.e.y) &&
    max(l2.s.y,l2.e.y) >=min(l1.s.y,l1.e.y) &&
    sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e))<=0 &&
    sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e))<=0 ;
}

int main()
{
    freopen("in.txt","r",stdin);
    int n;
    int len;
    node cur;
    list<node> stick;
    list<node>::iterator p;

    while(scanf("%d",&n),n){

        for(int i=1;i<=n;i++){
            scanf("%lf%lf%lf%lf",&cur.t.s.x,&cur.t.s.y,&cur.t.e.x,&cur.t.e.y);
            cur.no=i;
            stick.push_back(cur);

            if(i>1){
                p=stick.begin();
                len=stick.size();
                for(int i=1;i<len;i++){
                    if(inter( (*p).t, cur.t))
                       p=stick.erase(p);
                    else
                       p++;
                }
            }

        }
        printf("Top sticks: ");
        p=stick.begin();
        len=stick.size();
        for(int i=1;i<len;i++){
            printf("%d, ",(*p).no);
            p++;
        }
        printf("%d.\n",(*p).no);
        stick.clear();
    }
    return 0;
}


hdu_1147 Pick_up sticks,布布扣,bubuko.com

hdu_1147 Pick_up sticks

原文:http://blog.csdn.net/vuorange/article/details/21747181

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!