poj 2653 Pick-up sticks 线段相交判断

http://poj.org/problem?id=2653

顺序的在地上扔一些细棒,求最后没有被压的细棒。
我是用list容器存储,枚举过去,有被压的就删除,效率比较底,运行800多ms。
 
 
     
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<cmath>
#include<list>
using namespace std;
struct point
{
double x,y;
};
struct line
{
point a,b;
int num;
};
list<line> dlist;
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 is(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);
}
double dis(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
int main()
{
int n,i;
line in;
list<line>::iterator iter1,iter2;
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++)
{
scanf("%lf%lf%lf%lf",&in.a.x,&in.a.y,&in.b.x,&in.b.y);
in.num=i+1;
dlist.push_back(in);
}
for(iter1=dlist.begin();iter1!=dlist.end();iter1++)
for(iter2=dlist.begin();iter2!=iter1;)
{
if(is((*iter1).a,(*iter1).b,(*iter2).a,(*iter2).b))
iter2=dlist.erase(iter2);
else
iter2++;
}
printf("Top sticks: %d",(*dlist.begin()).num);
for(iter1=++dlist.begin();iter1!=dlist.end();iter1++)
printf(", %d",(*iter1).num);
printf(".\n");
dlist.clear();
}
return 0;
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 

你可能感兴趣的:(poj)