poj 1389 Area of Simple Polygons

离散化处理:

View Code
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<algorithm>

#include<cmath>

#include<queue>

#include<set>

#include<map>

#include<cstring>

#include<vector>

#include<string>

#define LL long long

using namespace std;

class Rec

{

public:

      int x1,y1,x2,y2;    

}rec[1024];

int tx[2024];

bool cmp1( Rec a, Rec b )

{

     return a.y1 < b.y1;    

}

bool cmp2( int a , int b )

{

     return a < b;

}

int Solve( int n , int  cnt )

{

    int s = 0;

    sort( tx , tx + cnt , cmp2 );

    sort( rec , rec + n , cmp1 );

    cnt = unique( tx , tx + cnt ) - tx;

    for( int i = 1 ; i < cnt ; i ++ )

    {

        int l = tx[i-1],r = tx[i],up = -1,down = -1;

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

        {

             if( l >= rec[j].x1 && r <= rec[j].x2 )

             {

                 if( up < rec[j].y1 )

                 {

                     s += ( r - l )*( up - down );

                     up = rec[j].y2 ; down = rec[j].y1;        

                 }

                 else if( up < rec[j].y2 )

                          up = rec[j].y2;        

             }

        }

        s += ( r - l )*( up - down );

    }

    return s;

}

int main(  )

{

    while( 1 )

    {

         int n = 1,cnt = 0;

         scanf( "%d%d%d%d",&rec[0].x1,&rec[0].y1,&rec[0].x2,&rec[0].y2 );

         tx[cnt++] = rec[0].x1 ; tx[cnt++] = rec[0].x2;

         if( rec[0].x1 == -1 ) break;

         

         while( scanf( "%d%d%d%d",&rec[n].x1,&rec[n].y1,&rec[n].x2,&rec[n].y2 ),rec[n].x1 !=-1 )

         {

                tx[cnt++] = rec[n].x1 ; tx[cnt++] = rec[n].x2;

                n++;

//                printf( "n == %d",n );

         }

         printf( "%d\n",Solve( n , cnt ) );    

    }

    //system( "pause" );

    return 0;

}

你可能感兴趣的:(simple)