PS

백준 BOJ 1780 C++ 종이의 개수

firsthg 2026. 4. 22. 12:58
#include <bits/stdc++.h>
using namespace std;

int arr[2200][2200];
int papers[3];

bool is_uniform(int n, int x, int y) {
    int first = arr[x][y];
    for (int i = x; i < x + n; ++i) {
        for (int j = y; j < y + n; ++j) {
            if (first != arr[i][j]) {
                return false;
            }
        }
    }
    return true;
}

void func(int n, int x, int y) {
    if (is_uniform(n, x, y)) {
        papers[arr[x][y] + 1]++;
    } else {
        int sub_size = n / 3;
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                func(sub_size, x + i * sub_size, y + j * sub_size);
            }
        }
    }
}

void solve() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> arr[i][j];
        }
    }
    func(n, 0, 0);
    for (int i = 0; i < 3; ++i) {
        cout << papers[i] << '\n';
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    solve();
    return 0;
}