본문 바로가기

알고리즘 관련/BOJ

BOJ)14500 테트로미노

문제:icpc.me/14500


주어진 문제의 답은 모든 경우에 테트로미노를 다 대봄으로서 구할 수 있다.


주어지는 테트로미노의 5가지 모양중 ㅜ 모양을 제외한 4가지 모양은 DFS를 통하여 탐색이 가능하고 ㅜ모양만 따로 처리를 해주면 된다.


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
#include <cstdio>
#include <algorithm>
using namespace std;
int n, m, a[501][501], v[501][501], r;
int dx[] = { 0,0,1,-};
int dy[] = { 1,-1,0,};
bool chk(int x, int y) {
    return <= x&&< n && <= y&&< m;
}
int dfs(int x, int y, int c) {
    if (c == 4)return a[x][y];
    v[x][y] = 1;
    int ret = a[x][y];
    for (int i = 0; i < 4; i++) {
        int cx = x + dx[i];
        int cy = y + dy[i];
        if (chk(cx, cy) && !v[cx][cy])
            ret = max(ret, a[x][y] + dfs(cx, cy, c + 1));
    }
    v[x][y] = 0;
    return ret;
}
int func(int x, int y) {
    int ret = 0, mmin = 987654321;
    int c[4= { -1,-1,-1,-};
    for (int i = 0; i < 4; i++) {
        int cx = x + dx[i];
        int cy = y + dy[i];
        if (chk(cx, cy))
            c[i] = a[cx][cy];
    }
    int ct = 0;
    for (int i = 0; i < 4; i++)
        if (c[i] == -1
            ct++;
    if (ct >= 2)return -1;
    for (int i = 0; i < 4; i++) {
        if (c[i] == -1)continue;
        ret += c[i];
        mmin = min(mmin, c[i]);
    }
    if (!ct)
        return ret - mmin + a[x][y];
    else
        return ret + a[x][y];
}
int main() {
    scanf("%d%d"&n, &m);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++)
            scanf("%d"&a[i][j]);
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            r = max(r, dfs(i, j, 1));
            r = max(r, func(i, j));
        }
    }
    printf("%d\n", r);
    return 0;
}
cs




'알고리즘 관련 > BOJ' 카테고리의 다른 글

BOJ)1251 단어 나누기  (0) 2017.04.20
BOJ)14503 로봇 청소기  (0) 2017.04.17
BOJ)14501 퇴사  (0) 2017.04.17
BOJ)14502 연구소  (2) 2017.04.17
BOJ)3079 입국심사  (1) 2017.04.13