码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Rectangle Area

时间:2015-06-13 11:26:51      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:rectangle   area   

LeetCode Rectangle Area

题目

技术分享

思路

刚开始自己写别提WA多少遍了;
后来看到标达真的被惊讶到了;
代码可以这么美;

代码

自己的

#define min(A, B) (A > B ? B : A)

bool pointInRectangle(int px, int py, int ax, int ay, int bx, int by) {
    return ax <= px && px <= bx && ay <= py && py <= by;
}

int computeAreaSub(int A, int B, int C, int D, int E, int F, int G, int H) {
    int area1 = (D - B) * (C - A), area2 = (H - F) * (G - E);
    if (pointInRectangle(A, B, E, F, G, H)      // left down
        && pointInRectangle(A, D, E, F, G, H)      // left up
        && pointInRectangle(C, D, E, F, G, H)      // right up
        && pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area2;
    }
    if (pointInRectangle(A, B, E, F, G, H)      // left down
        && pointInRectangle(A, D, E, F, G, H)      // left up
        && !pointInRectangle(C, D, E, F, G, H)      // right up
        && !pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (D - B) * (G - A);
    }
    if (!pointInRectangle(A, B, E, F, G, H)      // left down
        && !pointInRectangle(A, D, E, F, G, H)      // left up
        && pointInRectangle(C, D, E, F, G, H)      // right up
        && pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (D - B) * (C - E);
    }
    if (!pointInRectangle(A, B, E, F, G, H)      // left down
        && pointInRectangle(A, D, E, F, G, H)      // left up
        && pointInRectangle(C, D, E, F, G, H)      // right up
        && !pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (D - F) * (C - A);
    }
    if (pointInRectangle(A, B, E, F, G, H)      // left down
        && !pointInRectangle(A, D, E, F, G, H)      // left up
        && !pointInRectangle(C, D, E, F, G, H)      // right up
        && pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (H - B) * (C - A);
    }
    if (pointInRectangle(A, B, E, F, G, H)      // left down
        && !pointInRectangle(A, D, E, F, G, H)      // left up
        && !pointInRectangle(C, D, E, F, G, H)      // right up
        && !pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (G - A) * (H - B);
    }
    if (!pointInRectangle(A, B, E, F, G, H)      // left down
        && !pointInRectangle(A, D, E, F, G, H)      // left up
        && pointInRectangle(C, D, E, F, G, H)      // right up
        && !pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (C - E) * (D - F);
    }
    if (!pointInRectangle(A, B, E, F, G, H)      // left down
        && pointInRectangle(A, D, E, F, G, H)      // left up
        && !pointInRectangle(C, D, E, F, G, H)      // right up
        && !pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (D - F) * (G - A);
    }
    if (!pointInRectangle(A, B, E, F, G, H)      // left down
        && !pointInRectangle(A, D, E, F, G, H)      // left up
        && !pointInRectangle(C, D, E, F, G, H)      // right up
        && pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (H - B) * (C - E);
    }
    return area1 + area2;
}

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    int temp1 = computeAreaSub(A, B, C, D, E, F, G, H);
    int temp2 = computeAreaSub(E, F, G, H, A, B, C, D);
    return min(computeAreaSub(A, B, C, D, E, F, G, H), computeAreaSub(E, F, G, H, A, B, C, D));
}

Discuss的

#define min(A, B) (A > B ? B : A)
#define max(A, B) (A > B ? A : B)

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    int a = max(A, E), b = max(B, F), c = min(C, G), d = min(D, H);
    return (C - A) * (D - B) + (G - E) * (H - F) - ((a < c && b < d) ? (c - a) * (d - b) : 0);
}

LeetCode Rectangle Area

标签:rectangle   area   

原文地址:http://blog.csdn.net/u012925008/article/details/46480641

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!