1. 程式人生 > >hdu-1147(跨立實驗)

hdu-1147(跨立實驗)

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1147

思路:判斷每條線段,如果將要輸入的線段和已經有的線段相交,則這條線段不算。

參考文章:https://blog.csdn.net/hexianhao/article/details/51254487

判斷兩條線段是否相交:https://www.cnblogs.com/wuwangchuxin0924/p/6218494.html

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using
namespace std; struct Node{ double x1,y1,x2,y2; }; struct N{ int x,y; }; int pd(Node t1,Node t2) { N a,b,c,d; a.x=t1.x1;a.y=t1.y1; b.x=t1.x2;b.y=t1.y2; c.x=t2.x1;c.y=t2.y1; d.x=t2.x2;d.y=t2.y2; double u=(c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); double v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y);
if(u*v>0) return 0; double w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y); double z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y); if(w*z>0) return 0; return 1; } const int maxn = 100100; Node vc[maxn]; int vis[maxn]; int main(void) { int n,i,j; while(scanf("%d",&n)&&n) { memset(vis,
0,sizeof(vis)); for(i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&vc[i].x1,&vc[i].y1,&vc[i].x2,&vc[i].y2); } int num=0; for(i=1;i<n;i++) { for(j=i+1;j<=n;j++) { if(pd(vc[i],vc[j])) { vis[i]=1;num++;break; } } } num=n-num; printf("Top sticks:"); for(j=0,i=1;i<=n;i++) { if(vis[i]==0) { j++; if(j==num) printf(" %d.\n",i); else printf(" %d,",i); } } } return 0; }