Boj 7576 토마토
Updated:
문제 링크 : 백준 https://www.acmicpc.net/problem/7576
문제풀이
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Boj_7576 {
static int[] dx = { -1, 1, 0, 0 };
static int[] dy = { 0, 0, -1, 1 };
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
public static void main(String args[]) throws Exception {
st = new StringTokenizer(br.readLine());
int M = Integer.parseInt(st.nextToken());
int N = Integer.parseInt(st.nextToken());
int[][] arr = new int[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
//----------------- 입력 부 ------------------
BFS(arr, N, M);
}
public static void BFS(int[][] arr, int N, int M) {
Queue<DOT> queue = new LinkedList<DOT>();
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (arr[i][j] == 1)
//익은 토마토가 있는 모든 위치를 큐에 담는다.
queue.add(new DOT(i, j));
}
}
while (!queue.isEmpty()) {
//익은 토마토의 상하좌우는 다음에 익기 때문에 큐에 담아야한다.
DOT dot = queue.poll();
for (int i = 0; i < 4; i++) {
int nextX = dot.x + dx[i];
int nextY = dot.y + dy[i];
//범위 밖 패스
if (nextX < 0 || nextY < 0 || nextX >= N || nextY >= M) {
continue;
}
//다음 위치가 익지 않은 토마토가 아니면 패스
if (arr[nextX][nextY] != 0) {
continue;
}
//최대 일수를 구하기 때문에 1로 바꾸는 것이 아니라 현재 일수 +1 을 해줘야한다.
arr[nextX][nextY] = arr[dot.x][dot.y] + 1;
queue.add(new DOT(nextX, nextY));
}
print(arr, N, M); // 농장 전체 출력
System.out.println();
}
int max = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (arr[i][j] == 0) {
//토마토가 모두 익지 못한 상황이라면 -1 을 출력한다.
System.out.println(-1);
return;
}
max = Math.max(max, arr[i][j]);
}
}
//그렇지 않다면 최대값을 출력한다.
System.out.println(max - 1);
}
//농장을 전체 보여주는 함수
public static void print(int[][] arr, int N, int M) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
class DOT {
int x;
int y;
DOT(int x, int y) {
this.x = x;
this.y = y;
}
}
Leave a comment