본문 바로가기
둥지/알고리즘

[백준 1992] 쿼드 트리

by 까닭 2023. 2. 20.

문제링크

#include <iostream>
#include <vector>

char arr[100][100] = {};

void f(int a, int b, int n)
{
    int flag = 0;
    int j = 0;

    for (int i = 0; i < n; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            if (arr[a][b] != arr[a + i][b + j])
                break;
        }

        if (n != j)
        {
            flag = 1;
            break;
        }
    }

    if (0 == flag)
        std::cout << arr[a][b];

    else
    {
        std::cout << "(";
        
        n /= 2;
        f(a, b, n);
        f(a, b + n, n);
        f(a + n, b, n);
        f(a + n, b + n, n);

        std::cout << ")";
    }
}

int main()
{
    size_t n = 0;
    std::cin >> n;

    for (size_t i = 0; i < n; i++)
    {
        std::cin >> arr[i];
    }

    f(0, 0, n);
    return 0;
}

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

버블 정렬  (0) 2023.02.25
Array, Vector 구현  (0) 2023.02.20
리스트 구현  (0) 2023.02.17
큐 구현  (0) 2023.02.10
스택 구현  (0) 2023.02.09