토마토

Huisu·2023년 8월 17일
0

Coding Test Practice

목록 보기
27/98
post-thumbnail

문제

7576번: 토마토

문제 설명

철수의 토마토 농장에서는 토마토를 보관하는 큰 창고를 가지고 있다. 토마토는 아래의 그림과 같이 격자 모양 상자의 칸에 하나씩 넣어서 창고에 보관한다.
https://upload.acmicpc.net/de29c64f-dee7-4fe0-afa9-afd6fc4aad3a/-/preview/

창고에 보관되는 토마토들 중에는 잘 익은 것도 있지만, 아직 익지 않은 토마토들도 있을 수 있다. 보관 후 하루가 지나면, 익은 토마토들의 인접한 곳에 있는 익지 않은 토마토들은 익은 토마토의 영향을 받아 익게 된다. 하나의 토마토의 인접한 곳은 왼쪽, 오른쪽, 앞, 뒤 네 방향에 있는 토마토를 의미한다. 대각선 방향에 있는 토마토들에게는 영향을 주지 못하며, 토마토가 혼자 저절로 익는 경우는 없다고 가정한다. 철수는 창고에 보관된 토마토들이 며칠이 지나면 다 익게 되는지, 그 최소 일수를 알고 싶어 한다.

토마토를 창고에 보관하는 격자모양의 상자들의 크기와 익은 토마토들과 익지 않은 토마토들의 정보가 주어졌을 때, 며칠이 지나면 토마토들이 모두 익는지, 그 최소 일수를 구하는 프로그램을 작성하라. 단, 상자의 일부 칸에는 토마토가 들어있지 않을 수도 있다.

제한 사항

첫 줄에는 상자의 크기를 나타내는 두 정수 M,N이 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M,N ≤ 1,000 이다. 둘째 줄부터는 하나의 상자에 저장된 토마토들의 정보가 주어진다. 즉, 둘째 줄부터 N개의 줄에는 상자에 담긴 토마토의 정보가 주어진다. 하나의 줄에는 상자 가로줄에 들어있는 토마토의 상태가 M개의 정수로 주어진다. 정수 1은 익은 토마토, 정수 0은 익지 않은 토마토, 정수 -1은 토마토가 들어있지 않은 칸을 나타낸다.

토마토가 하나 이상 있는 경우만 입력으로 주어진다.

입출력 예

입력출력
6 4
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 18
6 4
0 -1 0 0 0 0
-1 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 1-1
6 4
1 -1 0 0 0 0
0 -1 0 0 0 0
0 0 0 0 -1 0
0 0 0 0 -1 16
5 5
-1 1 0 0 0
0 -1 -1 -1 0
0 -1 -1 -1 0
0 -1 -1 -1 0
0 0 0 0 014
2 2
1 -1
-1 10

입출력 예 설명

아이디어

bfs네

제출 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class five7576 {
    int[] dRow = new int[] {0, 0, 1, -1};
    int[] dCol = new int[] {1, -1, 0, 0};
    public void solution() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer infoToken = new StringTokenizer(reader.readLine());
        int colNum = Integer.parseInt(infoToken.nextToken());
        int rowNum = Integer.parseInt(infoToken.nextToken());
        int[][] tomato = new int[rowNum][colNum];
        Queue<int[]> toVisit = new LinkedList<>();

        for (int i = 0; i < rowNum; i++) {
            StringTokenizer tomatoToken = new StringTokenizer(reader.readLine());
            for (int j = 0; j < colNum; j++) {
                tomato[i][j] = Integer.parseInt(tomatoToken.nextToken());
                if (tomato[i][j] == 1) toVisit.offer(new int[] {i, j});
            }
        }

        int time = 0;
        boolean[][] visited = new boolean[rowNum][colNum];

        while(!toVisit.isEmpty()) {
            time++;
            int repeatNum = toVisit.size();
            for (int index = 0; index < repeatNum; index++) {
                int[] current = toVisit.poll();
                int currentRow = current[0];
                int currentCol = current[1];
                visited[currentRow][currentCol] = true;

                for (int i = 0; i < 4; i++) {
                    int nextRow = currentRow + dRow[i];
                    int nextCol = currentCol + dCol[i];
                    if (-1 < nextRow && nextRow < rowNum
                            && -1 < nextCol && nextCol < colNum
                            && !visited[nextRow][nextCol]
                            && tomato[nextRow][nextCol] == 0) {
                        tomato[nextRow][nextCol] = 1;
                        toVisit.offer(new int[]{nextRow, nextCol});
                    }
                }
            }
        }

        for (int i = 0; i < rowNum; i++) {
            for (int j = 0; j < colNum; j++) {
                if (tomato[i][j] == 0) time = 0;
            }
        }

        System.out.println(time - 1); // 맨 마지막에 익는 토마토 제외
    }

    public static void main(String[] args) throws IOException {
        new five7576().solution();
    }
}

0개의 댓글