您的位置:首页 > 其它

poj 2653 Pick-up sticks(判断线段是否相交)

2012-03-13 01:15 405 查看
//以下为原blog搬迁过来的内容

【题目大意】:给出n条木棍,然后依次摆放在桌面上,每次摆放的木棍的起始点和终止点给定,求最上面的木棍的标号

【解题思路】:线段判相交就是了,水题

【代码】:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>

using namespace std;

#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long

struct Point
{
double x, y;
Point() {}
Point(double a, double b)
{
x = a, y = b;
}
}point[200000];

struct Line
{
Point a, b;
Line() {}
Line(Point x, Point y)
{
a = x, b = y;
}
}line[150000];

int judge[150000],n,m;
double p,q;

inline int sig(double k)
{
return k < -eps ? -1 : k > eps;
}

inline double det(double x1, double y1, double x2, double y2)
{
return x1 * y2 - x2 * y1;
}

inline double xmult(Point o, Point a, Point b)
{
return det(a.x - o.x, a.y - o.y, b.x - o.x, b.y - o.y);
}

inline double dotdet(double x1, double y1, double x2, double y2)
{
return x1 * x2 + y1 * y2;
}
inline double dot(Point o, Point a, Point b)
{
return dotdet(a.x - o.x, a.y - o.y, b.x - o.x, b.y - o.y);
}

inline int between(Point o, Point a, Point b)
{
return sig(dot(o, a, b));
}

inline int intersect1(Point a, Point b, Point c, Point d)
{
double s1, s2, s3, s4;
int d1 = sig(s1 = xmult(a, b, c));
int d2 = sig(s2 = xmult(a, b, d));
int d3 = sig(s3 = xmult(c, d, a));
int d4 = sig(s4 = xmult(c, d, b));
if ((d1^d2) == -2 && (d3^d4) == -2)
{
return 1;
}
if (d1 == 0 && between(c, a, b) <= 0) return 2;
if (d2 == 0 && between(d, a, b) <= 0) return 2;
if (d3 == 0 && between(a, c, d) <= 0) return 2;
if (d4 == 0 && between(b, c, d) <= 0) return 2;
return 0;
}

inline int intersect(Line u, Line v)
{
return intersect1(u.a, u.b, v.a, v.b);
}

int main()
{
while (~scanf("%d",&n))
{
if (n==0) break;
for (int i=0; i<n; i++)
{
scanf("%lf%lf",&p,&q);
point[2*i-1]=Point(p,q);
scanf("%lf%lf",&p,&q);
point[2*i]=Point(p,q);
line[i]=Line(point[2*i-1],point[2*i]);
}
memset(judge,0,sizeof(judge));
for (int i=0; i<n; i++)
{
for (int j=i+1; j<n; j++)
{
int k;
k=intersect(line[i],line[j]);
if (k!=0) {judge[i]=1; break;}
}
}
printf("Top sticks:");
for (int i=0; i<n-1; i++)
{
if (judge[i]==0)
printf(" %d,",i+1);
}
printf(" %d.\n",n);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: