POJ 3304
来源:互联网 发布:淘宝收藏宝贝没反应 编辑:程序博客网 时间:2024/05/29 15:38
POJ 3304
计算几何基础,叉积应用
大意是给出n条线段,问是否存在一条直线使得所有线段投影到直线上后存在至少一个公共点。其实就是判断是否存在一条直线与所有线段相交。
画图稍加分析可以发现,如果存在一条直线与所有线段相交,我们可以转动这条直线与至少两个线段端点重合,也就是说,我们可以通过枚举两个端点来确定一条直线,再判断它是否与所有线段相交。
枚举端点O(n^2),判断与线段相交O(n),总体时间复杂度O(n^3),对于n不超过100的规模,可以接受。
判断直线与线段相交只要两个端点分别与直线做叉积,线段两个端点相对于直线的方位相同则说明线段没有横跨直线,即不相交。除此,在枚举点时要注意判断是否重合,|dx|
#include<cmath>#include<ctime>#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#define mm0(a) memset(a,0,sizeof(a));#define debug puts("It's ok here!")using namespace std;const int maxn=5e3+5;const double eps=1e-8;struct Point{ double x, y; Point(){} Point(double _x, double _y):x(_x), y(_y){}};struct line{ Point s, t; line(){} line(double sx,double sy,double tx,double ty){s.x=sx; s.y=sy; t.x=tx; t.y=ty;} line(Point _s, Point _t):s(_s), t(_t){}};int dcmp(double x){ if(fabs(x)<eps) return 0; return x<0?-1:1;}double cross(Point a,Point b,Point c){ return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);}bool crossleft(Point a,Point b,Point c){ return cross(a, b, c)>0;}double dis(Point a,Point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}Point P[maxn];line Seg[maxn];int n, m;bool ok(Point a,Point b){ for(int i=0;i<n;i++) if(dcmp(cross(Seg[i].s,a,b)*cross(Seg[i].t,a,b))>0) return false; return true;}int main(){ double x1, y1, x2, y2; int T=0; scanf("%d",&T); while(T--){ scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%lf%lf%lf%lf",&x1, &y1, &x2, &y2); P[2*i]=Point(x1,y1), P[2*i+1]=Point(x2,y2); Seg[i]=line(P[2*i],P[2*i+1]); } int flag=0; for(int i=0;i<2*n;i++) for(int j=i+1;j<2*n;j++) if((dcmp(P[i].x-P[j].x)!=0||dcmp(P[i].y-P[j].y)!=0)&&ok(P[i],P[j])){ flag=1; } if(flag) puts("Yes!"); else puts("No!"); } return 0;}
0 0
- POJ 3304
- poj 3304
- poj 3304
- poj 3304
- poj 3304
- POJ-3304
- poj 3304
- poj 3304
- poj 3304
- POJ 3304
- poj 3304
- POJ 3304 Segments
- POJ 3304 计算几何
- poj 3304 Segments
- poj 3304 Segments
- poj 3304 Segments
- POJ 3304 Segments
- poj 3304解题报告
- Java自动生成Mvp框架目录结构
- Codeforces 768B Code For 1
- Xcode无开发者账号真机测试
- Android开发--fragment中Button点击切换Activity
- IntelliJ IDEA 创建并部署单纯的web工程(不用Maven和Gradle管理)
- POJ 3304
- Python3.6连接MySQL
- Intent传递数据与Activity启动模式singleTask的关系。
- easyui datagrid 相同列合并/编辑行后保存
- 学生选课抽签管理系统设计(二)
- 阿里云数据盘挂载遇到的一些问题总结
- 轻量级的测试工具:ContiPerf
- JavaScript 帧动画
- volatile详解