[JAVA-자바] 14500번: 테트로미노

2025. 4. 8. 21:36·알고리즘/백준

 

소스코드:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class Main {
    static BufferedReader br;
    static BufferedWriter bw;

    static int width, height;
    static int[][] board;
    static boolean[][] visited;
    static int result;

    static int[] dx = new int[]{0, 0, -1, 1};
    static int[] dy = new int[]{-1, 1, 0, 0};


    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        bw = new BufferedWriter(new OutputStreamWriter(System.out));

        StringTokenizer st = new StringTokenizer(br.readLine());
        height = Integer.parseInt(st.nextToken());
        width = Integer.parseInt(st.nextToken());

        board = new int[height][width];
        visited = new boolean[height][width];
        result = 0;

        for (int y = 0; y < height; y++) {
            st = new StringTokenizer(br.readLine());
            for (int x = 0; x < width; x++) {
                board[y][x] = Integer.parseInt(st.nextToken());
            }
        }
        br.close();

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                visited[y][x] = true;
                dfs(y, x, board[y][x], 1);
                visited[y][x] = false;
            }
        }
        br.close();

        bw.write(Integer.toString(result));
        bw.close();
    }

    private static void dfs(int y, int x, int score, int depth) {
        if (depth == 4) {
            result = Math.max(result, score);
            return;
        }

        for (int i = 0; i < 4; i++) {
            int nextY = y + dy[i];
            int nextX = x + dx[i];
            if (!isValidPos(nextY, nextX)) {
                continue;
            }
            if (!visited[nextY][nextX]) {
                if (depth == 2) {
                    // ㅗ ㅜ ㅓ ㅏ
                    visited[nextY][nextX] = true;
                    dfs(y, x, score + board[nextY][nextX], depth + 1);
                    visited[nextY][nextX] = false;
                }
                visited[nextY][nextX] = true;
                dfs(nextY, nextX, score + board[nextY][nextX], depth + 1);
                visited[nextY][nextX] = false;
            }
        }
    }

    private static boolean isValidPos(int y, int x) {
        return 0 <= y && y < height &&
                0 <= x && x < width;
    }
}

 

글의 내용 중 잘못된 점이나 수정이 필요한 부분, 혹은 궁금한 사항이 있다면 언제든 댓글로 남겨주시면 감사하겠습니다.

여러분의 피드백은 더 나은 글을 작성하는 데 큰 도움이 됩니다. 감사합니다.

'알고리즘 > 백준' 카테고리의 다른 글

[JAVA-자바] 11725번: 트리의 부모 찾기  (0) 2025.04.09
[JAVA-자바] 11053번: 가장 긴 증가하는 부분 수열  (0) 2025.04.09
[JAVA-자바] 9019번: DSLR  (0) 2025.04.08
[JAVA-자바] 16928번: 뱀과 사다리 게임  (0) 2025.04.08
[JAVA-자바] 7662번: 이중 우선순위 큐  (0) 2025.04.07
'알고리즘/백준' 카테고리의 다른 글
  • [JAVA-자바] 11725번: 트리의 부모 찾기
  • [JAVA-자바] 11053번: 가장 긴 증가하는 부분 수열
  • [JAVA-자바] 9019번: DSLR
  • [JAVA-자바] 16928번: 뱀과 사다리 게임
raven
raven
Github : https://github.com/RabeMaster | Email : ra___be@naver.com
  • raven
    배움을 원하는 사람
    raven
  • 전체
    오늘
    어제
  • 공지사항

    • 안녕하세요
    • 분류 전체보기 (169)
      • 네이버 부스트캠프 10기 (7)
        • 멤버십 (5)
        • 챌린지 (1)
        • 베이직 (1)
      • 공부 (2)
        • JAVA (1)
        • CS (0)
        • 정보처리기사 (1)
      • 알고리즘 (159)
        • 백준 (159)
      • 개발 (1)
        • 백준 확장 프로그램 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 인기 글

  • 태그

    멤버십
    수학
    코딩
    IT
    스택
    코딩테스트
    정렬
    알고리즘
    브루트포스 알고리즘
    그래프 이론
    백트래킹
    네이버부스트캠프
    너비 우선 탐색
    회고
    자바
    java
    자료 구조
    네이버
    문자열
    그래프 탐색
    다이나믹 프로그래밍
    코테
    그리디 알고리즘
    부트캠프
    개발
    네부캠
    부스트캠프
    구현
    최단 경로
    백준
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
raven
[JAVA-자바] 14500번: 테트로미노
상단으로

티스토리툴바