반응형

www.acmicpc.net/problem/14391

 

14391번: 종이 조각

영선이는 숫자가 쓰여 있는 직사각형 종이를 가지고 있다. 종이는 1×1 크기의 정사각형 칸으로 나누어져 있고, 숫자는 각 칸에 하나씩 쓰여 있다. 행은 위에서부터 아래까지 번호가 매겨져 있고,

www.acmicpc.net

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
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <algorithm>
#include <cstring>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int arr[5][5], direction[5][5], visited[5][5];
int n, m;
int val = 0;
int total = 0;
int ans = -1;
int cur;
int temp = 0;
void func(int x, int y) {
    while(1){
        if (x >= n || y >= m || cur != direction[x][y]) {
            total += temp;
            temp = 0;
            return;
        }
        if (visited[x][y])
            return;
        visited[x][y] = 1;
        temp = temp * 10 + arr[x][y];
        if (direction[x][y] == 0)
            y++;
        else
            x++;
    }
}
int main() {
    cin >> n >> m;
    rep(i, n) {
        rep(j, m) {
            char c;
            cin >> c;
            arr[i][j] = c - '0';
        }
    }
 
    while (val < (1 << (n * m))) {
        // 1. val -> direction[][]
        int idx = 0;
        while (idx < n*m) {
            int nx = idx / m;
            int ny = idx % m;
            direction[nx][ny] = (val >> idx) & 1;
            idx++;
        }
        // 2. arr, direction, visited
        memset(visited, 0sizeof(visited));
        total = 0;
        rep(i, n) {
            rep(j, m) {
                if (!visited[i][j]) {
                    cur = direction[i][j];
                    func(i,j);
                }
            }
 
        }
        ans = max(ans, total);
        val++;
    }
    cout << ans;
}
cs
반응형

'백준 > 브루트포스' 카테고리의 다른 글

백준 1208 [복습 필수]  (0) 2021.03.06
백준 14225  (0) 2021.02.23
백준 1182  (0) 2021.02.15
백준 6064  (0) 2021.02.13
백준 1107 [복습 필수]  (0) 2021.02.13

+ Recent posts