알고리즘/BaekJoon 단계별로 풀어보기
[2차원 배열] 2563번 - 색종이 (c++)
jylee3
2024. 11. 3. 20:12
https://www.acmicpc.net/problem/2563
소스 코드 (c++)
#include <iostream>
#include <algorithm>
using namespace std;
void check_page(bool (*area)[100], int pos_x, int pos_y) {
for (int i = pos_x; i < pos_x + 10; i++) {
for (int j = pos_y; j < pos_y + 10; j++) {
area[i][j] = true;
}
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, x, y, width = 0;
bool area[100][100] = {false, };
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> x >> y;
check_page(area, x, y);
}
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
if (area[i][j] == true) width++;
}
}
cout << width;
}
Review
처음에는 기존 색종이 넓이를 더한 다음 겹치는 색종이를 빼려고 하였으나 매우 복잡하다는 것을 깨닫고 다른 방식으로 생각을 해보았다.
전체 도화지의 격자 중 색종이가 존재하는 위치를 모두 true로 표시한 후, 최종적으로 true인 격자만 세어 넓이를 1씩 더해주는 방식으로 계산했다.