내 코드
package com.company;
import java.io.*;
import java.util.*;
/**
* 먼저 호수가 물로 변하는 시간을 저장
* 백조가 최소로 움직이는 날 구함
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int R = Integer.parseInt(st.nextToken());
int C = Integer.parseInt(st.nextToken());
int[] birdA = new int[3];
int[] birdB = new int[2];
int[][] d = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int[][] arr = new int[R][C];
PriorityQueue<int[]> q = new PriorityQueue<>((o1, o2) -> o1[2] - o2[2]);
boolean[][] visited = new boolean[R][C];
for (int i = 0; i < R; i++) {
String s = br.readLine();
for (int j = 0; j < C; j++) {
char c = s.charAt(j);
if (c != 'X') {
q.add(new int[]{i, j, 0});
visited[i][j] = true;
if (c == 'L') {
if (birdA[0] == 0) {
birdA[0] = i;
birdA[1] = j;
} else {
birdB[0] = i;
birdB[1] = j;
}
}
}
}
}
// 호수가 물로 변하는 시간 찾음
while (!q.isEmpty()) {
int[] node = q.poll();
for (int i = 0; i < 4; i++) {
int nextX = d[i][0] + node[0];
int nextY = d[i][1] + node[1];
if (nextX >= 0 && nextY >= 0 && nextX < R && nextY < C && !visited[nextX][nextY]) {
visited[nextX][nextY] = true;
arr[nextX][nextY] = node[2] + 1;
q.add(new int[]{nextX, nextY, node[2] + 1});
}
}
}
visited = new boolean[R][C];
visited[birdA[0]][birdA[1]] = true;
q.add(birdA);
// 새가 만나는 시간 찾음
while (!q.isEmpty()) {
int[] node = q.poll();
if (node[0] == birdB[0] && node[1] == birdB[1]) {
System.out.println(node[2]);
break;
}
for (int i = 0; i < 4; i++) {
int nextX = d[i][0] + node[0];
int nextY = d[i][1] + node[1];
if (nextX >= 0 && nextY >= 0 && nextX < R && nextY < C && !visited[nextX][nextY]) {
visited[nextX][nextY] = true;
q.add(new int[]{nextX, nextY, Math.max(node[2], arr[nextX][nextY])});
}
}
}
}
}
'코딩테스트 > [백준] 코딩테스트 연습' 카테고리의 다른 글
녹색 옷 입은 애가 젤다지? - 4485번 (0) | 2022.04.01 |
---|---|
달이 차오른다, 가자. - 1194번 (0) | 2022.04.01 |
경사로 - 14890번 (0) | 2022.03.30 |
가운데를 말해요 - 1655 (0) | 2022.03.27 |
주사위 굴리기 - 14499번 (0) | 2022.03.22 |