본문 바로가기
Computer Science

1780 종이의 개수 / 분할 정복

by OKOK 2018. 12. 11.

1. 문제

2. 재귀형식

3. 체크 사항

4. 인덱스 주의

5. 같은 지 확인 하는 함수, 풀이 함수 


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
#include <iostream>
 
using namespace std;
#define MAX_N 2188
int N;
int paper[MAX_N][MAX_N];
int cnt[3]; // cnt[0] : -1로만 채워진 종이의 개수, cnt[1] : 0으로만 채워진 종이의 개수, cnt[2] : 1로만 채워진 종이의 개수
bool same(int x, int y, int n)
{
    for (int i = x; i < x + n; i++)
    {
        for (int j = y; j < y + n; j++)
        {
            if (paper[x][y] != paper[i][j])
                return false;
        }
    }
    return true;
}
 
void solve(int x, int y, int n)
{
    // 같은 숫자로 이루어진 종이면
    if (same(x, y, n))
    {
        cnt[paper[x][y] + 1+= 1;
        return;
    }
    int m = n / 3;
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            solve(x + i * m, y + j * m, m);
        }
    }
}
 
int main()
{
    freopen("input.txt""r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            cin >> paper[i][j];
        }
    }
    solve(00, N);
    for (int i = 0; i < 3; i++cout << cnt[i] << "\n";
    return 0;
}
cs




댓글