[백준] 2563번 - 색종이 [Java][C++]
[백준] 2563번 - 색종이 [Java][C++]
1. 문제 풀이
주어진 도화지를 2차원 배열로, 색종이를 붙이는 것을 이 2차원 배열에 대한 방문 체크로 해석하여 방문한 칸의 개수를 세면 색종이가 붙은 검은 영역의 넓이를 구할 수 있다.
2. 코드
1. 구현 [Java]
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[][] visited = new boolean[100][100];
int N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
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++) {
visited[r][c] = true;
}
}
}
int sum = 0;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
if (visited[i][j]) sum++;
}
}
System.out.println(sum);
}
}
2. 구현 [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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
bool visited[100][100] = {};
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int row, col;
cin >> row >> col;
for (int r = row; r < row + 10; r++) {
for (int c = col; c < col + 10; c++) {
visited[r][c] = true;
}
}
}
int sum = 0;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
if (visited[i][j]) sum++;
}
}
cout << sum;
}
3. 풀이 정보
1. 구현 [Java]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| Java 11 | 104 ms | 14120 KB | 945 B |
2. 구현 [C++]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| C++ 17 | 0 ms | 2020 KB | 594 B |
This post is licensed under CC BY 4.0 by the author.