1+ #include < iostream>
2+ #include < vector>
3+ #include < queue>
4+ #include < cstring>
5+
6+ using namespace std ;
7+
8+ #define ROW 12
9+ #define COL 6
10+
11+ char maps[ROW][COL];
12+ bool visited[ROW][COL];
13+
14+ int dx[] = {0 , 0 , 1 , -1 };
15+ int dy[] = {1 , -1 , 0 , 0 };
16+
17+ bool flag = false ;
18+ int answer = 0 ;
19+ int temp_cnt = 0 ;
20+
21+ queue<pair<int , int >> q;
22+ vector<pair<int , int >> puyo_temp, puyo_vec;
23+
24+ void input () {
25+ for (int i = 0 ; i < ROW; i++) {
26+ for (int j = 0 ; j < COL; j++) {
27+ cin >> maps[i][j];
28+ }
29+ }
30+ }
31+
32+ void bfs () {
33+ while (!q.empty ()) {
34+ int x = q.front ().first ;
35+ int y = q.front ().second ;
36+ q.pop ();
37+ visited[x][y] = true ;
38+ puyo_temp.push_back (make_pair (x, y));
39+
40+ for (int i = 0 ; i < 4 ; i++) {
41+ int nx = x + dx[i];
42+ int ny = y + dy[i];
43+
44+ if (nx < 0 || ny < 0 || nx >= ROW || ny >= COL) continue ;
45+ if (maps[nx][ny] == ' .' ) continue ;
46+ if (visited[nx][ny] == true ) continue ;
47+ if (maps[nx][ny] != maps[x][y]) continue ;
48+
49+ temp_cnt += 1 ;
50+ visited[nx][ny] = true ;
51+ q.push (make_pair (nx, ny));
52+ }
53+ }
54+ }
55+
56+ void update () {
57+ for (int i = 0 ; i < puyo_vec.size (); i++) {
58+ int x = puyo_vec[i].first ;
59+ int y = puyo_vec[i].second ;
60+
61+ maps[x][y] = ' .' ;
62+ }
63+ }
64+
65+ void down () {
66+ for (int i = ROW - 1 ; i >= 0 ; i--) {
67+ for (int j = COL - 1 ; j >= 0 ; j--) {
68+ if (maps[i][j] == ' .' ) continue ;
69+ int temp = i;
70+
71+ while (1 ) {
72+ // temp๊ฐ ๋๊น์ง ๋ด๋ ค์๊ฑฐ๋, ๋ค์ ์นธ์ Puyo๊ฐ ์๊ฑฐ๋
73+ if (temp == ROW - 1 || maps[temp + 1 ][j] != ' .' ) break ;
74+
75+ maps[temp + 1 ][j] = maps[temp][j];
76+ maps[temp][j] = ' .' ;
77+ temp++;
78+ }
79+ }
80+ }
81+ }
82+
83+ int main () {
84+ input ();
85+
86+ while (1 ) {
87+ flag = false ;
88+ memset (visited, false , sizeof (visited));
89+ puyo_vec.clear ();
90+
91+ for (int i = 0 ; i < ROW; i++) {
92+ for (int j = 0 ; j < COL; j++) {
93+ // '.' ์ด๊ฑฐ๋ ๋ฐฉ๋ฌธํ ๊ณณ์ ์คํต
94+ if (maps[i][j] == ' .' || visited[i][j]) continue ;
95+
96+ temp_cnt = 1 ;
97+ q.push (make_pair (i, j));
98+ bfs ();
99+
100+ if (temp_cnt >= 4 ) {
101+ flag = true ; // ํ!
102+
103+ // ์ขํ ์ฎ๊ธฐ๊ธฐ: puyo_temp => puyo_vec
104+ for (int k = 0 ; k < puyo_temp.size (); k++) {
105+ puyo_vec.push_back (puyo_temp[k]);
106+ }
107+ }
108+ puyo_temp.clear ();
109+ }
110+ }
111+ update (); // ํฐ์ง ๋ฟ์ ์์ ๊ธฐ
112+ down (); // ๊ณต๊ฐ ์๋๋ก ๋ด๋ฆฌ๊ธฐ
113+
114+ if (flag == true ) answer++;
115+ else break ;
116+ }
117+
118+ cout << answer;
119+
120+ return 0 ;
121+ }
0 commit comments