Lined Notebook

[Gold 5] [Java] Puyo Puyo (11559 번)

by HeshAlgo

Java Code

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.baekjoon.java;
 
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
 
public class Main {
    static final int R = 12;
    static final int C = 6;
    static int[] dx = {-1010};
    static int[] dy = {010-1};
    static int answer, cnt;
    static Queue<Point> q;
    static char[][] map;
    static boolean[][] visit;
    static boolean flag, check;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        map = new char[R][C];
        q = new LinkedList<Point>();
        // 뿌요뿌여 인풋 입력
        for (int row = 0; row < R; row++) {
            String input = br.readLine();
            for (int col = 0; col < C; col++) {
                map[row][col] = input.charAt(col);
            }
        }
        check = true;
        
        while (check) {
            check = false;
            
            for (int row = 0; row < R; row++) {
                for (int col = 0; col < C; col++) {
                    if (map[row][col] != '.') {
                        visit = new boolean[R][C];
                        // dfs() => 뿌요뿌요가 4개 이상 모여 있는지 체크 
                        q.add(new Point(row, col));
                        cnt = 0;
                        bfs(row, col, map[row][col]);
                        // 뿌요뿌요가 4개 이상 모여있는 경우
                        if (flag) {
                            // 뿌요 없애기
                            delete();
                            flag = false;
                            check = true;
                        } 
                    
                    }
                }
            }
            
            // 뿌요뿌요를 밑으로 내려주기
            move();
            if (check) {
                  answer++;    
            }
            
        }
        
        System.out.println(answer);
        
    }
 
    private static void delete() {
        for (int row = 0; row < R; row++) {
            for (int col = 0; col < C; col++) {
                // 방문의 흔적이 있는 경우 .으로 변경
                if (visit[row][col]) {
                    map[row][col] = '.';
                }
            }
        }
        
    }
 
    private static void move() {
        int cnt = R - 1;
        // 최대 11번 내릴 수 있으므로
        while (cnt-- > 0) {
            for (int row = R - 2; row >= 0; row--) {
                for (int col = 0; col < C; col++) {
                    // 현재 위치가 알파벳이고 밑에가 .인 경우 뿌요를 내리기
                    if (map[row][col] != '.' && map[row + 1][col] == '.') {
                        map[row + 1][col] = map[row][col];
                        map[row][col] = '.';
                    }
                    
                }
            }
            
        }
    }
 
    private static void bfs(int row, int col, char color) {
        while (!q.isEmpty()) {
            Point point = q.poll();
            int x = point.x;
            int y = point.y;
            
            if (cnt >= 4) {
                flag = true
            }
            
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                
                if (0 <= nx && nx < R && 0 <= ny && ny < C) {
                    if (!visit[nx][ny] && map[nx][ny] == color) {
                        visit[nx][ny] = true;
                        cnt += 1;
                        q.add(new Point(nx, ny));
                    }
                }
            }    
        }
        
    }
 
}
 
 
cs

 

블로그의 정보

꾸준히 공부하는 개발 노트

HeshAlgo

활동하기