提示:
(小)剪枝,每次用队列来优化,如果之前的被覆盖了,那么不在放进队里,最后统计队里残余的线段。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
struct point {
double x,y;
};
struct line{
point s;//start
point t;//to
int num;
}a[100100];
int paichi(line a,line b){
if(max(a.s.x,a.t.x)>=min(b.s.x,b.t.x)
&&max(b.s.x,b.t.x)>=min(a.s.x,a.t.x)
&&max(a.s.y,a.t.y)>=min(b.s.y,b.t.y)
&&max(b.s.y,b.t.y)>=min(a.s.y,a.t.y))
return 1;
else return 0;
}
double cross(point a,point b,point c){
double x1=b.x-a.x;
double y1=b.y-a.y;
double x2=c.x-a.x;
double y2=c.y-a.y;
return x1*y2-x2*y1;
}
int kuali(line a,line b){
if(cross(a.s,a.t,b.s)*cross(a.s,a.t,b.t)<=0
&& cross(b.s,b.t,a.s)*cross(b.s,b.t,a.t)<=0)
return 1;
return 0;
}
int vis[100100];
int main(){
int n;
while(scanf("%d",&n),n){
memset(vis,0,sizeof(vis));
scanf("%lf%lf%lf%lf",&a[1].s.x,&a[1].s.y,&a[1].t.x,&a[1].t.y);a[1].num = 1;
queue<line> q;q.push(a[1]);
for(int i = 2;i <= n;i++){
scanf("%lf%lf%lf%lf",&a[i].s.x,&a[i].s.y,&a[i].t.x,&a[i].t.y);a[i].num = i;
int siz = q.size();line temp;
for(int j = 0;j < siz;j++){
temp = q.front();
q.pop();
if(paichi(a[i],temp) && kuali(a[i],temp)){
}
else
q.push(temp);
}
q.push(a[i]);
}
int cnt = 0,cnt2 = 0;line temp;
while(!q.empty()){
temp = q.front();
vis[temp.num] = 1;cnt++;
q.pop();
}
printf("Top sticks: ");
for(int i = 1;i <= n;i++){
if(vis[i] == 1){
if(cnt2 == cnt-1)
printf("%d.\n",i);
else
printf("%d, ",i);
cnt2++;
}
}
}
return 0;
}