728x90
분류: 재귀, 분할정복
문제: www.acmicpc.net/problem/2630
2630번: 색종이 만들기
첫째 줄에는 전체 종이의 한 변의 길이 N이 주어져 있다. N은 2, 4, 8, 16, 32, 64, 128 중 하나이다. 색종이의 각 가로줄의 정사각형칸들의 색이 윗줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다.
www.acmicpc.net
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <climits>
#include <cstring>
#include <iomanip>
#include <bitset>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#define ll long long
#define INF 1e9
using namespace std;
//https://www.acmicpc.net/problem/2630 색종이 만들기
int n;
int ar[130][130];
int ans[2];
void go(int size, int x, int y) {
if(size == 0) return;
int start_color = ar[x][y];
bool flag = 0;
for(int i=x; i<x+size; i++) {
for(int j=y; j<y+size; j++) {
if(ar[i][j] != start_color) {
flag = true;
break;
}
}
if(flag) break;
}
if(!flag) {
ans[start_color]++;
return;
}
size /= 2;
go(size, x, y);
go(size, x, y+size);
go(size, x+size, y);
go(size, x+size, y+size);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
cin >> ar[i][j];
}
}
go(n, 0, 0);
cout << ans[0] << '\n' << ans[1];
return 0;
}
728x90
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준 / BOJ] 2579 계단 오르기 (0) | 2021.03.15 |
---|---|
[백준 / BOJ] 15650 N과 M(2) (0) | 2021.03.15 |
[백준 / BOJ] 2108 통계학 (0) | 2021.03.15 |
[백준 / BOJ] 1260 DFS와 BFS (0) | 2021.03.15 |
[백준 / BOJ] 5052 전화번호 목록 (0) | 2021.03.15 |