나의 브을로오그으

[c++] 1992번 : 쿼드트리 본문

알고리즘/BaekJoon

[c++] 1992번 : 쿼드트리

__jhp_+ 2022. 6. 23. 10:17

https://www.acmicpc.net/problem/1992

 

1992번: 쿼드트리

첫째 줄에는 영상의 크기를 나타내는 숫자 N 이 주어진다. N 은 언제나 2의 제곱수로 주어지며, 1 ≤ N ≤ 64의 범위를 가진다. 두 번째 줄부터는 길이 N의 문자열이 N개 들어온다. 각 문자열은 0 또

www.acmicpc.net

#include <iostream>
#include <string>
using namespace std;

#define MAX_SIZE		65

bool IsSameValue(bool matrix[][65], int x, int y, int len);
void QuadTree(bool matrix[][65], int x, int y, int len, string* result);

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

	string result;
	result.reserve(MAX_SIZE * MAX_SIZE * 5);
	bool matrix[MAX_SIZE][MAX_SIZE] = { false, };
	int N = 0;
	char input[MAX_SIZE] = { 0, };
	cin >> N;
	cin.ignore();

	for (int i = 1; i <= N; ++i)
	{
		cin.getline(input, MAX_SIZE);
		for (int j = 1; j <= N; ++j)
		{
			matrix[i][j] = input[j - 1] - '0';
		}
	}

	QuadTree(matrix, 1, 1, N, &result);
	cout << result << '\n';
	return 0;
}

bool IsSameValue(bool matrix[][65], int x, int y, int len)
{
	bool value = matrix[y][x];
	for (int i = y; i < y + len; ++i)
	{
		for (int j = x; j < x + len; ++j)
		{
			if (matrix[i][j] != value)
			{
				return false;
			}
		}
	}
	return true;
}

void QuadTree(bool matrix[][65], int x, int y, int len, string* result)
{
	if (len == 1 || IsSameValue(matrix, x, y, len) == true)
	{
		int n = matrix[y][x] == true ? 1 : 0;
		result->append(to_string(n));
	}
	else
	{
		result->append("(");
		// todo quadtree
		int half = len / 2;
		QuadTree(matrix, x, y, half, result);
		QuadTree(matrix, x + half, y, half, result);
		QuadTree(matrix, x, y + half, half, result);
		QuadTree(matrix, x + half, y + half, half, result);
		result->append(")");
	}
	return;
}

'알고리즘 > BaekJoon' 카테고리의 다른 글

[c++] 2579번 : 계단 오르기  (0) 2022.06.27
[c++] 2178번 : 미로 탐색  (0) 2022.06.23
[c++] 1931번 : 회의실 배정  (0) 2022.06.22
[c++] 1927번 : 최소 힙  (0) 2022.06.21
[c++] 1764번 : 듣보잡  (0) 2022.06.15