poj 1151 Atlantis
用离散化处理就可以了:
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 Point { public: double x1,y1,x2,y2; }p[124]; double tx[224]; bool cmp1( double a, double b ) { return a < b; } bool cmp2( Point a , Point b ) { return a.y1 < b.y1; } double Solve( int N ,int cnt ) { double s = 0; sort( tx , tx + cnt , cmp1 ); sort( p , p + N , cmp2 ); cnt = unique( tx, tx + cnt ) - tx; for( int i = 1 ; i <cnt ; i ++ ) { double l = tx[i-1],r = tx[i],up = -1.0,down = -1.0; for( int j = 0 ; j < N; j ++ ) { if( l >= p[j].x1 && r <= p[j].x2 ) { if( p[j].y1 > up ) { s += ( r - l )*( up - down ); up = p[j].y2 ; down = p[j].y1; } else if( up < p[j].y2 ) up = p[j].y2; } // printf( "s = %lf %lf %lf\n",s,l,r ); } s += ( r - l )*( up - down ); } return s; } int main( ) { int Case = 1,N; while( scanf( "%d",&N ),N ) { int cnt = 0; for( int i = 0 ; i < N; i ++ ) { scanf( "%lf%lf%lf%lf",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2 ); tx[cnt++] = p[i].x1; tx[cnt++] = p[i].x2; } printf( "Test case #%d\n",Case++ ); printf( "Total explored area: %.2f\n\n",Solve( N ,cnt) ); } //system( "pause" ); return 0; }