Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 6686 | Accepted: 2450 |
Description
Input
Output
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
Source
#include<cstdio> #include<iostream> using namespace std; const int mm=111111; typedef double mType; /**表示点或向量*/ struct Tpoint { mType x,y; Tpoint(){} Tpoint(mType _x,mType _y):x(_x),y(_y){} }; /**有起点和终点的向量或线段*/ struct Tsegment { Tpoint start,end; Tsegment(){} Tsegment(Tpoint _start,Tpoint _end):start(_start),end(_end){} Tsegment(mType sx,mType sy,mType tx,mType ty):start(sx,sy),end(tx,ty){} }; /**生成一个点P到点Q的向量*/ Tpoint MakeVector(Tpoint P,Tpoint Q) { return Tpoint(Q.x-P.x,Q.y-P.y); } /**向量P与Q的叉积PQ*/ mType CrossProduct(Tpoint P,Tpoint Q) { return P.x*Q.y-P.y*Q.x; } /**向量QP与向量QR的叉积,用来判断向量的拐向 * 返回值: >0 向右拐, <0 向右拐,等于零同向或反向 */ mType MultiCross(Tpoint P,Tpoint Q,Tpoint R) { return CrossProduct(MakeVector(Q,P),MakeVector(Q,R)); } /**判断线段P和线段Q是否相交*/ bool IsIntersect(Tsegment P,Tsegment Q) { if(max(P.start.x,P.end.x)<min(Q.start.x,Q.end.x)||max(Q.start.x,Q.end.x)<min(P.start.x,P.end.x)|| max(P.start.y,P.end.y)<min(Q.start.y,Q.end.y)||max(Q.start.y,Q.end.y)<min(P.start.y,P.end.y))return 0; return (MultiCross(P.end,P.start,Q.start)*MultiCross(P.end,P.start,Q.end)<0&& MultiCross(Q.end,Q.start,P.start)*MultiCross(Q.end,Q.start,P.end)<0); } Tsegment g[mm]; int main() { int i,j,n; mType sx,sy,ex,ey; while(scanf("%d",&n),n) { for(i=1;i<=n;++i) { scanf("%lf%lf%lf%lf",&sx,&sy,&ex,&ey); g[i]=Tsegment(sx,sy,ex,ey); } printf("Top sticks:"); for(i=1;i<n;++i) { for(j=i+1;j<=n;++j) if(IsIntersect(g[i],g[j]))break; if(j>n)printf(" %d,",i); } printf(" %d.\n",n); } return 0; }