标签:poj 2653 pick-up sti 线段相交判断 几何
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 10949 | Accepted: 4085 |
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<cstring> #include<algorithm> #include<cmath> #include<iostream> #include<cstdio> #include<vector> #define N 100010 #define inf 99999999999.0 using namespace std; struct Point { double x,y; } ; struct Line { Point p1,p2; } L[N]; int n,num; bool vis[N]; Point addPoint(double x,double y) { Point it; it.x=x; it.y=y; return it; } Line addLine(double x1,double y1,double x2,double y2) { Point a=addPoint(x1,y1); Point b=addPoint(x2,y2); Line it; it.p1=a,it.p2=b; return it; } double multi(Point p0,Point p1,Point p2) { return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } ///判断是否相交 bool is_inter(Point s1,Point e1,Point s2,Point e2) { return (max(s1.x,e1.x)>=min(s2.x,e2.x))&& (max(s2.x,e2.x)>=min(s1.x,e1.x))&& (max(s1.y,e1.y)>=min(s2.y,e2.y))&& (max(s2.y,e2.y)>=min(s1.y,e1.y))&& (multi(s1,s2,e1)*multi(s1,e1,e2)>0)&& (multi(s2,s1,e2)*multi(s2,e2,e1)>0); } int ans[1010]; int main() { //freopen("test.in","r",stdin); while(cin>>n&&n) { double x,y,_x,_y; num=0; memset(vis,0,sizeof vis); for(int i=0; i<n; i++) { scanf("%lf%lf%lf%lf",&x,&y,&_x,&_y); L[i]=addLine(x,y,_x,_y); } for(int i=0; i<n; i++) { for(int j=i+1; j<n; j++) { if(is_inter(L[i].p1,L[i].p2,L[j].p1,L[j].p2)) { vis[i]=1; break; } } } int k=0; for(int i=0; i<n; i++) { if(!vis[i]) { ans[k++]=i+1; } } printf("Top sticks: "); for(int i=0; i<k-1; i++) printf("%d, ",ans[i]); printf("%d.\n",ans[k-1]); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
poj 2653 Pick-up sticks(几何线段相交判断)
标签:poj 2653 pick-up sti 线段相交判断 几何
原文地址:http://blog.csdn.net/acm_baihuzi/article/details/47301881