[ ALGORITHM ]/[ 백 준 ]

[BAEKJOON] 1992번 : 쿼드트리

HiStar__ 2020. 8. 2. 15:38

문 제

 

소 스  코 드

#include <iostream>
#include <string>
constexpr char WHITE		{ '0' };
constexpr char BLACK		{ '1' };


void QuadTree();
void Quad(std::string *, const int&, const int&, const int&);
bool CompareColor(std::string *, const int&, const int&, const int&);
void Display(const std::string * , const int & , const int & , const int & );


int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	std::cout.tie(NULL);

	QuadTree();
	
	return 0;
}

void QuadTree()
{
	int N;
	std::cin >> N;

	std::string *data{ new std::string[N] };
	
	for (int i = 0; i < N; ++i) {
		std::cin >> data[i];
	}

	Quad(data, N, 0, 0);
	
	delete[] data;
}

void Quad(std::string * arr, const int & n, const int & x, const int & y)
{
	//Display(arr, n, x, y);
	if (CompareColor(arr, n, x, y)) return;
	else {
		std::cout << '(';
		int num = n / 2;
		Quad(arr, num, x, y);				// 왼쪽 위
		Quad(arr, num, x, y + num);			// 오른쪽 위
		Quad(arr, num, x + num, y);			// 왼쪽 아래
		Quad(arr, num, x + num, y + num);	// 오른쪽 아래
		std::cout << ')';
	}
}

bool CompareColor(std::string * arr, const int & n, const int & x, const int & y)
{
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (arr[x][y] != arr[i + x][j + y]) return false;
		}
	}
	if (BLACK == arr[x][y])			std::cout << '1';
	else if (WHITE == arr[x][y])	std::cout << '0';

	return true;
}

void Display(const std::string * arr, const int & n, const int & x, const int & y) {
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			std::cout << arr[x + i][y + j] << " ";
		}
		std::cout << '\n';
	}
	std::cout << "\n\n";
}

 

풀 이

 

순서에 맞게 재귀 함수를 통하여 구현 하였습니다.

 

출 력 값

 

 

문 제  출 처

문제 링크 : https://www.acmicpc.net/problem/1992

 

1992번: 쿼드트리

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

www.acmicpc.net

 

'[ ALGORITHM ] > [ 백 준 ]' 카테고리의 다른 글

[BAEKJOON] 1629번 : 곱셈 *  (0) 2020.08.04
[BAEKJOON] 1780번 : 종이의 개수  (0) 2020.08.03
[BAEKJOON] 2630번 : 색종이 만들기  (0) 2020.08.01
[BAEKJOON] 5430번 : AC  (0) 2020.07.31
[BAEKJOON] 1021번 : 회전하는 큐  (0) 2020.07.30