#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX = 12;
string map[MAX];
int visited[MAX][6];
struct Move {
int x, y;
};
Move mv[4] = { {1,0},{-1,0}, {0,1}, {0,-1} };
bool cmp(pair<int, int> a, pair<int, int> b){
if (a.first < b.first)
return true;
else if (a.first == b.first){
if (a.second < b.second)
return true;
return false;
}
return false;
}
int bfs() {
int answer = 0;
while (1) {
vector<pair<int, int>> puyopop;
memset(visited, -1, sizeof(visited));
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < 6; j++) {
if (map[i][j] == '.' || visited[i][j] == 1)
continue;
queue<pair<int, int>> q;
vector<pair<int, int>> cache;
q.push(make_pair(i, j));
visited[i][j] = 1;
while (!q.empty()) {
int currenti = q.front().first;
int currentj = q.front().second;
q.pop();
cache.push_back(make_pair(currenti, currentj));
for (int k = 0; k < 4; k++) {
int movei = currenti + mv[k].x;
int movej = currentj + mv[k].y;
if (movei >= 0 && movei < 12 && movej >= 0 && movej < 6 && visited[movei][movej] == -1 && map[movei][movej] == map[currenti][currentj]) {
visited[movei][movej] = 1;
q.push(make_pair(movei, movej));
}
}
}
if (cache.size() >= 4) {
for (int k = 0; k < cache.size(); k++)
puyopop.push_back(make_pair(cache[k].first, cache[k].second));
}
}
}
if (puyopop.size() > 0) {
sort(puyopop.begin(), puyopop.end(), cmp);
answer++;
for (int k = 0; k < puyopop.size(); k++) {
for (int i = puyopop[k].first; i >= 1; i--) {
map[i][puyopop[k].second] = map[i - 1][puyopop[k].second];
}
map[0][puyopop[k].second] = '.';
}
}
else
break;
}
return answer;
}
int main(void) {
for (int i = 0; i < MAX; i++) {
cin >> map[i];
}
cout << bfs() << endl;
return 0;
}
'백준 알고리즘 > 시뮬레이션' 카테고리의 다른 글
백준 1526번 C++ (0) | 2019.08.29 |
---|---|
백준 2979번 C++ (0) | 2019.08.29 |
백준 5397번 C++ (0) | 2019.08.29 |
백준 2161번 C++ (0) | 2019.08.28 |
백준 5532번 C++ (0) | 2019.08.28 |