개발/알고리즘

백준 2630번 : 색종이 만들기

라사 RASA 2020. 8. 28. 21:08

문제 접근


예전에 비슷한 문제 푼 적 있었는데... 범위 내부 확인하고, 조건에 성립하지 않으면 재귀로 범위 나눠서 다시 확인하면 되는 간단한 문제였다.

 

 

문제 코드


#include <iostream>

using namespace std;	

int plane[128][128];
int blue=0, white=0;

void count(int x, int y, int n)
{
	// 해당 면이 전부 같은 색이면 true.
	bool check = true;
	
	for(int i=x; i<x+n; ++i)
		for(int j=y; j<y+n; ++j)
			if(plane[i][j]!=plane[x][y])
				check = false;
	
	if(check)
	{
		if(plane[x][y]==0)
			++white;
		else
			++blue;
	}
	
    // 해당 면이 전부 같은 색이 아니면 4등분해서 다시 확인.
	else
	{
		count(x,y,n/2);
		count(x+n/2,y,n/2);
		count(x,y+n/2,n/2);
		count(x+n/2,y+n/2,n/2);
	}
}				

int main(void)
{
	int N;
	cin>>N;
	
	for(int i=0; i<N; ++i)
		for(int j=0; j<N; ++j)
			cin>>plane[i][j];
	
	count(0,0,N);
	
	cout<<white<<'\n'<<blue<<'\n';
}