Programing/BaekJoon

[JAVA] 백준 14500 : 테트로미노

블스뜸 2025. 3. 30. 17:29
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        // 도형
        // 1은 도형이 차지한 공간 0은 도형이 차지하지 않은 공간
        int[][][][] tetrominoes = {
                // 막대 모양
                {{{1, 1, 1, 1}}, {{1}, {1}, {1}, {1}}},
                // 정사각형 모양
                {{{1, 1}, {1, 1}}},
                // Z 모양
                {{{1, 1, 0}, {0, 1, 1}}, {{0, 1, 1}, {1, 1, 0}}, {{1, 0}, {1, 1}, {0, 1}}, {{0, 1}, {1, 1}, {1, 0}}},
                // L 모양
                {{{1, 0, 0}, {1, 1, 1}}, {{1, 1, 1}, {1, 0, 0}}, {{1, 1}, {1, 0}, {1, 0}}, {{1, 0}, {1, 0}, {1, 1}}, {{0, 0, 1}, {1, 1, 1}}, {{1, 1, 1}, {0, 0, 1}}, {{0, 1}, {0, 1}, {1, 1}}, {{1, 1}, {0, 1}, {0, 1}}},
                // T 모양
                {{{0, 1, 0}, {1, 1, 1}}, {{1, 0}, {1, 1}, {1, 0}}, {{1, 1, 1}, {0, 1, 0}}, {{0, 1}, {1, 1}, {0, 1}}}};


        // 축의 크기 입력
        StringTokenizer st = new StringTokenizer(br.readLine());
        int y = Integer.parseInt(st.nextToken());
        int x = Integer.parseInt(st.nextToken());
        String[][] board = new String[y][x];
        
        // 보드 내용 입력
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < y; i++) {
            board[i] = br.readLine().split(" ");
        }
        // 도형별로 보드에서 측정
        for (int i = 0; i < tetrominoes.length; i++) {
            max= Math.max(max,check(tetrominoes[i], x, y, board));
        }
        bw.write(String.valueOf(max));

        bw.flush();
        bw.close();
        br.close();
    }

    // 시작점 탐색
    static int check(int[][][] arr, int _x, int _y, String[][] board) { // board는 판의 상태를 나타내는 2차원 배열
        int num = 0;

        for (int rotate = 0; rotate < arr.length; rotate++) {
            int[][] shape = arr[rotate];
            int shapeHeight = shape.length;
            int shapeWidth = shape[0].length;
            for (int x = 0; x <= _x - shapeWidth; x++) {
                for (int y = 0; y <= _y - shapeHeight; y++) {
                    // 시작점에서 값을 측정하고 이전 값이랑 비교해서 더 높은 값 반환
                    num = Math.max(num,place(shape, board, x, y));
                }
            }
        }

        return num;
    }

    // 도형의 모양, 보드, 시작점 축을 받아오고 
    // 도형이 차지하는 공간이 나오면
    // 받아온 시작점 + 도형의 축을 해서 값을 측정
    static int place(int[][] shape, String[][] board, int x, int y) {
        int num = 0;
        for (int i = 0; i < shape.length; i++) {
            for (int j = 0; j < shape[i].length; j++) {
                if (shape[i][j] == 1) {
                    num += Integer.parseInt(board[y + i][x + j]);
                }
            }
        }
        return num;
    }


}