ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 16768, Gold 4
    백준 2021. 9. 21. 16:44
     

    16768번: Mooyo Mooyo

    In the example above, if $K = 3$, then there is a connected region of size at least $K$ with color 1 and also one with color 2. Once these are simultaneously removed, the board temporarily looks like this: 0000000000 0000000300 0054000300 1054500030 220000

    www.acmicpc.net

    문제

    • 같은 숫자가 $k$개 이상 인접하면 사라지고 아래로 내려온다. 최종 모습은?

     

    풀이

    ※ 마킹한 1은 미리 0으로 바꾸지 않는다(변화는 항상 마지막에)
    화살표를 따라가다가 0을 만나면 열을 바꾸도록 설계되었기 때문에 아래와 같은경우 제대로 동작하지 않는다

    이런식으로 구성되면 2에서 bfs 진행x

    인접한 $k$개 이상의 수를 모두 찾으면 모두 0으로 바꾸고 모두 아래로 내린다(반복)

    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
    #include <bits/stdc++.h>
    #define endl "\n"
    #define ooop(i, n) for(int i = 0; i < n; i++)
    #define loop(i, n) for(int i = 1; i <= n; i++)
     
    using namespace std;
    typedef long long ll;
    typedef pair<intint> pii;
    typedef pair<ll, ll> pll;
     
    int n, k;
     
    bool pop_hey(vector<vector<int> >& d)
    {
        vector<vector<int> > visited(n, vector<int>(10));
        stack<pii> will_pop;
     
        for(int c = 0; c < 10; c++){
            for(int r = n-1; r >= 0; r--){
                if(d[r][c] == 0continue;
     
                if(!visited[r][c]){
                    visited[r][c] = 1;
                    int color = d[r][c];
                    stack<pii> adj;
                    queue<pii> q;
     
                    adj.push({r, c});
                    q.push({r, c});
     
                    while(!q.empty()){
                        auto cur = q.front(); q.pop();
                        ooop(i, 4){
                            int nr = cur.first + "0121"[i] - '1';
                            int nc = cur.second + "1210"[i] - '1';
                            if(nr < 0 || nr >= n || nc < 0 || nc >= 10continue;
                            if(!visited[nr][nc] && d[nr][nc] == color){
                                visited[nr][nc] = 1;
                                q.push({nr, nc});
                                adj.push({nr, nc});
                            }
                        }
                    }
     
                    if(adj.size() >= k){
                        while(!adj.empty()){
                            will_pop.push(adj.top());
                            adj.pop();
                        }
                    }
     
                }
     
            }
        }
     
        if(will_pop.empty()) return false;
        else{
            while(!will_pop.empty()){
                auto cur = will_pop.top(); will_pop.pop();
                d[cur.first][cur.second] = 0;
            }
            return true;
        }
    }
     
    void gravity(vector<vector<int> >& d)
    {
        for(int c = 0; c < 10; c++){
            int empty = 0;
            for(int r = n-1; r >= 0; r--){
                if(d[r][c] == 0) {
                    empty = r;
                    break;
                }
            }
     
            if(empty){
                for(int r = empty-1; r >= 0; r--) {
                    if (d[r][c]) {
                        swap(d[empty][c], d[r][c]);
                        empty--;
                    }
                }
     
            }
        }
     
    }
     
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0), cout.tie(0);
     
        cin >> n >> k;
        vector<vector<int> > d(n, vector<int>(10));
     
        string s;
        ooop(r, n){
            cin >> s;
            ooop(c, 10) d[r][c] = s[c] - '0';
        }
     
        while(pop_hey(d))  gravity(d);
     
        ooop(r, n){
            ooop(c, 10cout << d[r][c];
            cout << endl;
        }
     
        return 0;

    '백준' 카테고리의 다른 글

    15589, Gold 1  (0) 2021.10.03
    2170, Gold 5  (0) 2021.10.03
    1654, Silver 3  (0) 2021.09.15
    11047, Silver 2  (0) 2021.08.19
    18242, silver 5  (0) 2021.08.18

    댓글

Designed by Tistory.