With given coordinates of two billboards and a truck, count total combined area of both billboards that remains visible.
My 1st Correct Code
#include <iostream>
int main() {
int b1[4], b2[4], t[4];
int result = 0;
scanf("%d %d %d %d", &b1[0], &b1[1], &b1[2], &b1[3]);
scanf("%d %d %d %d", &b2[0], &b2[1], &b2[2], &b2[3]);
scanf("%d %d %d %d", &t[0], &t[1], &t[2], &t[3]);
result += (b1[2] - b1[0]) * (b1[3] - b1[1]);
result += (b2[2] - b2[0]) * (b2[3] - b2[1]);
//1st
if (t[0] < b1[2] && t[1] < b1[3] && t[2] > b1[0] && t[3] > b1[1]) {
int inv_w = 0; int inv_h = 0;
//width
if (b1[0] <= t[0]) {
if (t[2] <= b1[2])
inv_w = t[2] - t[0];
else
inv_w = b1[2] - t[0];
}
else {
if (t[2] <= b1[2])
inv_w = t[2] - b1[0];
else inv_w = b1[2] - b1[0];
}
//height
if (b1[1] <= t[1]) {
if (t[3] <= b1[3])
inv_h = t[3] - t[1];
else
inv_h = b1[3] - t[1];
}
else {
if (t[3] <= b1[3])
inv_h = t[3] - b1[1];
else inv_h = b1[3] - b1[1];
}
int inv = inv_w * inv_h;
result -= inv;
}
//2nd
if (t[0] < b2[2] && t[1] < b2[3] && t[2] > b2[0] && t[3] > b2[1]) {
int inv_w = 0; int inv_h = 0;
//width
if (b2[0] <= t[0]) {
if (t[2] <= b2[2])
inv_w = t[2] - t[0];
else
inv_w = b2[2] - t[0];
}
else {
if (t[2] <= b2[2])
inv_w = t[2] - b2[0];
else inv_w = b2[2] - b2[0];
}
//height
if (b2[1] <= t[1]) {
if (t[3] <= b2[3])
inv_h = t[3] - t[1];
else
inv_h = b2[3] - t[1];
}
else {
if (t[3] <= b2[3])
inv_h = t[3] - b2[1];
else inv_h = b2[3] - b2[1];
}
int inv = inv_w * inv_h;
result -= inv;
}
printf("%d", result);
}
comments
2020-01-17 roWjsek
Solution
#include <iostream>
using namespace std;
int arr[2002][2002];
int main() {
// your code goes here
int ax1, ay1, ax2, ay2;
int bx1, by1, bx2, by2;
int cx1, cy1, cx2, cy2;
cin >> ax1 >> ay1 >> ax2 >> ay2 >> bx1 >> by1 >> bx2 >> by2 >> cx1 >> cy1 >> cx2 >> cy2;
ax1 += 1000;
ay1 += 1000;
ax2 += 1000;
ay2 += 1000;
bx1 += 1000;
by1 += 1000;
bx2 += 1000;
by2 += 1000;
cx1 += 1000;
cy1 += 1000;
cx2 += 1000;
cy2 += 1000;
for(int i=ax1;i<ax2;i++){
for(int j=ay1;j<ay2;j++){
arr[i][j] = 1;
}
}
for(int i=bx1;i<bx2;i++){
for(int j=by1;j<by2;j++){
arr[i][j] = 1;
}
}
for(int i=cx1;i<cx2;i++){
for(int j=cy1;j<cy2;j++){
arr[i][j] = 2;
}
}
int ans = 0;
for(int i=0;i<=2000;i++){
for(int j=0;j<=2000;j++){
if(arr[i][j]==1) ans++;
}
}
printf("%d", ans);
return 0;
}
