hdu1542 Atlantis

 离散化的题目,和之前的大同小异

 

#include <iostream>
using namespace std;
const int size = 110;
double x[2*size], y[2*size];
double ax[size], bx[size], ay[size], by[size];
bool map1[2*size][2*size];
bool cmp(double a, double b)
{
     return a < b;
}
int n;
int BinaryS(double ans, int st, int ed, double *x)//二分搜索查找每一个点的映射位置
{
    int left = st, right = ed;
    while (left <= right){
          int mid = (left+right)/2;
          if (x[mid] == ans){ return mid;}
          if (x[mid] < ans){
             left = mid+1;          
          }     
          if (x[mid] > ans){
             right = mid-1;          
          }
    }
}
int main()
{
    int w, h;
    int nc = 0;
    int n;
    while (scanf("%d", &n) && n){
          int nx, ny;
          nx = ny = 0;
          int ii1 = 0;
         for (int i = 0; i < n; i ++){
                scanf("%lf%lf%lf%lf", &ax[ii1], &ay[ii1], &bx[ii1], &by[ii1]);
                if (ax[ii1] > bx[ii1])swap(ax[ii1], bx[ii1]);
                if (ay[ii1] > by[ii1])swap(ay[ii1], by[ii1]);
                x[nx ++] = ax[ii1], y[ny ++] = ay[ii1];//将点依次分为x、y记录下来
                x[nx ++] = bx[ii1], y[ny ++] = by[ii1];
                ii1 ++;
          }
          int xx = 0, yy = 0;
          for (int i = 1; i < nx; i ++){//去除相邻的x相等的情况
              if (x[i] != x[i-1]){
                 xx ++;
                 x[xx] = x[i];        
              }
          }
          for (int i = 1; i < ny; i ++){//去除相邻的y相等的情况
              if (y[i] != y[i-1]){
                 yy ++;
                 y[yy] = y[i];        
              }   
          }
          sort(x, x+xx+1, cmp);//对x、y从小到大排序
          sort(y, y+yy+1, cmp);
          memset(map1, false, sizeof(map1));
          for (int i = 0; i < ii1; i ++){
              int sx = BinaryS(ax[i], 0, xx, x);//二分搜索查找每次输入的点的位置
              int sy = BinaryS(ay[i], 0, yy, y);
              int ex = BinaryS(bx[i], 0, xx, x);
              int ey = BinaryS(by[i], 0, yy, y);
              for (int ii = sx; ii < ex; ii ++){//进行映射
                  for (int j = sy; j < ey; j ++){
                      map1[ii][j] = true;
                  }   
              }
          }
          double sum = 0;
          for (int i = 0; i < xx; i ++){
              for (int j = 0; j < yy; j ++){
                  if (map1[i][j]){ //计算
                     sum += (x[i+1]-x[i])*(y[j+1]-y[j]);
                  }
              }   
          }
          printf("Test case #%d\n", ++nc);
          printf("Total explored area: %.2lf\n\n", sum);
    }
    return 0;
}

你可能感兴趣的:(hdu1542 Atlantis)