poj 2007 Scrambled Polygon

这个题就是求凸包;

极角排序:

View Code
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 Point
{
public:
     double x,y;    
}point[54];
int dcmp( double x )
{
   if( fabs( x ) <1.0e-8 ) return 0;
   if( x < 0 ) return -1;
   return 1;    
}
double multi( Point p1 ,Point p2 ,Point q )
{
   return ( p1.x - q.x )*( p2.y - q.y )-( p2.x - q.x )*( p1.y - q.y );     
}
double Distance( Point a , Point b )
{
   return sqrt(( a.x - b.x )*( a.x - b.x ) + ( a.y - b.y )*( a.y - b.y ));    
}
bool cmp( Point a , Point b )
{
     int t = dcmp( multi ( a , b , point[0]) );
     if( t > 0 ) return true;
     return false;
}

int main(  )
{
    int cnt = 1;
    scanf( "%lf %lf" ,&point[0].x ,&point[0].y); 
    while( scanf( "%lf %lf",&point[cnt].x,&point[cnt].y )!=EOF )
    {
        cnt++;
        while( scanf( "%lf %lf",&point[cnt].x ,&point[cnt].y ) != EOF &&(point[cnt].x||point[cnt].y))
        { 
            cnt++;
           
        }
        sort( point + 1 , point + cnt  , cmp );
        for( int i = 0 ; i < cnt ;i ++ )
         printf( "(%.0f,%.0f)\n",point[i].x,point[i].y );
        cnt = 1;
        point[0].x = 0 ; point[0].y = 0;    
    }    
    //system( "pause" );
    return 0;
}

水平排序:

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 Point
{
public:
     double x,y;    
}point[54];
int dcmp( double x )
{
   if( fabs( x ) <1.0e-8 ) return 0;
   if( x < 0 ) return -1;
   return 1;    
}
bool cmp( Point a , Point b )
{
     if( a.y == b.y ) return a.x < b.x;
     return a.y < b.y;    
}
double multi( Point p1 ,Point p2 ,Point q )
{
   return ( p1.x - q.x )*( p2.y - q.y )-( p2.x - q.x )*( p1.y - q.y );     
}
void Graham( int n )
{
   Point p[54];
   int len = 0,i; 
   for( i = 0 ; i < n ; i ++ )
   {
      while( len >= 2 && dcmp(multi( p[len-1],point[i],p[len-2] )) < 0 )    
             len --;
      p[len++] = point[i];
   }    
   int t = len + 1;
   for( i = n -2 ; i >=0 ; i --  )
   {
       while( len >= t && dcmp(multi( p[len-1],point[i],p[len-2] )) < 0 )
              len --;
       p[len++] = point[i];
   }
  
   for( i = 0 ; i < len ; i ++ )
   {
        if( dcmp( p[i].x )==0 &&dcmp( p[i].y  ) == 0 )
             break;        
   }
   len -- ;
   printf( "(0,0)\n" ); 
   t = i + 1;
   while( t != i )
   {
       printf( "(%.0f,%.0f)\n",p[t].x,p[t].y );
       t++;
       t %= len;        
   }
}
int main(  )
{
    int cnt = 1;
    scanf( "%lf %lf" ,&point[0].x ,&point[0].y); 
    while( scanf( "%lf %lf",&point[cnt].x,&point[cnt].y )!=EOF )
    {
        cnt++;
        while( scanf( "%lf %lf",&point[cnt].x ,&point[cnt].y ) != EOF &&(point[cnt].x||point[cnt].y))
        {
            cnt++;
        }
        sort( point , point + cnt  , cmp );
        Graham( cnt  );
        cnt = 1;
        point[0].x = 0 ; point[0].y = 0;    
    }    
    //system( "pause" );
    return 0;
}

 

 

 

posted @ 2012-07-11 21:19  wutaoKeen  阅读(120)  评论(0编辑  收藏  举报