[백준 5989] The Robot Plow (C#, C++) - soo:bak
작성일 :
문제 링크
설명
가로 X, 세로 Y 크기의 밭에서 여러 번 쟁기 작업을 합니다. 각 작업은 하나의 직사각형 범위로 주어지고, 서로 겹치는 영역이 있을 수도 있습니다.
모든 작업이 끝난 뒤, 한 번이라도 갈아엎어진 칸이 몇 개인지 구하면 됩니다.
접근법
밭을 배열로 두고, 각 명령이 가리키는 직사각형 칸을 모두 표시합니다.
같은 칸이 여러 번 포함되어도 한 번 표시된 상태로 그대로 두면 됩니다. 모든 명령을 처리한 뒤에는 표시된 칸의 개수만 세면 됩니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Linq;
class Program {
static void Main() {
int[] first = Console.ReadLine()!.Split().Select(int.Parse).ToArray();
int x = first[0];
int y = first[1];
int instructions = first[2];
bool[,] plowed = new bool[x + 1, y + 1];
for (int i = 0; i < instructions; i++) {
int[] line = Console.ReadLine()!.Split().Select(int.Parse).ToArray();
int xll = line[0];
int yll = line[1];
int xur = line[2];
int yur = line[3];
for (int cx = xll; cx <= xur; cx++) {
for (int cy = yll; cy <= yur; cy++) {
plowed[cx, cy] = true;
}
}
}
int count = 0;
for (int cx = 1; cx <= x; cx++) {
for (int cy = 1; cy <= y; cy++) {
if (plowed[cx, cy])
count++;
}
}
Console.WriteLine(count);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int x, y, instructions;
cin >> x >> y >> instructions;
vector<vector<bool>> plowed(x + 1, vector<bool>(y + 1, false));
for (int i = 0; i < instructions; i++) {
int xll, yll, xur, yur;
cin >> xll >> yll >> xur >> yur;
for (int cx = xll; cx <= xur; cx++) {
for (int cy = yll; cy <= yur; cy++) {
plowed[cx][cy] = true;
}
}
}
int count = 0;
for (int cx = 1; cx <= x; cx++) {
for (int cy = 1; cy <= y; cy++) {
if (plowed[cx][cy])
count++;
}
}
cout << count << "\n";
return 0;
}