[백준] 7576* 토마토

AI·2025년 9월 11일

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

import java.io.*;
import java.util.*;

public class Main {

    // 방향 벡터 (상, 하, 좌, 우)
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};
    static int m;
    static int n;
    static int[][] matrix;
    static Queue<int[]> queue;

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        // M: 가로(열), N: 세로(행)
        StringTokenizer st = new StringTokenizer(br.readLine());
        m = Integer.parseInt(st.nextToken());
        n = Integer.parseInt(st.nextToken());
        // 토마토 정보를 담을 2차원 배열
        matrix = new int[n][m];
        // BFS를 위한 큐(Queue) 자료구조
        queue = new ArrayDeque<>();
        // 토마토 입력 받기
        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < m; j++) {
                matrix[i][j] = Integer.parseInt(st.nextToken());
                // 익은 토마토(1)는 큐에 추가한다
                if (matrix[i][j] == 1) {
                    queue.add(new int[]{i, j});
                }
            }
        }
        BFS();
        // 결과 계산
        int result = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                // 만약 익지 않은 토마토(0)가 남아있다면 -1 출력 후 종료
                if (matrix[i][j] == 0) {
                    System.out.println("-1");
                    return;
                }
                // 최대 날짜 갱신
                result = Math.max(result, matrix[i][j]);
            }
        }
        System.out.println(result - 1);
        br.close();
    }

    static void BFS() {
        while (!queue.isEmpty()) {
            // 현재 익은 토마토 위치
            int[] current = queue.poll();
            int x = current[0];
            int y = current[1];
            // 상하좌우 탐색
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                // Validation
                if (nx < 0 || ny < 0 || nx >= n || ny >= m || matrix[nx][ny] != 0) continue;
                // 익은 날짜를 기록 (이전 값 + 1)
                matrix[nx][ny] = matrix[x][y] + 1;
                queue.add(new int[]{nx, ny});
            }
        }
    }
}

0개의 댓글