
백준 1992번 : 쿼드트리 (https://www.acmicpc.net/problem/1992)

가장 처음의 맵이 모두 흰색(0)이거나, 모두 검은색(1)이 아니기 때문에 압축할 수 없다.
따라서, 맵을 4등분한다. (위의 그림에서 빨간색 라인)
4등분된 맵에서 각각의 구간을 살펴보면, 왼쪽 위 구간은 모두 0이기 때문에 압축이 가능하다.
따라서, 하나의 0으로 표현합니다.
오른쪽 위 구간은 모두 흰색(0)이거나, 모두 검은색(1)이 아니기 때문에 압축할 수 없다.
따라서, 맵을 다시 4등분합니다.
모든 구간들을 계속 반복합니다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int map[65][65];
void compress(int n, int y, int x)
{
if (n == 1)
{
cout << map[y][x];
return;
}
bool exist0 = false;
bool exist1 = false;
for (int i = y; i < y + n; i++)
{
for (int j = x; j < x + n; j++)
{
if (map[i][j] == 0)
exist0 = true;
else
exist1 = true;
if (exist0 && exist1)
break;
}
if (exist0 && exist1)
break;
}
if (exist0 && exist1)
{
cout << '(';
compress(n / 2, y, x); // 2사분면
compress(n / 2, y, x + n / 2); // 1사분면
compress(n / 2, y + n / 2, x); // 3사분면
compress(n / 2, y + n / 2, x + n / 2); // 4사분면
cout << ')';
}
else if (exist0)
cout << 0;
else
cout << 1;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
string str;
cin >> str;
for (int j = 0; j < n; j++)
{
map[i][j] = str[j] - '0';
}
}
compress(n, 0, 0);
}
참고
https://velog.io/@doorbals_512/UNSEEN-%ED%85%8C%EC%8A%A4%ED%8A%B8-%EB%8C%80%EB%B9%84-%EA%B2%8C%EC%9E%84-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98
https://pangtrue.tistory.com/62