AcWing 1761.阻挡广告牌

题意

给出三个矩形的左下角和右上角的坐标,前两个矩形代表广告牌,第三个矩形代表卡车,卡车可能会遮挡广告牌,求两个广告牌未被遮挡的面积的和。

输入格式

第一行包含四个整数 x1,y1,x2,y2,其中 (x1,y1) 和 (x2,y2) 表示在贝茜的二维视野中,第一个广告牌的左下角和右上角坐标。
第二行按照如上形式,包含四个整数,表示第二个广告牌的左下角和右上角坐标。
第三行按照如上形式,包含四个整数,表示卡车侧面的左下角和右上角坐标。

输出格式

输出两个广告牌的仍然可见的总面积。

数据范围

−1000≤x1,y1,x2,y2≤1000,
保证两个广告牌之间重叠面积为 0

题解

  • intersect函数写得很妙,用来求两个矩形面积的交
  • 结构体开得很简洁
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

struct Rect{
    int x1, y1, x2, y2;
    int area() {
        return (y2 - y1) * (x2 - x1);
    }
}a, b, t;

int intersect(Rect a, Rect b){
    int xOverlap = max(0, min(a.x2, b.x2) - max(a.x1, b.x1));
    int yOverlap = max(0, min(a.y2, b.y2) - max(a.y1, b.y1));
    return xOverlap * yOverlap;
}

int main()
{
    scanf("%d%d%d%d", &a.x1, &a.y1, &a.x2, &a.y2);
    scanf("%d%d%d%d", &b.x1, &b.y1, &b.x2, &b.y2);
    scanf("%d%d%d%d", &t.x1, &t.y1, &t.x2, &t.y2);

    printf("%d\n", a.area() + b.area() - intersect(a, t) - intersect(b, t));
    return 0;
}
posted @   小菜珠的成长之路  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示