문제 : 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;
}
'Problem Solving > BOJ' 카테고리의 다른 글
[BOJ] 18311 : 왕복 - travelbeeee (0) | 2020.01.31 |
---|---|
[BOJ] 18353 : 병사 배치하기 - travelbeeee (0) | 2020.01.30 |
[BOJ] 17198 : Bucket Brigade - travelbeeee (0) | 2020.01.29 |
[BOJ] 1780 : 종이의 개수 - travelbeeee (0) | 2020.01.29 |
[BOJ] 1992 : 쿼드트리 - travelbeeee (0) | 2020.01.29 |