poj 3304 Segments

这个题就是纯暴力枚举:枚举所有顶点的连线的直线与所有线段是否有交点;

View Code
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<algorithm>

#include<cmath>

#include<queue>

#include<set>

#include<map>

#include<cstring>

#include<vector>

using namespace std;

class Line

{

public:

      double x1,x2,y1,y2;

}line[124],L[4];

int dcmp( double x )

{

   if( fabs( x ) < 1.0e-8 ) return 0;

   if( x < 0 ) return -1;

   return 1;    

}

double multi( double x1 ,double x2 ,double y1 ,double y2 )

{

    return x1 * y2 - x2 * y1;    

}

int segment( Line a , Line b )

{

   int d1 = dcmp( multi( a.x1 - b.x1 , a.x2 - b.x1 ,a.y1 - b.y1,a.y2 - b.y1 ) );

   int d2 = dcmp( multi( a.x1 - b.x2 , a.x2 - b.x2 ,a.y1 - b.y2,a.y2 - b.y2 ) );

   return d1 * d2;    

}

bool Solve( int n )

{

    for( int i = 0 ; i < n ; i ++ )

    {

       for( int j = 0 ; j < n  ; j ++  )

       {

          int cnt = 0;

          if( dcmp(line[i].x1 - line[j].x1) || dcmp( line[i].y1 - line[j].y1 ))

          {

              L[cnt].x1 = line[i].x1;L[cnt].y1 = line[i].y1;

              L[cnt].x2 = line[j].x1;L[cnt++].y2 = line[j].y1;

          }

          if( dcmp(line[i].x1 - line[j].x2) || dcmp( line[i].y1 - line[j].y2 ) )

          {

              L[cnt].x1 = line[i].x1;L[cnt].y1 = line[i].y1;

              L[cnt].x2 = line[j].x2;L[cnt++].y2 = line[j].y2;

          }

          if( dcmp(line[i].x2 - line[j].x1) || dcmp( line[i].y2 - line[j].y1 ) )

          {

              L[cnt].x1 = line[i].x2;L[cnt].y1 = line[i].y2;

              L[cnt].x2 = line[j].x1;L[cnt++].y2 = line[j].y1;

          }

          if(dcmp(line[i].x2 - line[j].x2) || dcmp( line[i].y2 - line[j].y2 ))

          {

              L[cnt].x1 = line[i].x2;L[cnt].y1 = line[i].y2;

              L[cnt].x2 = line[j].x2;L[cnt++].y2 = line[j].y2;

          }

          for( int l = 0 ; l < cnt ; l ++ )

          {

              int k;

              for( k = 0; k < n ; k ++ )

              {

                  if( segment(L[l] , line[k]) > 0 )

                    break;        

              }    

              if( k == n ) return true;

          }    

       }

    }    

    return false;

}

int main(  )

{

    int N,n;

    while( scanf( "%d",&N )==1 )

    {

      while( N-- )

      {

           scanf( "%d",&n );

           for( int i = 0 ; i < n ; i++ )

           {

               scanf( "%lf %lf %lf %lf",&line[i].x1,&line[i].y1,&line[i].x2,&line[i].y2 );        

           }    

           if( Solve( n ) ) printf( "Yes!\n" );

           else printf( "No!\n" );

      }

    }

    //system( "pause" );

    return 0;

}

 

你可能感兴趣的:(poj)