1992 - 쿼드트리

Solution

code

#include <iostream>
using namespace std;

bool compressable(int ** arr, int row, int col, int size)
{
	int elem = arr[row][col];
	for(int i = row; i < row+size; i++)
	{
		for(int j = col; j < col+size; j++)
		if(arr[i][j] != elem) return false;
	}
	return true;
}

void compress(int ** arr, int row, int col, int size)
{
	if(compressable(arr, row, col, size)) cout << arr[row][col];
	else
	{
		cout << "(";
		int sub_size = size / 2;
		compress(arr, row, col, sub_size);
		compress(arr, row, col+sub_size, sub_size);
		compress(arr, row+sub_size, col, sub_size);
		compress(arr, row+sub_size, col+sub_size, sub_size);
		cout << ")";
	}
} 

int main()
{
	int N;
	cin >> N;
	int ** arr = new int*[N];
	for(int i = 0; i < N; i++)
	{
		arr[i] = new int[N];
		for(int j = 0; j < N; j++)
		{
			char c;
			cin >> c;
			arr[i][j] = c - '0';
		}
	}
	compress(arr, 0, 0, N); 
	return 0;
}

ref

1992번: 쿼드트리