코딩테스트/[백준] 코딩테스트 연습
아기 상어 - 16236번
쵼쥬
2022. 2. 23. 10:51
내 코드
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 = null;
int N = Integer.parseInt(br.readLine());
int[][] d = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
int[][] arr = new int[N][N];
PriorityQueue<Shark> pq = new PriorityQueue<>();
boolean[][] visited = new boolean[N][N];
int time = 0;
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
if (arr[i][j] == 9) {
pq.add(new Shark(i, j, 2, 0, 0));
visited[i][j] = true;
arr[i][j] = 0;
}
}
}
while (!pq.isEmpty()) {
Shark s = pq.poll();
if (arr[s.x][s.y] > s.size) {
continue;
}
if (arr[s.x][s.y] != 0 && arr[s.x][s.y] < s.size) {
visited = new boolean[N][N];
pq.clear();
time = s.sec;
s.count++;
arr[s.x][s.y] = 0;
if (s.count == s.size) {
s.count = 0;
s.size++;
}
}
for (int i = 0; i < 4; i++) {
int nextX = s.x + d[i][0];
int nextY = s.y + d[i][1];
if (nextX >= 0 && nextX < N && nextY >= 0 && nextY < N && !visited[nextX][nextY]) {
visited[nextX][nextY] = true;
pq.add(new Shark(nextX, nextY, s.size, s.sec + 1, s.count));
}
}
}
System.out.println(time);
}
static class Shark implements Comparable<Shark> {
int x, y, size, sec, count;
public Shark(int x, int y, int size, int sec, int count) {
this.x = x;
this.y = y;
this.size = size;
this.sec = sec;
this.count = count;
}
@Override
public int compareTo(Shark o) {
if (sec == o.sec) {
if (o.x == x) {
return y - o.y;
}
return x - o.x;
}
return sec - o.sec;
}
}
}