Algorithm
2018 KAKAO BLIND RECRUITMENT[1차] 프렌즈4블록
이무쿤
2020. 9. 10. 00:50
반응형
programmers.co.kr/learn/courses/30/lessons/17679
코딩테스트 연습 - [1차] 프렌즈4블록
프렌즈4블록 블라인드 공채를 통과한 신입 사원 라이언은 신규 게임 개발 업무를 맡게 되었다. 이번에 출시할 게임 제목은 프렌즈4블록. 같은 모양의 카카오프렌즈 블록이 2×2 형태로 4개가 붙��
programmers.co.kr
하나의 문자로 부터 터질 때 떨어지는 것이 아니라 한번의 턴이 끝난 후에 떨어지는 것임.
로직 잘 생각할 것!
#include <string>
#include <queue>
#include <vector>
#include <string.h>
#include <iostream>
using namespace std;
char temp[30][30];
int solution(int m, int n, vector<string> board) {
int answer = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
temp[i][j] = board[i][j];
}
}
while (1)
{
bool flag = false;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (board[i][j] != '0')
{
queue<pair<int, int> > que;
que.push({ i,j });
while (!que.empty())
{
int y = que.front().first;
int x = que.front().second;
que.pop();
if (y + 1 < m && x + 1 < n)
{
char cur = board[y][x];
if (cur == board[y + 1][x] && cur == board[y][x + 1] && cur == board[y + 1][x + 1])
{
//cout << y << " , " << x << " 에서 " << board[y][x] << " 가 터져요" << endl;
flag = true;
if (temp[y][x] != '0')
{
temp[i][j] = '0';
answer++;
}
if (temp[y + 1][x + 1] != '0')
{
que.push({ y + 1,x + 1 });
temp[y + 1][x + 1] = '0';
answer++;
}
if (temp[y + 1][x] != '0')
{
que.push({ y + 1,x });
temp[y + 1][x] = '0';
answer++;
}
if (temp[y][x + 1] != '0')
{
que.push({ y,x + 1 });
temp[y][x + 1] = '0';
answer++;
}
}
}
}
}
}
}
if (!flag)
{
break;
}
for (int k = 0; k < n; k++)
{
queue<char> re;
for (int l = m - 1; l >= 0; l--)
{
if (temp[l][k] != '0')
{
re.push(temp[l][k]);
}
}
for (int l = m - 1; l >= 0; l--)
{
temp[l][k] = '0';
}
int size = re.size();
int idx = m - 1;
for(int l = 0; l < size; l++)
{
temp[idx][k] = re.front();
idx--;
re.pop();
}
for (int l = m - 1; l >= 0; l--)
{
board[l][k] = temp[l][k];
}
}
}
return answer;
}
반응형