# 유형 : 탐색 + 시뮬레이션

# 조건문만 잘 따져서 구현하면 어렵지 않은 문제이다, 코드를 더 간결화 시킬 수 있을 것 같은데.. 귀찮다.

# 연결을 체크해주는 탐색 메소드 + 연결을 체크해주는 메소드를 통해 얻은 삭제할 대상을 삭제해주는 메소드 + 삭제하고 난 후 칸을 땡겨주는 메소드 3개를 구현하면 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package bj;
 
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
 
public class p11559 {
    
    static char arr[][] = new char[12][6];
    static int moveX[] = {0,1,0,-1};
    static int moveY[] = {-1,0,1,0};
    static ArrayList<Point> deleteList;
    static boolean visit[][];
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        int result=0;
        for(int i=0; i<12; i++) {
            str = br.readLine();
            for(int j=0; j<6; j++) {
                arr[i][j] = str.charAt(j);
            }
        }
        while(true) {
            deleteList = new ArrayList<>();
            visit = new boolean[12][6];
            
            for(int i=11; i>=0; i--) {
                for(int j=0; j<6; j++) {
                    if(arr[i][j] != '.' && !visit[i][j] && check(i,j) >= 4) {
                        deleteList.add(new Point(j,i));
                    }
                }
            }
            if(deleteList.size()==0)
                break;
            for(int i=0; i<deleteList.size(); i++) {
                delete(deleteList.get(i).y, deleteList.get(i).x);
            }
            process();
            result++;
            
        }
        
        System.out.println(result);
    }
    public static void process() {
        for(int i=10; i>=0; i--) {
            for(int j=0; j<6; j++) {
                int next=i;
                boolean isTrue=false;
                while(next<11 && arr[next+1][j] == '.') {
                    next++;
                    isTrue=true;
                }
                if(isTrue) {
                    char tmp = arr[i][j];
                    arr[next][j] = tmp;
                    arr[i][j] = '.';
                }
                
            }
        }
    }
    public static void delete(int i, int j) {
        char ch = arr[i][j];
        Queue<Point> queue = new LinkedList<>();
        queue.add(new Point(j,i));
        while(!queue.isEmpty()) {
            Point p = queue.poll();
            int y = p.y;
            int x = p.x;
            arr[y][x] = '.';
            for(int d=0; d<4; d++) {
                int newY = y + moveY[d];
                int newX = x + moveX[d];
                if(0<=newY && newY<12 && 0<=newX && newX<6 && arr[newY][newX]==ch) {
                    queue.add(new Point(newX,newY));
                }
            }
        }
    }
    
    public static int check(int i, int j) {
        char ch = arr[i][j];
        int rs=0;
        Queue<Point> queue = new LinkedList<Point>();
        queue.add(new Point(j,i));
        while(!queue.isEmpty()) {
            
            Point p = queue.poll();
            visit[p.y][p.x] = true;
            rs++;
            
            for(int d=0; d<4; d++) {
                int newY = p.y + moveY[d];
                int newX = p.x + moveX[d];
                if(0<=newY && newY<12 && 0<=newX && newX<6 && !visit[newY][newX] && arr[newY][newX] == ch) {
                    queue.add(new Point(newX,newY));
                }
            }
        }
        return rs;
    }
}
 
cs

+ Recent posts