백준 풀이 C++

백준 2630 C++ (분할과 정복)

ag2개발자 2022. 9. 24. 16:55
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ss stable_sort
#define ll long long

int paper[128][128];
int white , blue;

void Devide(int x, int y, int s) {   //분할 함수
    int color = paper[x][y];      //왼쪽 모서리 색깔저장
    for (int i = x; i < x + s; i++) {    
        for (int j = y; j < y + s; j++) {    //사이즈내를 돌면서
            if (color == 0 && paper[i][j]==1) {
                color = 2;
            }
            else if (color == 1 && paper[i][j] == 0) {    //색깔이 다르면
                color = 2;                                //color를 2로 설정

            }
            if (color == 2) {
                Devide(x, y, s / 2);
                Devide(x, y+s/2, s / 2);
                Devide(x+s/2, y, s / 2);
                Devide(x+s/2, y+s/2, s / 2); //color가 2로 설정돼 있으면
                                             //동일한 크기의 4개로 분할
                return;
            }
        }
    }
    
    if (color==1) {
        blue++;    //색깔이 모두 같으면 해당 색깔++
    }
    else {
        white++;
    }
}


int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> paper[i][j];
        }
    }    //입력

    Devide(0, 0, n);    //0,0에 사이즈는 n
    cout << white << "\n" << blue;

	return 0;
}

'백준 풀이 C++' 카테고리의 다른 글

백준 1072 c++ 이분탐색  (0) 2022.09.27
백준 2805 C++ (이분탐색)  (0) 2022.09.24
백준 3020 c++ (이분탐색)  (1) 2022.09.24
백준 7785 c++ set 역순 출력  (1) 2022.09.23
백준 2702 C++(유클리드 호제법)  (0) 2022.09.22