zoukankan      html  css  js  c++  java
  • POJ3304(KB13-C 计算几何)

    Segments

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 15335   Accepted: 4862

    Description

    Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common.

    Input

    Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n ≤ 100 showing the number of segments. After that, n lines containing four real numbers x1 y1 x2 y2 follow, in which (x1y1) and (x2y2) are the coordinates of the two endpoints for one of the segments.

    Output

    For each test case, your program must output "Yes!", if a line with desired property exists and must output "No!" otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.

    Sample Input

    3
    2
    1.0 2.0 3.0 4.0
    4.0 5.0 6.0 7.0
    3
    0.0 0.0 0.0 1.0
    0.0 1.0 0.0 2.0
    1.0 1.0 2.0 1.0
    3
    0.0 0.0 0.0 1.0
    0.0 2.0 0.0 3.0
    1.0 1.0 2.0 1.0

    Sample Output

    Yes!
    Yes!
    No!

    Source

     
    询问是否存在一条直线与所有线段相交
    暴力线段的断点构成直线,判断是否可行。
      1 //2017-08-30
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <iostream>
      5 #include <algorithm>
      6 #include <cmath>
      7 
      8 using namespace std;
      9 
     10 const int N = 110;
     11 const double EPS = 1e-8;
     12 
     13 struct Point{
     14     double x, y;
     15     Point(){}
     16     Point(double _x, double _y):x(_x), y(_y){}
     17     Point(const Point &p):x(p.x), y(p.y){}
     18     //a-b为向量ba
     19     Point operator- (const Point &b) const {
     20         return Point(x-b.x, y-b.y);
     21     }
     22     //向量叉积
     23     double operator^ (const Point &b) const {
     24         return x*b.y - y*b.x;
     25     }
     26     //向量点积
     27     double operator* (const Point &b) const {
     28         return x*b.x + y*b.y;
     29     }
     30 };
     31 
     32 struct Line{
     33     Point a, b;
     34     Line(){}
     35     Line(Point _a, Point _b):a(_a), b(_b){}
     36     void set_a_b(const Point &_a, const Point &_b){
     37         a = _a;
     38         b = _b;
     39     }
     40 }seg[N];
     41 
     42 //三态函数
     43 int sgn(double x){
     44     if(fabs(x) < EPS)return 0;
     45     if(x < 0)return -1;
     46     else return 1;
     47 }
     48 
     49 //input:Point a;Point b
     50 //output:distance a、b两点间的距离
     51 //note:该函数取名distance会编译错误
     52 double dist(Point a, Point b){
     53     return sqrt((a-b)*(a-b));
     54 }
     55 
     56 //input:l1 直线l1;l2 线段l2
     57 //output:true 直线l1与线段l2相交;false 直线l1与线段l2不想交
     58 bool seg_inter_line(Line l1, Line l2){
     59     return sgn((l2.a-l1.b)^(l1.a-l1.b))*sgn((l2.b-l1.b)^(l1.a-l1.b)) <= 0;
     60 }
     61 
     62 int n;
     63 //input:直线line
     64 //output:true 直线与所有线段都相交
     65 bool all_inter(Line line){
     66     for(int i = 0; i < n; i++)
     67           if(!seg_inter_line(line, seg[i]))
     68               return false;
     69     return true;
     70 }
     71 
     72 bool check(){
     73     Line line;
     74     for(int i = 0; i < n; i++){
     75         for(int j = 0; j < n; j++){
     76             if(i == j)continue;
     77             if(dist(seg[i].a, seg[j].a) > EPS){
     78                 line.set_a_b(seg[i].a, seg[j].a);
     79                 if(all_inter(line))return true;
     80             }
     81             if(dist(seg[i].a, seg[j].b) > EPS){
     82                 line.set_a_b(seg[i].a, seg[j].b);
     83                 if(all_inter(line))return true;
     84             }
     85             if(dist(seg[i].b, seg[j].a) > EPS){
     86                 line.set_a_b(seg[i].b, seg[j].a);
     87                 if(all_inter(line))return true;
     88             }
     89             if(dist(seg[i].b, seg[j].b) > EPS){
     90                 line.set_a_b(seg[i].b, seg[j].b);
     91                 if(all_inter(line))return true;
     92             }
     93         }
     94     }
     95     return false;
     96 }
     97 
     98 int main()
     99 {
    100     std::ios::sync_with_stdio(false);
    101     //freopen("inputC.txt", "r", stdin);
    102     int T;
    103     cin>>T;
    104     while(T--){
    105         cin>>n;
    106         for(int i = 0; i < n; i++){
    107             cin>>seg[i].a.x>>seg[i].a.y>>seg[i].b.x>>seg[i].b.y;
    108         }
    109         if(check() || n == 1 || n == 2)cout<<"Yes!"<<endl;
    110         else cout<<"No!"<<endl;
    111     }
    112 
    113     return 0;
    114 }
  • 相关阅读:
    MapReduce实例
    hadoop 分布式安装
    redis缓存
    Flink初始
    Flume初始
    大数据学习之路(持续更新中...)
    使用VisualVM分析性能
    JVM的理解
    Java日记
    UI笔记2
  • 原文地址:https://www.cnblogs.com/Penn000/p/7453891.html
Copyright © 2011-2022 走看看