반응형

www.acmicpc.net/problem/14500

 

14500번: 테트로미노

폴리오미노란 크기가 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
 https://velog.io/@skyepodium/%EB%B0%B1%EC%A4%80-14500-%ED%85%8C%ED%8A%B8%EB%A1%9C%EB%AF%B8%EB%85%B8
#include <iostream>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, m, arr[501][501], val=0, ans = 0;
int dx[] = { 10-10 };
int dy[] = { 010-1 };
int ex[4][4= {
    {000-1}, // ㅗ
    {0121}, // ㅏ
    {0001}, // ㅜ
    {0-101// ㅓ
};
int ey[4][4= {
    {0121}, // ㅗ
    {0001}, // ㅏ
    {0121}, // ㅜ
    {0111// ㅓ
};
bool visited[501][501];
void dfs(int x, int y, int cnt) {
    if (cnt == 4) {
        ans = max(ans, val);
        return;
    }
 
    rep(i, 4) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
            if (!visited[nx][ny]) {
                visited[nx][ny] = 1;
                val += arr[nx][ny];
                dfs(nx, ny, cnt + 1);
                val -= arr[nx][ny];
                visited[nx][ny] = 0;
            }
        }
    }
}
void func2(int x, int y) {
    rep(i, 4) {
    int cnt = 0, temp = 0;
        rep(j, 4) {
            int nx = x + ex[i][j];
            int ny = y + ey[i][j];
            if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
                temp += arr[nx][ny];
                cnt++;
            }
        }
        if (cnt == 4)
            ans = max(ans, temp);
    }
}
 
void func1(int x, int y) {
    int temp = 0;
    // ㅗ
    if (x - 1 >= 0 && x < n && y >= 0 && y + 2 < m) {
        temp = arr[x][y] + arr[x][y + 1+ arr[x - 1][y + 1+ arr[x][y + 2];
        ans = max(ans, temp);
    }
    // ㅏ
    if (x >= 0 && x + 2 < n && y >= 0 && y + 1 < m) {
        temp = arr[x][y] + arr[x+1][y] + arr[x+2][y] + arr[x+1][y + 1];
        ans = max(ans, temp);
    }
    // ㅜ
    if (x >= 0 && x + 1 < n && y >= 0 && y + 2 < m) {
        temp = arr[x][y] + arr[x][y + 1+ arr[x +1][y + 1+ arr[x][y + 2];
        ans = max(ans, temp);
    }
    // ㅓ
    if (x - 1 >= 0 && x + 1 < n && y >= 0 && y + 1 < m) {
        temp = arr[x][y] + arr[x][y + 1+ arr[x - 1][y + 1+ arr[x+1][y + 1];
        ans = max(ans, temp);
    }
 
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    rep(i, n) {
        rep(j, m) {
            cin >> arr[i][j];
        }
    }
 
    rep(i, n) {
        rep(j, m) {
            if (!visited[i][j]) {
                visited[i][j] = 1;
                val += arr[i][j];
                dfs(i, j, 1);
                func2(i, j);
                val -= arr[i][j];
                visited[i][j] = 0;
            }
        }
    }
    cout << ans;
}
cs
반응형

'백준 > 삼성기출' 카테고리의 다른 글

백준 14499  (0) 2021.02.21
백준 14501  (0) 2021.02.15
백준 15686  (0) 2021.02.05
백준 14502  (0) 2021.02.03
백준 15684  (0) 2021.02.03

+ Recent posts