https://school.programmers.co.kr/learn/courses/30/lessons/17679
#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;
}
'Coding Test > Programmers' 카테고리의 다른 글
[Programmers] Lv 2. 숫자 변환하기 (0) | 2023.07.20 |
---|---|
[Programmers] Lv 2. 2 x N 타일링 (0) | 2023.07.19 |
[Programmers] (2018 KAKAO BLIND RECRUITMENT) Lv 2. [3차] 파일명 정렬 (0) | 2023.07.15 |
[Programmers] (stack) Lv 2. 뒤에 있는 큰 수 찾기 (0) | 2023.07.14 |
[Programmers] (완전탐색) Lv 2. 모음사전 (0) | 2023.07.14 |
댓글