您的位置:首页 > 其它

uva 10902 Pick-up Sticks

2016-05-16 21:36 183 查看
题意:有n根木条,一根一根的往一个坐标系上丢(给出木条两点的坐标),问最后不被覆盖的木条有哪些,即丢的木条如果和前面丢的木条交叉的话,就会覆盖前面那根木条。

博主呢一开始直接在uva11343上改了输入,然后提交了,结果TLE。。。看了下题中范围,比较大,所以改用输一个删一堆

/*
线段相交
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
//#define T
#define def 1e-6
using namespace std;
//点
struct Point
{
double x;
double y;
};
//线
struct Line
{
Point p1;
Point p2;
int flag;
};
//判断点在pi-pj在线段的那一侧
int direction(Point* pi,Point* pj,Point* pk)
{
Point p1,p2;
p1.x = pk->x - pi->x;
p1.y = pk->y - pi->y;
p2.x = pj->x - pi->x;
p2.y = pj->y - pi->y;
double cross = p1.x*p2.y - p2.x*p1.y;
if(cross >= def)
return 1;
else if(cross <= -def)
return -1;
else
return 0;
}
//判断点是否在pi和pj为对角点的矩形里
bool onSegment(Point* pi,Point* pj,Point* pk)
{
double minx,miny,maxx,maxy;
if(pi->x > pj->x)
{
minx = pj->x;
maxx = pi->x;
}
else
{
minx = pi->x;
maxx = pj->x;
}
if(pi->y > pj->y)
{
miny = pj->y;
maxy = pi->y;
}
else
{
miny = pi->y;
maxy = pj->y;
}
return (minx <= pk->x)&&(maxx >= pk->x)&&(miny <=pk->y)&&(maxy >= pk->y);
}
//判断线段相交
bool segmentIntersect(Point* p1,Point* p2,Point* p3,Point* p4)
{
int d1 = direction(p3,p4,p1);
int d2 = direction(p3,p4,p2);
int d3 = direction(p1,p2,p3);
int d4 = direction(p1,p2,p4);
if(d1*d2 < 0&&d3*d4 < 0)
return true;
if(!d1 && onSegment(p3,p4,p1))
return true;
if(!d2 && onSegment(p3,p4,p2))
return true;
if(!d3 && onSegment(p1,p2,p3))
return true;
if(!d4 && onSegment(p1,p2,p4))
return true;
return false;
}

vector<Line> LL;
vector<Line>::iterator it;
int main()
{
//Point p1,p2,p3,p4;
#ifdef T
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // T
int n,i,t;
double a,b,c,d;
Line L;
while(scanf("%d",&n)&&n)
{
t=0;
LL.clear();

for(i=1;i<=n;i++)
{
scanf("%lf %lf %lf %lf",&a,&b,&c,&d);
L.p1.x = a;L.p1.y = b;
L.p2.x = c;L.p2.y = d;
L.flag = i;
for(it = LL.begin();it != LL.end();)
{
if(segmentIntersect(&L.p1,&L.p2,&it->p1,&it->p2))
{
LL.erase(it);
}
else
it++;
}
LL.push_back(L);

}

printf("Top sticks:");
for(it = LL.begin();it != LL.end();it++)
{

if(t) printf(",");
printf(" %d",it->flag); t=1;

}
printf(".\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: