토마토(BFS)

Changwook Yang·2023년 2월 19일

알고리즘 연습

목록 보기
29/41

창고의 토마토가 익은것도 있고(1) 안익은것도 있고(0) 토마토가 없는 곳(-1)도 있다.
며칠이 지나야 토마토가 다 익을 수 있는가?
걸리는 최소날짜를 출력하고 다 못익는 상황이면 -1을 출력

입력)
6 4
0 0 -1 0 0 0
0 0 1 0 -1 0
0 0 -1 0 0 0
0 0 0 0 -1 1

출력)
4

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

    static int n, m, cnt;
    static int[][] arr;
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};
    static Queue<Point> queue = new LinkedList<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        m = scanner.nextInt(); // col
        n = scanner.nextInt(); // row
        cnt = 0;
        arr = new int[n][m]; // [row][col]

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int value = scanner.nextInt();
                arr[i][j] = value;
                if (value == 1)
                    queue.offer(new Point(i, j)); // row, col
            }
        }

        BFS();

        boolean isExistNotRipe = false;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (arr[i][j] == 0)
                    isExistNotRipe = true;
            }
        }

        if (isExistNotRipe) {
            System.out.println(-1);
        } else {
            System.out.println(cnt);
        }
    }

    private static void BFS() {

        while (!queue.isEmpty()) {
            int size = queue.size();
            boolean isNewRipe = false;
            for (int i = 0; i < size; i++) {
                Point point = queue.poll();
                int curX = point.x; // row
                int curY = point.y; // col
                for (int dir = 0; dir < 4; dir++) {
                    int newX = curX + dx[dir];
                    int newY = curY + dy[dir];
                    if (newX < 0 || newY < 0 || newX >= n || newY >= m) {
                        continue;
                    } else if (arr[newX][newY] == 0) {
                        arr[newX][newY] = 1;
                        isNewRipe = true;
                        queue.offer(new Point(newX, newY));
                    }
                }
            }
            if(isNewRipe) cnt++;
        }

    }


    private static class Point {
        public int x, y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}
profile
멋있는 백엔드 개발자 / 꾸준히 의미있게!

0개의 댓글