判断线段是否相交
判断线段是否相交问题,依然见刘汝佳紫书。
// 每条线段的两个端点都在另一条线段两侧(叉积符号不同)
// a1,a2为一条线段,b1,b2为另一条线段
bool SegProInt(Point a1,Point a2,Point b1,Point b2) {
double c1 = Cross(a2-a1,b1-a1), c2 = Cross(a2-a1,b2-a1),
c3 = Cross(b2-b1,a1-b1), c4 = Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
// 允许端点处相交,则需要判断端点是否在线段上,判断函数如下
// 判断p是否在线段a1,a2上
bool OnSegment(Point p,Point a1,Point a2) {
return dcmp(Cross(a1-p,a2-p))==0 && dcmp(Dot(a1-p,a2-p))<0;
}
Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 15567 Accepted: 5880
Description
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.
Input
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.
Output
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.
Sample Input
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
Sample Output
Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.
Hint
Huge input,scanf is recommended.
Source
分析
不断往地上扔棍子,问哪些棍子是在最上层的。把棍子当做线段,看每个线段后是否有线段与当前线段相交,若没有则说明是最上层的。判断线段相交时不需要判断端点。
参考代码
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
const double eps=1e-10;
using namespace std;
// 依旧是板子
struct Point {
double x, y;
Point(double x=0, double y=0) : x(x),y(y) {}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B) {return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (Vector A,double p) {return Vector(A.x*p,A.y*p);}
Vector operator / (Vector A,double p) {return Vector(A.x/p,A.y/p);}
bool operator < (const Point& a,const Point& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
int dcmp(double x) {
if (fabs(x) < eps) return 0;
else return x<0?-1:1;
}
bool operator == (const Point &a,const Point &b) {
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
double Cross(Vector A,Vector B) {
return A.x*B.y - A.y*B.x;
}
bool SegProInt(Point a1,Point a2,Point b1,Point b2) {
double c1 = Cross(a2-a1,b1-a1), c2 = Cross(a2-a1,b2-a1),
c3 = Cross(b2-b1,a1-b1), c4 = Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
int n;
Point p[100005][2];
bool flag[100005];
int main()
{
while(cin >> n && n)
{
for (int i=1;i<=n;i++)
scanf("%lf%lf%lf%lf",&p[i][0].x,&p[i][0].y,&p[i][1].x,&p[i][1].y);
// 用flag来标记每个棍子
memset(flag,false,sizeof(flag));
// 遍历每个棍子
for (int i=1;i<n;i++)
{
for (int j=i+1;j<=n;j++)
if (SegProInt(p[i][0],p[i][1],p[j][0],p[j][1]))
{
flag[i]=true;
break;
}
}
// 输出
printf("Top sticks: ");
int temp=0;
for (int i=1;i<=n;i++)
{
if (!flag[i])
{
if (!temp)
{
printf("%d",i);
temp=1;
}
else
printf(", %d",i);
}
}
printf(".\n");
}
return 0;
}