본문 바로가기

Algorithm/Implementation

(C++) - 백준(BOJ) 2630번 : 색종이 만들기 답

반응형

www.acmicpc.net/problem/2630

 

2630번: 색종이 만들기

첫째 줄에는 전체 종이의 한 변의 길이 N이 주어져 있다. N은 2, 4, 8, 16, 32, 64, 128 중 하나이다. 색종이의 각 가로줄의 정사각형칸들의 색이 윗줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다.

www.acmicpc.net

재귀를 이용해 푸는 문제였습니다.

 

풀이방법 

 면적이 다른 색인 색종이가 나오면 2로 계속 나누고 면적이 같다면 파란색인지 흰색인지 판별하여 답을 구하는 문제였습니다.

 

Code

#include <iostream>
using namespace std;
int paper[130][130];
int n;
int whiteNum;
int blueNum;
bool isSameColor(int i, int j, int width){
    int color = paper[i][j];
    for(int x = i; x < i + width; x++)
        for(int y = j; y < j + width; y++)
            if(paper[x][y]!=color)
                return 0;
    return 1;
}

void cutPaper(int i,int j,int n){
    if(!isSameColor(i,j,n)){
        cutPaper(i+n/2,j+n/2,n/2);
        cutPaper(i,j+n/2,n/2);
        cutPaper(i+n/2,j,n/2);
        cutPaper(i,j,n/2);
    }else{
        if(paper[i][j]==0)
            whiteNum+=1;
        else
            blueNum+=1;
    }
    return;
}

int main(){
    cin >> n;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            cin >> paper[i][j];
    cutPaper(0,0,n);
    cout << whiteNum << '\n' << blueNum << '\n';
}