您的位置:首页 > 其它

hdu_1147 Pick_up sticks

2014-03-21 22:29 323 查看
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.



 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
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.

 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  计算几何