BaekJoon 14500, 테트로미노 전체 탐색


이번 문제는 삼성에서 출제 했었던 문제입니다.

https://www.acmicpc.net/problem/1874

짜는데 오래걸리긴 했지만,  나름 재미있는 문제였습니다.

주어진 블록으로 만들 수 있는 최대 값을 찾는 문제인데 블록을 만들고 해당 블록으로 전체 탐색을 실행하면 되는 문제였습니다.


 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
import java.util.Scanner;

public class BaekJoon14500 {
    static int N, M;
    static int[][] table;
    static int[][][] blocks = new int[19][][];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();M = sc.nextInt();
        table = new int[N][M];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                table[i][j] = sc.nextInt();
            }
        }
        initBlocks();
        scan();
    }
    public static void initBlocks () {
        int[][] b = {{1, 1},{1, 1}};
        int[][] s = {{1, 1, 0},{0, 1, 1}};
        int[][] h = {{1, 1, 1},{0, 1, 0}};
        int[][] f = {{1, 0, 0},{1, 1, 1}};
        int[][] l = {{1,1,1,1}};
        blocks[0] = b;blocks[1] = s;blocks[5] = h;blocks[9] = f;blocks[17] = l;
        blocks[2] = rotate(blocks[1]);blocks[3] = symmetry(blocks[1]);
        blocks[4] = rotate(blocks[3]);blocks[13] = symmetry(blocks[9]);
        for (int i = 6; i < blocks.length; i++) {
            if (i == 9 || i == 13 || i == 17) continue;
            blocks[i] = rotate(blocks[i - 1]);
        }
    }
    public static int[][] symmetry(int[][] b) {
        int[][] symmetricBlock = new int[b.length][b[0].length];
        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < b[i].length; j++) {
                symmetricBlock[(b.length - 1) - i][j] = b[i][j];
            }
        }
        return symmetricBlock;
    }
    public static int[][] rotate(int [][] b) {
        int[][] rotatedBlock = new int[b[0].length][b.length];
        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < b[i].length; j++) {
                rotatedBlock[j][(b.length - 1) - i] = b[i][j];
            }
        }
        return rotatedBlock;
    }
    public static void print(int[][] b) {
        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < b[i].length; j++) {
                System.out.print(b[i][j] + " ");
            }
            System.out.println();
        }
    }
    public static void scan() {
        int max = 0;
        for (int i = 0; i < blocks.length; i++) {
            for (int j = 0; j < N; j++) {
                for (int k = 0; k < M; k++) {
                    if (j + blocks[i].length <= N && k + blocks[i][0].length <= M)
                        max = Math.max(sumOfBlock(blocks[i], j, k), max);
                }
            }
        }
        System.out.println(max);
    }
    public static int sumOfBlock(int[][] b, int x, int y) {
        int sum = 0;
        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < b[i].length; j++) {
                sum += table[x + i][y + j] * b[i][j];
            }
        }
        return sum;
    }
}

이 블로그의 인기 게시물

웹툰 무료로 볼 수 있는 사이트

BackJoon 1011, Fly me to the alpha centauri, 규칙 찾기 문제

BaekJoon 14503, 로봇 청소기