알고리즘 문제풀이/백준
[백준 1992번] 쿼드트리
슥지니
2021. 12. 27. 11:37
반응형
문제 링크: https://www.acmicpc.net/problem/1992
1992번: 쿼드트리
첫째 줄에는 영상의 크기를 나타내는 숫자 N 이 주어진다. N 은 언제나 2의 제곱수로 주어지며, 1 ≤ N ≤ 64의 범위를 가진다. 두 번째 줄부터는 길이 N의 문자열이 N개 들어온다. 각 문자열은 0 또
www.acmicpc.net
<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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <utility>
#include <climits>
#include <cmath>
#include <deque>
using namespace std;
char board[64][64];
int res[2];
// x,y에서 시작해서 가로 세로 길이가 각각 n인 영상
void solve(int x, int y, int n) {
if (n == 1) {
cout << board[x][y];
return;
}
char flag = board[x][y];
bool run = false;
for (int i = x; i < x + n; i++) {
for (int j = y; j < y + n; j++) {
if (flag != board[i][j]) run = true;
}
}
if (!run) {
cout << board[x][y];
return;
}
cout << "(";
solve(x, y, n / 2);
solve(x, y + n / 2, n / 2);
solve(x + n / 2, y, n / 2);
solve(x + n / 2, y + n / 2, n / 2);
cout << ")";
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
for (int i = 0; i < n; i++) {
string s; cin >> s;
int s_len = s.length();
for (int j = 0; j < s_len; j++) {
board[i][j] = s[j];
}
}
solve(0, 0, n);
return 0;
}
|
cs |
반응형