지구 온난화로 인하여 북극의 빙산이 녹고 있다. 빙산을 그림 1과 같이 2차원 배열에 표시한다고 하자. 빙산의 각 부분별 높이 정보는 배열의 각 칸에 양의 정수로 저장된다. 빙산 이외의 바다에 해당되는 칸에는 0이 저장된다. 그림 1에서 빈칸은 모두 0으로 채워져 있다고 생각한다.
그림 1. 행의 개수가 5이고 열의 개수가 7인 2차원 배열에 저장된 빙산의 높이 정보
빙산의 높이는 바닷물에 많이 접해있는 부분에서 더 빨리 줄어들기 때문에, 배열에서 빙산의 각 부분에 해당되는 칸에 있는 높이는 일년마다 그 칸에 동서남북 네 방향으로 붙어있는 0이 저장된 칸의 개수만큼 줄어든다. 단, 각 칸에 저장된 높이는 0보다 더 줄어들지 않는다. 바닷물은 호수처럼 빙산에 둘러싸여 있을 수도 있다. 따라서 그림 1의 빙산은 일년후에 그림 2와 같이 변형된다.
그림 3은 그림 1의 빙산이 2년 후에 변한 모습을 보여준다. 2차원 배열에서 동서남북 방향으로 붙어있는 칸들은 서로 연결되어 있다고 말한다. 따라서 그림 2의 빙산은 한 덩어리이지만, 그림 3의 빙산은 세 덩어리로 분리되어 있다.
한 덩어리의 빙산이 주어질 때, 이 빙산이 두 덩어리 이상으로 분리되는 최초의 시간(년)을 구하는 프로그램을 작성하시오. 그림 1의 빙산에 대해서는 2가 답이다. 만일 전부 다 녹을 때까지 두 덩어리 이상으로 분리되지 않으면 프로그램은 0을 출력한다.
우선 입력을 받으면서 icebergList에 빙산의 위치를 넣어준다.
메소드 설명을 통해 설명하겠다.
detachCheck메소드는 빙산의 분리를 체크하는 메소드로 icebergList를 탐색하면서 bfs를 실행한다.
bfs가 실행된 수 만큼이 분리의 수이다.
iceBurgCheck메소드는 빙산이 녹은 후의 남아있는 빙산의 icebergList를 만들어준다.
위 두개의 메소드가 반복되면서 detachCheck 가 2 이상이면 count를 반환
2가 되기전에 icebergList가 empty가 되면 0을 반환하게 해주었다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static List<Node> icebergList = new LinkedList<>();
static int[][] map;
static int[] dx = {0, 1, 0, -1};
static int[] dy = {1, 0, -1, 0};
static int N, M;
static boolean[][] visit;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new int[N][M];
for (int i = 0; i < N; i++) {
String[] a = br.readLine().split(" ");
for (int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(a[j]);
if (map[i][j] != 0) {
icebergList.add(new Node(i, j, 0));
}
}
}
int count = 0;
iceBurgCheck();
while (true) {
if (icebergList.isEmpty()) {
count = 0;
break;
}
if (detachCheck() >= 2) break;
iceBurgCheck();
count++;
}
System.out.println(count);
}
private static int detachCheck() {
visit = new boolean[N][M];
int size = icebergList.size();
int count = 0;
while (size-- > 0) {
Node cur = icebergList.remove(0);
if (!visit[cur.x][cur.y]) {
count++;
bfs(cur);
}
icebergList.add(cur);
}
return count;
}
private static void bfs(Node node) {
visit[node.x][node.y] = true;
Queue<Node> bfsQ = new LinkedList<>();
bfsQ.offer(node);
while (!bfsQ.isEmpty()) {
Node cur = bfsQ.poll();
for (int i = 0; i < 4; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (!visit[nx][ny] && map[nx][ny] != 0) {
visit[nx][ny] = true;
bfsQ.offer(new Node(nx, ny, 0));
}
}
}
}
private static void iceBurgCheck() {
int size = icebergList.size();
while (size-- > 0) {
Node cur = icebergList.remove(0);
map[cur.x][cur.y] = Math.max(0, map[cur.x][cur.y] - cur.num);
if (map[cur.x][cur.y] != 0) {
icebergList.add(new Node(cur.x, cur.y, 0));
}
}
size = icebergList.size();
while (size-- > 0) {
Node cur = icebergList.remove(0);
int sum = 0;
for (int i = 0; i < 4; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (map[nx][ny] == 0) sum++;
}
icebergList.add(new Node(cur.x, cur.y, sum));
}
}
static class Node {
int x;
int y;
int num;
public Node(int x, int y, int num) {
this.x = x;
this.y = y;
this.num = num;
}
}
}
오랜만에 풀어본 구현문제다.
정말 오래걸렸다..ㅠㅠ