https://www.acmicpc.net/problem/2573
ํด๋น ๋ฌธ์ ๋ 1๋ ๋จ์๋ก ๊ฐ๊ฐ์ ๊ตฌ์ญ์ ์ธ์ ํ ๋ฐ๋ค์ ์นธ ๋งํผ์ ๋์ด๊ฐ ๊ฐ์ํ๊ฒ ํ๊ฒ ํ๋ฉฐ ๋น์ฐ์ด 2์กฐ๊ฐ ์ด์์ผ๋ก ์ชผ๊ฐ์ง ๊ฒฝ์ฐ ๊ทธ๋๊น์ง ๋ช๋ ์ด ๊ฑธ๋ ธ๋์ง ์ฒดํฌํ๋ ๋ฌธ์ ์ ๋๋ค.
์ต๊ทผ ์ฐํ
์ฝ ํ๋ฆฌ์ฝ์ค๋ฅผ ํตํด ๋ฐฐ์ด ๋ด์ฉ์ ๋ฐํ์ผ๋ก ๊ธฐ๋ฅ๋ณ๋ก ๋ฉ์๋๋ฅผ ๋ถ๋ฆฌํ์ฌ ๋ฌธ์ ๋ฅผ ํ์ด๋ณด๋ฉฐ main
๋ฉ์๋๋ฅผ ์ ์ธํ 5๊ฐ์ ๋ฉ์๋๋ฅผ ์์ฑํ์ฌ ํ์ดํ์์ต๋๋ค.
๊ฐ๊ฐ์ ๋ฉ์๋๋ ๋ค์๊ณผ ๊ฐ์ ์ญํ ์ ํฉ๋๋ค.
meltForYear()
: melt
๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ํ ํด๊ฐ ์ง๋จ์ ๋ฐ๋ฅธ ๋น์ฐ์ ๋์ด(matrix)๋ฅผ ์
๋ฐ์ดํธ ํด์ค๋ค.
melt()
: ํด๋น ์นธ์ ์ธ์ ํ ๋ฐ๋ท๋ฌผ ์นธ์ ์ฒดํฌํ๋ฉฐ ๋ณํ๋ ๋น์ฐ์ ๋์ด๋ฅผ returnํด์ค๋ค.
checkSeparate()
: ๋น์ฐ์ด ๋ ๋ฉ์ด ์ด์์ผ๋ก ๋๋์๋์ง ์ฒดํฌ๋ฅผ ํ๋ ๋ฉ์๋์
๋๋ค.
dfs()
: checkSeparate()
์์ ํธ์ถ๋์ด ๋ฐ๋ค๊ฐ ์๋ ์ธ์ ํ ๋น์ฐ ์นธ๋ค์ ์ํํ๋ฉฐ ์ฒดํฌํ๋ค.
checkAllMelt()
: ๋น์ฐ์ด ๋ชจ๋ ๋
น์๋์ง ์ฒดํฌํด์ฃผ๋ ์ญํ ์ ํด์ค๋๋ค.
์ฒ์ ๋ฌธ์ ๋ฅผ ํ๊ณ ๋์ ์๊ฐ์ด๊ณผ๊ฐ ๋์์ ๋ง์ ์๊ฐ์ ์๋ชจํ์์ต๋๋ค.
์ฒซ ํ์ด์ ์๊ฐ์ด๊ณผ ์ด์ ๋checkAllMelt()
๋ฉ์๋์ ๋ถ์ฌ๋ก ๋น์ฐ์ด ๋ชจ๋ ๋ น์ ๋๊น์ง ๋ ๋ฉ์ด ์ด์์ ๋น์ฐ์ผ๋ก ๋ถ๋ฆฌ๋์ง ์๊ธฐ ๋๋ฌธ์ ๋ฉ์ธ ๋ฉ์๋๊ฐ ๋ฌดํ ๋ฃจํ์ ๋น ์ง๊ฒ ๋์ด ์๊ฐ์ด๊ณผ๋ผ๋ ๊ฒฐ๊ณผ๊ฐ ๋์์ต๋๋ค.
๐ก ๋ฌธ์ ๋ฅผ ํ๊ณ ์๊ฐ์ด๊ณผ๊ฐ ๋์จ๋ค๋ฉด ์ ์ฒ๋ผ ๋ฌธ์ ๋ฅผ ๋ค์ ์ฝ๊ณ ๋์น ์กฐ๊ฑด์ด ์๋์ง ์ฒดํฌํ์๊ธฐ ๋ฐ๋๋๋ค.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N;
static int M;
static int[][] matrix;
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, -1, 0, 1};
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());
matrix = new int[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
matrix[i][j] = Integer.parseInt(st.nextToken());
}
}
int result = 0;
while (!checkSeparate()) {
meltForYear();
result++;
if (checkAllMelt()) {
result = 0;
break;
}
}
System.out.println(result);
}
// 1๋
๋จ์๋ก ๋์ด ์ถ์ํ๋ ๋ฉ์๋
private static void meltForYear() {
int[][] tempMatrix = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (matrix[i][j] > 0)
tempMatrix[i][j] = melt(i, j);
}
}
matrix = tempMatrix;
}
// ๊ฐ๊ฐ์ ์นธ์ ๋
น์ด๋ ๋ฉ์๋
private static int melt(int y, int x) {
int count = 0;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= M || ny < 0 || ny >= N || matrix[ny][nx] == 0) {
count++;
}
}
if (matrix[y][x] >= count) {
return matrix[y][x] - count;
}
return 0;
}
// ๋ ๋ฉ์ด ์ด์์ผ๋ก ๋๋์๋์ง ์ฒดํฌํ๋ ๋ฉ์๋
private static boolean checkSeparate() {
boolean[][] checked = new boolean[N][M];
int count = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (matrix[i][j] > 0 && !checked[i][j]) {
dfs(i, j, checked);
count++;
}
if (count >= 2) return true;
}
}
return false;
}
// dfs ํ์ ๋ฉ์๋
private static void dfs(int y, int x, boolean[][] checked) {
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= M || ny < 0 || ny >= N || matrix[ny][nx] == 0 || checked[ny][nx]) {
continue;
}
checked[ny][nx] = true;
dfs(ny, nx, checked);
}
}
// ๋น์ฐ์ด ๋ชจ๋ ๋
น์๋์ง ์ฒดํฌ
private static boolean checkAllMelt() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (matrix[i][j] != 0) {
return false;
}
}
}
return true;
}
}