zoukankan      html  css  js  c++  java
  • POJ 1389 Area of Simple Polygons

    POJ_1389

        这个题目和POJ_1151基本是一样的,一些具体的思路可以参考我的那篇题解:http://www.cnblogs.com/staginner/archive/2012/02/20/2359396.html

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #define MAXD 2010
    #define zero 1e-8
    struct square
    {
    double x1, y1, x2, y2;
    }s[MAXD];
    int N, M, X;
    double tx[MAXD], left[MAXD], right[MAXD];
    int cmp1(const void *_p, const void *_q)
    {
    square *p = (square *)_p, *q = (square *)_q;
    return p->y1 < q->y1 ? -1 : 1;
    }
    int cmp2(const void *_p, const void *_q)
    {
    double *p = (double *)_p, *q = (double *)_q;
    return *p < *q ? -1 : 1;
    }
    double fabs(double x)
    {
    return x < 0 ? -x : x;
    }
    int dcmp(double x)
    {
    return fabs(x) < zero ? 0 : (x < 0 ? -1 : 1);
    }
    void readin()
    {
    scanf("%lf%lf%lf%lf", &s[N].x1, &s[N].y1, &s[N].x2, &s[N].y2);
    tx[X ++] = s[N].x1;
    tx[X ++] = s[N].x2;
    }
    void prepare()
    {
    int i, j, k;
    qsort(s, N, sizeof(s[0]), cmp1);
    qsort(tx, X, sizeof(tx[0]), cmp2);
    M = 0;
    for(i = 1; i < X; i ++)
    if(dcmp(tx[i] - tx[i - 1]) != 0)
    {
    left[M] = tx[i - 1], right[M] = tx[i];
    ++ M;
    }
    }
    void solve()
    {
    int i, j, k;
    double ans = 0, up, down;
    prepare();
    for(i = 0; i < M; i ++)
    {
    up = down = -1;
    for(j = 0; j < N; j ++)
    if(dcmp(left[i] - s[j].x1) >= 0 && dcmp(right[i] - s[j].x2) <= 0)
    {
    if(dcmp(s[j].y1 - up) > 0)
    {
    ans += (up - down) * (right[i] - left[i]);
    down = s[j].y1, up = s[j].y2;
    }
    else if(dcmp(s[j].y2 - up) > 0)
    up = s[j].y2;
    }
    ans += (up - down) * (right[i] - left[i]);
    }
    printf("%.0lf\n", ans);
    }
    int main()
    {
    for(;;)
    {
    N = X = 0;
    readin();
    if(dcmp(s[N].x1 + 1) == 0)
    break;
    ++ N;
    for(;;)
    {
    readin();
    if(dcmp(s[N].x1 + 1) == 0)
    break;
    ++ N;
    }
    solve();
    }
    return 0;
    }


  • 相关阅读:
    第十五讲 实例模式
    第十四讲 实例模式
    将博客搬至CSDN
    CSU 1616: Heaps(区间DP)
    hdu 1281棋盘游戏(二分匹配)
    hdu 1042 N!(大数的阶乘)
    hdu 3371 Connect the Cities (最小生成树Prim)
    hdu 4502吉哥系列故事——临时工计划 (简单DP)
    hdu1230火星A+B (大数题)
    hdu1301 Jungle Roads (Prim)
  • 原文地址:https://www.cnblogs.com/staginner/p/2360516.html
Copyright © 2011-2022 走看看