/
1992.cpp
40 lines (37 loc) · 967 Bytes
/
1992.cpp
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
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
char pic[64][64];
int N;
bool isAll0(int x, int y, int sz) {
for (int i = x; i < x + sz; i++) {
for (int j = y; j < y + sz; j++) {
if (pic[i][j] == '1')
return false;
}
}
return true;
}
bool isAll1(int x, int y, int sz) {
for (int i = x; i < x + sz; i++) {
for (int j = y; j < y + sz; j++) {
if (pic[i][j] == '0')
return false;
}
}
return true;
}
string quadtree(int x, int y, int sz) {
if (isAll0(x, y, sz))
return "0";
if (isAll1(x, y, sz))
return "1";
return "(" + quadtree(x, y, sz / 2) + quadtree(x, y + sz / 2, sz / 2) + quadtree(x + sz / 2, y, sz / 2) + quadtree(x + sz / 2, y + sz / 2, sz / 2) + ")";
}
int main(void) {
scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%s", pic[i]);
cout << quadtree(0, 0, N);
}