矩形绘制
标题:矩形绘制 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
实现一个简单的绘图模块,绘图模块仅支持矩形的绘制和擦除,当新绘制的矩形与之前的图形重叠时,对图形取并集;当新擦除的矩形与之前的图形重叠时,对图形取差集。给定一系列矩形的绘制和擦除操作,计算最终图形的面积。
下面给出示例1和示例2的图示
n = int(input()) xy = [] for i in range(201): xy.append([0]* 201) for i in range(n): s = input().split() x1,y1,x2,y2, = map(int, s[1:]) x1 += 100 y1 += 100 x2 += 100 y2 += 100 for x in range(x1 ,x2): for y in range(y2, y1): if s[0] == 'd': xy[x][y] = 1 else: xy[x][y] = 0 ans = 0 for i in range(201): ans += sum(xy[i]) print(ans)
#include <iostream>
#include <vector>
using namespace std;
typedef vector<vector<bool>> plot;
void padding(plot & UCS, char op, int left, int up, int right, int down) {
bool manipulate = (op == 'd' ? true : false);
for (int i = up; i > down; --i) {
for (int j = left; j < right; ++j) {
UCS[i][j] = manipulate;
}
}
}
int getArea(plot UCS) {
int area = 0;
for (auto i : UCS) {
for (auto j : i) {
if (j == true) {
area += 1;
}
}
}
return area;
}
int input() {
int area, amount;
cin >> amount;
plot UCS(201, vector<bool>(201, false));
for (int i = 0; i < amount; ++i) {
char op;
int left, right, up, down;
cin >> op >> left >> up >> right >> down;
left += 100, up += 100, right += 100, down += 100;
padding(UCS, op, left, up, right, down);
}
area = getArea(UCS);
return area;
}
int main() {
cout << input() << endl;
return 0;
}
import java.util.Scanner;
public class Main{
static int[][] vis = new int[205][205];
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i = 1;i<=n;i++){
char op = scanner.next().charAt(0);
int x1 = scanner.nextInt();
int y1 = scanner.nextInt();
int x2 = scanner.nextInt(),y2 = scanner.nextInt();
x1+=100;y1+=100;x2+=100;y2+=100;
for(int j = x1+1;j<=x2;j++){
for(int k = y2+1;k<=y1;k++){
if(op=='d')vis[j][k]=1;
else
vis[j][k]=0;
}
}
}
int ans = 0;
for(int i = 0;i<=200;i++){
for(int j = 0;j<=200;j++){
if(vis[i][j]==1)ans++;
}
}
System.out.println(ans);
}
}
快手公司福利 1244人发布