[BaekJoon] #2563 - 색종이 [Java][C++]
[BaekJoon] #2563 - 색종이 [Java][C++]
1. 아이디어
주어진 도화지를 2차원 배열로, 색종이를 붙이는 것을 이 2차원 배열에 대한 방문 체크로 해석하여 방문한 칸의 개수를 세면 색종이가 붙은 검은 영역의 넓이를 구할 수 있다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(1)$ |
($N$ = 입력값 n)
3. 코드
풀이 [Java][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
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
boolean[][] vis = new boolean[100][100];
int n = Integer.parseInt(br.readLine());
while (n-- > 0) {
st = new StringTokenizer(br.readLine());
int row = Integer.parseInt(st.nextToken());
int col = Integer.parseInt(st.nextToken());
for (int r = row; r < row + 10; r++) {
for (int c = col; c < col + 10; c++) {
vis[r][c] = true;
}
}
}
int sum = 0;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
if (vis[i][j]) sum++;
}
}
System.out.println(sum);
}
}
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
#include <bits/stdc++.h>
using namespace std;
bool vis[100][100];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
while (n--) {
int row, col;
cin >> row >> col;
for (int r = row; r < row + 10; r++) {
for (int c = col; c < col + 10; c++) {
vis[r][c] = true;
}
}
}
int sum = 0;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
if (vis[i][j]) sum++;
}
}
cout << sum;
}
This post is licensed under CC BY 4.0 by the author.