[BOJ] 6580 쿼드 트리

Time Lapse :NONE

6580.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <cstring>

using namespace std;

#define MAXSIZE 600

char arr[MAXSIZE][MAXSIZE];
string bits[MAXSIZE];
string N_string;
string temp;
int N;

int ret_num(char c)
{
switch (c)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'a':
return 10;
case 'b':
return 11;
case 'c':
return 12;
case 'd':
return 13;
case 'e':
return 14;
case 'f':
return 15;
default:
break;
}
return 0;
}

void insert(int line, int start)
{
char temp2[2];
temp2[0] = temp[0];
temp2[1] = temp[1];
int hex_num = 16 * ret_num(temp2[0]) + ret_num(temp2[1]);

for (int i = start + 7; i >= start; i--)
{
if (hex_num >= pow(2, i - start))
{
hex_num -= pow(2, i - start);
arr[line][i] = 'B';
}
else
arr[line][i] = 'W';
}
}

void quard_tree(int x, int y, int n)
{
bool same = true;

for (int i = x; i < x+n; i++)
{
if (!same)
break;
for (int j = y; j < y+n; j++)
{
if (arr[i][j] != arr[x][y])
{
same = false;
break;
}
}
}

if (same)
{
cout << arr[x][y];
return;
}
cout << 'Q';
quard_tree(x, y, n/2);
quard_tree(x, y+n/2, n / 2);
quard_tree(x+n/2, y, n / 2);
quard_tree(x+n/2, y+n/2, n / 2);
}

int main(void)
{
getline(cin, N_string);
getline(cin, temp);
getline(cin, temp);


N_string = N_string.substr(23, N_string.length());
N = atoi(N_string.c_str());

for (int i = 0; i < N+1; i++)
{
getline(cin, bits[i]);
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N / 8; j++)
{
temp = bits[i].substr(2, 4);
insert(i,j*8);
bits[i] = bits[i].substr(5, bits[i].length());
}
}
cout << N << endl;
quard_tree(0,0,N);
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/6580/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.