시뮬레이션 문제입니다.
4개 이상의 인접한 색이 있을 경우 해당 색 블럭들은 터지게됩니다.
모든 위치에 동시에 터지게 되고 이를 1번의 연쇄로 처리합니다.
처리가 된 후 중력이 적용되어 공중에 있는 블럭들은 아래로 내려갑니다.
터질 수 있는 경우가 없을 때 까지 반복하여 몇번의 연쇄가 발생하는지 출력해줍니다.
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader br;
static BufferedWriter bw;
static StringTokenizer st;
static int n;
static int m;
static int cnt;
static int result;
static int bombCnt;
static String str;
static int[][] map;
static int[][] visit;
public static void main(String[] args) throws Exception {
init();
simulation();
}
public static void init() throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
n = 12;
m = 6;
result = 0;
bombCnt = 1;
map = new int[n][m];
visit = new int[n][m];
for (int i = 0; i < n; ++i) {
str = br.readLine();
for (int j = 0; j < m; ++j) {
char c = str.charAt(j);
if (c == '.') {
map[i][j] = 0;
} else if (c == 'R') {
map[i][j] = 1;
} else if (c == 'G') {
map[i][j] = 2;
} else if (c == 'B') {
map[i][j] = 3;
} else if (c == 'Y') {
map[i][j] = 4;
} else if (c == 'P') {
map[i][j] = 5;
}
}
}
}
public static void simulation() throws Exception {
while (bombCnt > 0) {
find();
gravity();
}
print();
}
public static void gravity() {
if (bombCnt == 0) {
return;
}
for (int j = 0; j < m; ++j) {
int index = -1;
for (int i = n - 1; i >= 0; --i) {
if (index == -1 && map[i][j] == 0) {
index = i;
}
if (index != -1 && map[i][j] > 0) {
map[index][j] = map[i][j];
map[i][j] = 0;
--index;
}
}
}
}
public static void find() {
bombCnt = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (map[i][j] > 0) {
cnt = 1;
initVisit();
bomb(i, j, map[i][j]);
if (cnt >= 4) {
++bombCnt;
remove(i, j, map[i][j]);
}
}
}
}
result = bombCnt > 0 ? result + 1 : result;
}
public static void initVisit() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
visit[i][j] = 0;
}
}
}
public static void remove(int y, int x, int t) {
int[] my = { 0, 1, 0, -1 };
int[] mx = { 1, 0, -1, 0 };
map[y][x] = 0;
for (int i = 0; i < 4; ++i) {
int nextY = y + my[i];
int nextX = x + mx[i];
if (nextY < 0 || nextX < 0 || nextY >= n || nextX >= m || map[nextY][nextX] != t) {
continue;
}
remove(nextY, nextX, t);
}
}
public static void bomb(int y, int x, int t) {
int[] my = { 0, 1, 0, -1 };
int[] mx = { 1, 0, -1, 0 };
visit[y][x] = 1;
for (int i = 0; i < 4; ++i) {
int nextY = y + my[i];
int nextX = x + mx[i];
if (nextY < 0 || nextX < 0 || nextY >= n || nextX >= m || map[nextY][nextX] != t || visit[nextY][nextX] == 1) {
continue;
}
++cnt;
bomb(nextY, nextX, t);
}
}
public static void print() throws Exception {
bw.write(result + "\n");
bw.close();
}
}
'알고리즘' 카테고리의 다른 글
[Java] 백준 22943번 수 (0) | 2023.07.15 |
---|---|
[Java] 백준 12869번 뮤탈리스크 (0) | 2023.07.13 |
[Java] 백준 18188번 다오의 데이트 (0) | 2023.07.12 |
[Java] 백준 24884번 장작 넣기 (0) | 2023.07.12 |
[Java] 백준 2110번 공유기 설치 (0) | 2023.07.11 |