Home BOJ. Puyo Puyo (11559)
Post
Cancel

BOJ. Puyo Puyo (11559)

[Link] https://www.acmicpc.net/problem/11559


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
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstring>
// #include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i < n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
#define ll long long
#define endl "\n"
#define nulls '\0'
#define dkdk " "
const int MAX = 5e5 + 1;
const int INF = 987654321;

int mm[12][6];
bool visit[12][6];
bool erase[12][6];
int newmap[12][6];
int di[4] = { -1, 0, 1, 0 };
int dj[4] = { 0, 1, 0, -1 };
vector<pair<int, int>> pop_list;
queue<pair<int,int>> q;
bool no_pop = true;

void bfs(int ci, int cj) {
  int cnt = 0;
  pop_list.clear();
  q.push({ ci, cj});
  int color = mm[ci][cj];
  while(!q.empty()) {
    pair<int, int> p = q.front(); q.pop();
    if(visit[p.first][p.second]) continue;
    visit[p.first][p.second] = true;
    cnt++;
    pop_list.push_back(p);
    for0(rp, 4) {
      int ai = p.first + di[rp], aj = p.second + dj[rp];
      if(ai < 0 || aj < 0 || ai >= 12 || aj >= 6 || visit[ai][aj]) continue;
      if(mm[ai][aj] == color) q.push({ ai, aj });
    }
  }
  if(cnt >= 4) {
    for(auto p : pop_list) erase[p.first][p.second] = true;
    no_pop = false;
  }
}

void solve() {
  int step = 0;
  while(true) {
    // cout << " -----------\n";
    // for0(i, 12) { for0(j, 6) cout << mm[i][j] << " "; cout << endl; }
    for0(i, 12) for0(j, 6) { visit[i][j] = erase[i][j] = false; }
    no_pop = true;
    for0(j, 6) {
      for(int i = 11; i >= 0; i--) {
        if(mm[i][j] == 0) break;
        if(visit[i][j]) continue;
        bfs(i, j);
      }
    }
    if(no_pop) break;
    step++;
    for0(i, 12) for0(j, 6) newmap[i][j] = 0;

    for0(j, 6) {
      int orig_idx = 11, new_idx = 11;
      while(orig_idx >= 0) {
        if(mm[orig_idx][j] == 0) break;
        if(erase[orig_idx][j]) {
          orig_idx--;
          continue;
        }
        newmap[new_idx--][j] = mm[orig_idx--][j];
      }
    }
    memcpy(mm, newmap, sizeof(newmap));
  }

  cout << step;
}

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  for0(i, 12) {
    for0(j, 6) {
      char in; cin >> in;
      if(in == '.') continue;
      if(in == 'R') mm[i][j] = 1;
      if(in == 'G') mm[i][j] = 2;
      if(in == 'B') mm[i][j] = 3;
      if(in == 'P') mm[i][j] = 4;
      if(in == 'Y') mm[i][j] = 5;
    }
  }
  solve();
  return 0;
}
This post is licensed under CC BY 4.0 by the author.