문제 : https://www.acmicpc.net/problem/2630


[ 알고리즘풀이 ]

재귀 함수를 이용해 문제를 분할하며 하얀 종이와 파란 종이를 count 해주면 됩니다.

#include<iostream>

using namespace std;

int color[2] = {}, map[128][128] = {};

void countPaper(int x, int y, int N) {
	int cur = map[x][y];
	bool check = false;
	for(int i = 0; i < N; i++)
		for(int j = 0; j < N; j++)
			if (map[x + i][y + j] != cur) {
				i = N, j = N; // break
				check = true;
			}
	if (check) {
		countPaper(x, y, N / 2);
		countPaper(x, y + (N / 2), N / 2);
		countPaper(x + (N / 2), y, N / 2);
		countPaper(x + (N / 2), y + (N / 2), N / 2);
	}
	else
		color[cur]++;

	return;
}
int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int N;
	cin >> N;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			cin >> map[i][j];
	countPaper(0, 0, N);
	cout << color[0] << '\n' << color[1];
	return 0;
}

 

+ Recent posts