5 분 소요

🗒️ 1992번) 쿼드트리

#include <iostream>
using namespace std;

char board[64][64];

void Func(int n, int base_r, int base_c)
{
	if (n == 0)
		return;
	
	bool isSame = true;
	char num = board[base_r][base_c];
	for (int r = 0; (r < n) && isSame; r++)
	{ 
		for (int c = 0; c < n; c++)
		{
			if (num != board[r + base_r][c + base_c])
			{
				isSame = false;
				break;
			}
		}
	}

	if (isSame)
		cout << num;
	else
	{
		cout << '(';
		int new_n = n / 2;
		for (int i = 0; i < 4; i++)
		{
			int new_r = base_r + new_n * (i / 2);
			int new_c = base_c + new_n * (i % 2);
			Func(new_n, new_r, new_c);
		}

		cout << ')';
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int N;
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		string s;
		cin >> s;
		for (int j = 0; j < N; j++)
			board[i][j] = s[j];
	}

	Func(N, 0, 0);

	return 0;
}

카테고리:

업데이트:

댓글남기기