문제
https://school.programmers.co.kr/learn/courses/30/lessons/17679
코드
import java.util.*;
class Solution {
public int solution(int m, int n, String[] board) {
int answer = 0;
char [][] map = new char [m][n];
for(int i=0;i<m;i++){
map[i] = board[i].toCharArray();
}
while(true){
boolean flag = false;
boolean check [][] = new boolean [m][n];
for(int i=0;i<m-1;i++){
for(int j=0;j<n-1;j++){
if(map[i][j] != '.'
&& map[i][j] == map[i+1][j]
&& map[i][j] == map[i][j+1]
&& map[i][j] == map[i+1][j+1]){
flag = true;
check[i][j] = true;
check[i][j+1] = true;
check[i+1][j] = true;
check[i+1][j+1] = true;
}
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(check[i][j]) {
map[i][j] = '.';
answer += 1;
}
}
}
if(!flag) break;
for(int j=0;j<n;j++){
Queue<Character> que = new LinkedList<>();
for(int i=m-1;i>=0;i--){
if(map[i][j] != '.')
que.add(map[i][j]);
}
int idx = m-1;
while(!que.isEmpty()){
map[idx][j] = que.poll();
idx -= 1;
}
for(int i=idx;i>=0;i--){
map[i][j] = '.';
}
}
}
return answer;
}
}