본문 바로가기
Coding Test/Programmers

[Programmers] (2018 KAKAO BLIND RECRUITMENT) Lv 2. [1차] 프렌즈4블록

by song.ift 2023. 7. 18.

https://school.programmers.co.kr/learn/courses/30/lessons/17679

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

#include <string>
#include <vector>

using namespace std;

int solution(int m, int n, vector<string> board) {
    int answer = 0;
    
    while (true)
    {
        vector<vector<bool>> check(m, vector<bool>(n, true));
        vector<pair<int, int>> remove;

        for (size_t x = 0; x < m - 1; ++x)
        {
            for (size_t y = 0; y < n - 1; ++y)
            {
                if (board[x][y] == 'X') continue;

                if (board[x][y] == board[x][y + 1] &&
                    board[x][y] == board[x + 1][y] &&
                    board[x][y] == board[x + 1][y + 1])
                {
                    vector<pair<int, int>> blocks{ {x, y}, {x, y + 1}, {x + 1, y}, {x + 1, y + 1} };
                    for (size_t i = 0; i < 4; ++i)
                    {
                        const auto& block = blocks[i];
                        if (check[block.first][block.second])
                        {
                            check[block.first][block.second] = false;
                            remove.emplace_back(make_pair(block.first, block.second));
                            ++answer;
                        }
                    }
                }
            }
        }

        if (remove.empty()) break;

        for (const auto& pos : remove)
            board[pos.first][pos.second] = 'X';

        for (size_t y = 0; y < n; ++y)
        {
            for (size_t x = m - 1; x > 0; --x)
            {
                if (board[x][y] == 'X')
                {
                    int t = x;
                    while (--t >= 0)
                    {
                        if (board[t][y] != 'X')
                        {
                            auto temp = board[t][y];
                            board[t][y] = board[x][y];
                            board[x][y] = temp;
                            break;
                        }
                    }
                }
            }
        }
    }

    return answer;
}

GitHub :https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/commit/e7bd07b21a2198cd9bcdaeabe3c49205bfa34809 

 

[level 2] Title: [1차] 프렌즈4블록, Time: 0.09 ms, Memory: 4.15 MB -Baekjoo… · developeSHG/Algorithm-Baekjoon_Programme

…nHub

github.com

 

댓글