ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 11559번 C++ 코드
    백준 2019. 8. 29. 22:43

    #include <iostream>
    #include <vector>
    #include <math.h>
    #include <queue>
    #include <map>
    #include <stack>
    #include <climits>
    #include <functional>
    #include <string>
    #include <cstring>
    #include <iomanip>

    using namespace std;

    char buffer[6][12];

    bool is_visited[6][12];
    int xxxx[4] = {0 , 0, -1, 1};
    int yyyy[4] = {1, -1, 0, 0};

    bool is_chain(int x, int y) {

    if (is_visited[x][y]) {
    return false;
    }

    if (buffer[x][y] == '.') {
    return false;
    }

    is_visited[x][y] = true;
    int chain = 1;

    auto color = buffer[x][y];
    queue<pair<int, int>> q;
    queue<pair<int, int>> backup;

    q.push({x, y});
    backup.push({x, y});

    while (!q.empty()) {
    auto top = q.front();
    q.pop();

    for (auto i=0; i<4; i++) {
    int nextX = top.first + xxxx[i];
    int nextY = top.second + yyyy[i];

    if (0 <= nextX && nextX < 6 && 0 <= nextY && nextY < 12) {
    if (buffer[nextX][nextY] == color && !is_visited[nextX][nextY] ) {
    chain++;
    q.push({nextX, nextY});
    backup.push({nextX, nextY});
    is_visited[nextX][nextY] = true;
    }
    }
    }
    }


    if (chain >= 4) {
    while (!backup.empty()) {
    auto top = backup.front();
    backup.pop();

    buffer[top.first][top.second] = '.';
    }

    return true;
    } else {
    return false;
    }

    }


    bool is_successful() {


    for (auto y=0; y<12; y++) {
    for (auto x=0; x<6; x++) {
    is_visited[x][y] = false;
    }
    }

    bool chainable = false;

    for (auto y=0; y<12; y++) {
    for (auto x=0; x<6; x++) {
    if (is_chain(x, y)) {
    chainable = true;
    }
    }
    }

    queue<char> q;

    for (auto x= 0; x<6; x++) {
    for (auto y=12-1; y>=0; y--) {
    if (buffer[x][y] != '.') {
    q.push(buffer[x][y]);
    }
    }

    for (auto y=12-1; y>=0; y--) {
    buffer[x][y] = '.';
    }

    int index = 11;
    while (!q.empty()) {
    buffer[x][index] = q.front();
    index--;
    q.pop();
    }


    }
    return chainable;


    }


    int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);



    for (auto y=0; y<12; y++) {
    for (auto x=0; x<6; x++) {
    cin >> buffer[x][y];
    }
    }

    int answer = 0;

    while (is_successful()) {
    answer++;
    }

    //
    // for (auto y=0; y<12; y++) {
    // for (auto x=0; x<6; x++) {
    // cout << buffer[x][y] << ' ';
    // }
    // cout << '\n';
    // }
    cout << answer;
    return 0;
    }

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

    백준 2096번 C++ 풀이  (0) 2019.10.29
    백준 17070번 C++ 풀이  (0) 2019.08.30
    백준 2799번 C++ 풀이  (0) 2019.01.04
    백준 2468번 C++ 풀이  (0) 2018.11.25
    백준 1707번 C++ 풀이  (0) 2018.11.25

    댓글 0

Designed by Tistory.