https://programmers.co.kr/learn/courses/30/lessons/17679#
코딩테스트 연습 - [1차] 프렌즈4블록
프렌즈4블록 블라인드 공채를 통과한 신입 사원 라이언은 신규 게임 개발 업무를 맡게 되었다. 이번에 출시할 게임 제목은 "프렌즈4블록". 같은 모양의 카카오프렌즈 블록이 2×2 형태로 4개가 붙
programmers.co.kr
나의 풀이
#include <string>
#include <vector>
#include <cctype>
#include <iostream>
using namespace std;
vector<pair<int, int>> check = { {0,0}, {0,1},{1,0},{1,1} };
int N, M;
void find_del(vector<pair<int, int>> &del, vector<string>& board) {
for (int i = 0; i < M - 1; i++) {
for (int k = 0; k < N - 1; k++) {
char now = board[i][k];
if (now != '.' && isupper(now)) {
bool flag = true;
for (int t = 1; t < check.size(); t++) {
if (now != board[i + check[t].first][k + check[t].second]) {
flag = false;
break;
}
}
if (flag) {
del.push_back({ i,k });
}
}
}
}
}
void drop(vector<pair<int, int>> &del, vector<string> &board) {
for (auto p : del) {
for (auto c : check) {
board[p.first + c.first][p.second + c.second] = '.';
}
}
for (auto p : del) {
for (int i = 0; i < 2; i++) {
int count = 0;
while (p.first + count < board.size() && board[p.first + count][p.second + i] == '.') {
count++;
}
for (int k = p.first - 1; k >= 0; k--) {
board[k + count][p.second + i] = board[k][p.second + i];
board[k][p.second + i] = '.';
}
}
}
}
int solution(int m, int n, vector<string> board) {
vector<pair<int, int>> del;
int answer = 0;
M = m; N = n;
do {
del.clear();
find_del(del, board);
drop(del, board);
} while (!del.empty());
for (string str : board) {
for (char c : str) {
if (c == '.') answer++;
}
}
return answer;
}
반례를 아무리 찾아봐도 테스트 6, 10에 실패를 한다..
'전공공부 > 코딩테스트' 카테고리의 다른 글
(c++) 프로그래머스 "괄호 회전하기" (0) | 2022.04.13 |
---|---|
(c++) 프로그래머스 "피로도" (0) | 2022.04.13 |
(c++) 프로그래머스 "가장 큰 사각형 찾기" (0) | 2022.04.12 |
(c++) 프로그래머스 "[1차] 캐시" (0) | 2022.04.12 |
(c++) 프로그래머스 "스킬트리" (0) | 2022.04.12 |