PS/백준 알고리즘[BOJ]

[백준 1992번] 쿼드트리 (C++)

BE_개발자 2023. 10. 21. 15:49
728x90
반응형

//white: 0, black: 1
#define SIZE 64
#include<iostream>
#include<string>
#include<cstdlib>

using namespace std;
char col;
char map[SIZE][SIZE];
// change - true, not change - false
bool check(int x1, int y1, int x2, int y2) {
	col = map[x1][y1];
	for (int i = x1; i < x2; i++) {
		for (int j = y1; j < y2; j++) {
			if (col != map[i][j]) return true;
		}
	}
	return false;
}

void dc(int size, int x, int y) {
	if (size == 1) {
		cout << map[x][y];
		return;
	}
	if (!check(x, y, x + size, y + size)) {
		cout << map[x][y];
		return;
	}
	size /= 2;
	cout << '(';
	dc(size, x, y);
	dc(size, x + size, y);
	dc(size, x, y + size);
	dc(size, x + size, y + size);
	cout << ')';
}

int main(void) {
	cin.tie(0);
	ios_base::sync_with_stdio(NULL);
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		string str;
		cin >> str;
		for (int j = 0; j < N; j++) map[j][i] = str[j];
	}
	dc(N, 0, 0);
	return 0;
}
728x90
반응형