백준 7576 - 토마토

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

 

토마토가 들어있는 칸(1)의 상하좌우를 모두 확인하여 토마토가 들어있지 않은 칸(0)이면 토마토가 들어있던 칸 숫자에 +1을 해주어 전체 토마토가 익은 날짜가 며칠인지 계산.

 

처음 푼 골드 문제

문제를 마주하고 로직을 생각하기에 상당히 어려웠던 문제였다.

import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
import static java.lang.System.exit;

public class Main {

    static class Node {
        int x;
        int y;

        public Node(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

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

        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        int[][] A = new int[M][N];
        int[] dx = {-1, 0, 1, 0};
        int[] dy = {0, -1, 0, 1};
        int answer = 0;
        Queue<Node> q = new LinkedList<>();
        
        for(int i = 0; i < M ; i++){
            st = new StringTokenizer(br.readLine());
            for(int j = 0; j < N; j++){
                A[i][j] = Integer.parseInt(st.nextToken());
                if(A[i][j] == 1){
                    q.add(new Node(i, j));
                }
            }
        }

        while (!q.isEmpty()){
            Node node = q.poll();
            int x = node.x;
            int y = node.y;

            for(int i=0; i<4; i++){
                int nx = x + dx[i];
                int ny = y + dy[i];
                if(0 <= nx && nx < M  && 0 <= ny && ny < N && A[nx][ny] == 0){
                    A[nx][ny] = A[x][y] + 1 ;
                    q.add(new Node(nx, ny));
                }
            }
        }

        for(int i=0; i<M; i++){
            for(int j=0; j<N; j++){
                if(answer < A[i][j]){
                    answer = A[i][j];
                } else if (A[i][j] == 0) {
                    bw.write("-1");
                    bw.flush();
                    exit(0);
                }
            }
        }
        bw.write(String.valueOf(answer -1));
        bw.flush();
    }
}

'JAVA STUDY > 백준' 카테고리의 다른 글

백준 24444 - 알고리즘 수업 - 너비 우선 탐색 1  (0) 2024.06.05