코드와이
[BAEKJOON] 7569. 토마토 본문
문제링크
7569번: 토마토
첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100,
www.acmicpc.net
package acmicpc.Silver1;
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 토마토 {
static int m, n, arr[][], zeroNum, ans;
static class Point{
int r;
int c;
int num;
public Point(int r, int c, int num) {
super();
this.r = r;
this.c = c;
this.num = num;
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
m = Integer.parseInt(st.nextToken());
n = Integer.parseInt(st.nextToken());
arr = new int[n][m];
zeroNum = 0;
ans = 1;
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());
if(arr[i][j] == 0) zeroNum++;
}
}
bfs();
System.out.println(zeroNum > 0 ? -1 : ans-1);
}
static int[] dr = {-1,1,0,0};
static int[] dc = {0,0,1,-1};
private static void bfs() {
Queue<Point> queue = new LinkedList<>();
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(arr[i][j] == 1) {
queue.add(new Point(i,j,1));
}
}
}
while(!queue.isEmpty()) {
Point p = queue.poll();
for(int d = 0 ; d < 4 ; d++) {
int nr = p.r + dr[d];
int nc = p.c + dc[d];
if(nr < 0 || nc < 0 || nr >= n || nc >= m || arr[nr][nc] > p.num) continue;
if(arr[nr][nc] == 0) {
zeroNum--;
arr[nr][nc] = p.num + 1;
queue.add(new Point(nr, nc, p.num + 1));
ans = Math.max(ans, p.num+1);
}
}
if(zeroNum == 0) {
return;
}
}
return;
}
}
'acmicpc' 카테고리의 다른 글
[BAEKJOON] 16236. 아기 상어 (0) | 2021.04.16 |
---|---|
[BAEKJOON] 16235. 나무 재테크 (0) | 2021.04.16 |
[BAEKJOON] 16973. 직사각형 탈출 (0) | 2021.04.15 |
[BAEKJOON] 11052. 카드 구매하기 (0) | 2021.04.15 |
[BAEKJOON] 1194. 달이 차오른다, 가자. (0) | 2021.04.14 |