기술 블로그

11559번 Puyo Puyo 본문

알고리즘 문제/BOJ

11559번 Puyo Puyo

parkit 2019. 5. 18. 23:49
728x90
반응형

https://www.acmicpc.net/problem/11559


지난 번 풀이 : https://hsdevelopment.tistory.com/9 


좋은 문제여서, 다시 풀어보았다.



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
#include <bits/stdc++.h>
 
using namespace std;
 
char Map[13][7];
 
bool visit[13][7= { false, };
 
int dy[4= { 010-1 };
int dx[4= { 10-10 };
 
vector<pair<intint> > v;
 
int dfs(int r, int c, char color)
{
    int ret = 1;
 
    v.push_back({ r, c });
 
    visit[r][c] = true;
 
    for (int i = 0; i < 4; i++)
    {
        int y = r + dy[i];
        int x = c + dx[i];
 
        if (y < 0 || y >= 12 || x < 0 || x >= 6 || visit[y][x] || Map[y][x] != color) continue;
 
        ret += dfs(y, x, color);
    }
 
    return ret;
}
 
void update()
{
    for (int j = 0; j < 6; j++)
        for (int i = 11; i >= 0; i--)
        {
            if (Map[i][j] == '.'continue;
 
            int y = i;
 
            while (1)
            {
                ++y;
 
                if (Map[y][j] != '.')
                {
                    --y;
                    swap(Map[y][j], Map[i][j]);
                    break;
                }
 
                if (y >= 12break;
            }
        }
}
 
int simulation()
{
    int ret = 0;
 
    while (1)
    {        
        bool stop = true;
 
        memset(visit, falsesizeof(visit));
 
        for (int i = 0; i < 12; i++)
            for (int j = 0; j < 6; j++)        
                if (Map[i][j] != '.' && !visit[i][j])
                {
                    if (dfs(i, j, Map[i][j]) >= 4)
                    {
                        stop = false;
                        for (auto a : v) Map[a.first][a.second] = '.';
                    }
 
                    v.clear();
                }
 
        if (!stop) update();                            
        else break;
 
        ++ret;
    }
    
    return ret;
}
 
int main(void)
{
    for (int i = 0; i < 12; i++scanf("%s", Map[i]);
    
    printf("%d\n", simulation());
 
    return 0;
}
cs















728x90
반응형

'알고리즘 문제 > BOJ' 카테고리의 다른 글

16932번 모양 만들기  (0) 2019.05.20
4991번 로봇 청소기  (0) 2019.05.19
14655번 욱제는 도박쟁이야!!  (0) 2019.05.10
11441번 합 구하기  (0) 2019.05.09
1867번 돌멩이 제거  (0) 2019.05.02