알고리즘 문제풀이/백준

[백준 2630번] 색종이 만들기

슥지니 2021. 12. 27. 11:11
반응형

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

 

2630번: 색종이 만들기

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

www.acmicpc.net

<C++ 소스 코드> 재귀

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <utility>
#include <climits>
#include <cmath>
#include <deque>
 
using namespace std;
 
int board[128][128];
int res[2];
// x,y에서 시작해서 가로 세로 길이가 각각 n인 종이
void solve(int x, int y, int n) {
    if (n == 1) {
        res[board[x][y]]++;
        return;
    }
    int flag = board[x][y];
    bool run = false;
    for (int i = x; i < x + n; i++) {
        for (int j = y; j < y + n; j++) {
            if (flag != board[i][j]) run = true;
        }
    }
    if (!run) {
        res[board[x][y]]++;
        return;
    }
    solve(x, y, n / 2);
    solve(x, y + n / 2, n / 2);
    solve(x + n / 2, y, n / 2);
    solve(x + n / 2, y + n / 2, n / 2);
}
 
int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int n; cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> board[i][j];
        }
    }
    solve(00, n);
 
    for (int i = 0; i < 2; i++)cout << res[i] << '\n';
 
    
    return 0;
}
 
cs

 

반응형