https://www.acmicpc.net/problem/1992
1992번: 쿼드트리
첫째 줄에는 영상의 크기를 나타내는 숫자 N 이 주어진다. N 은 언제나 2의 제곱수로 주어지며, 1 ≤ N ≤ 64의 범위를 가진다. 두 번째 줄부터는 길이 N의 문자열이 N개 들어온다. 각 문자열은 0 또
www.acmicpc.net
왼쪽 위, 오른쪽 위, 왼쪽 아래, 오른쪽 아래의 좌표는 아래와 같다
(x, y), (x, y + n/2), (x + n/2, y), (x + n/2, y + n/2)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int video[64][64];
vector <char> ans;
void printVideo(int n, int x, int y) {
ans.push_back('(');
int difcnt =0;
pair<int, int> entry[4] = {{x, y}, {x, y + n/2}, {x+n/2, y}, {x+n/2, y + n/2}};
for(int p = 0; p < 4; p++){
int nx = entry[p].first;
int ny = entry[p].second;
int num = video[nx][ny];
bool flag = false;
for(int i = nx; i < nx + n/2; i++){
for(int j = ny; j < ny + n/2; j++){
if(video[i][j] != num){
flag = true;
break;
}
}
if(flag) break;
}
if(!flag) {
ans.push_back(num + '0');
}else{
printVideo(n/2, nx, ny);
}
}
ans.push_back(')');
}
int main(){
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// cout.tie(NULL);
int n;
cin >> n;
int start;
bool flag = false;
for(int i = 0; i < n; i++){
string s;
cin >> s;
for(int j =0; j < n; j++){
video[i][j] = s[j] - '0';
if(i==0 && j==0){
start = video[0][0];
}
else{
if(video[i][j]!= start && !flag){
flag = true;
}
}
}
}
if(!flag){
char t = video[0][0] + '0';
cout << t ;
}else{
printVideo(n, 0,0);
for(int i=0; i< ans.size(); i++){
cout << ans[i];
}
}
}
728x90
'코테 > 백준 문제풀이' 카테고리의 다른 글
백준 1937 욕심쟁이 판다 (0) | 2023.05.29 |
---|---|
백준 Z C++ (0) | 2023.05.28 |
백준 공유기 설치 (0) | 2023.05.27 |
백준 중량제한 C++ (0) | 2023.05.26 |
백준 치킨배달 (0) | 2023.01.11 |