programmers.co.kr/learn/courses/30/lessons/64061
코딩테스트 연습 - 크레인 인형뽑기 게임
[[0,0,0,0,0],[0,0,1,0,3],[0,2,5,0,1],[4,2,4,4,2],[3,5,1,3,1]] [1,5,3,5,1,2,1,4] 4
programmers.co.kr
- 처음에 배열의 가로/세로를 엉뚱하게 생각하여 예제는 답이나오나 제출했을 때 0점이었다.
- Row별로 Idx를 따로 관리하도록 한다면 뽑힌 인형의 List를 알 수 있고 moves를 모두 계산했을 때,
- 한 번에 뿌요뿌요 하듯이 사라지는 인형의 개수를 계산하면된다.
더보기
#include <string>
#include <vector>
#define MIN_BOARD 5
#define MAX_BOARD 30
using namespace std;
int solution(vector<vector<int>> board, vector<int> moves) {
int answer = 0;
const int boardSize = board.size();
int boardCnt[MAX_BOARD] = { 0, };
vector<int> selectedList;
const int movesSize = moves.size();
for (int mIdx = 0; mIdx < movesSize; mIdx++)
{
const int moveVal = moves[mIdx] - 1;
while (true)
{
if (boardSize - 1 < boardCnt[moveVal])
break;
else if (0 == board[boardCnt[moveVal]][moveVal])
boardCnt[moveVal]++;
else
{
int value = board[boardCnt[moveVal]][moveVal];
selectedList.push_back(value);
board[boardCnt[moveVal]][moveVal] = 0;
break;
}
}
}
// check
while (true)
{
bool boom = false;
const int listSize = selectedList.size();
for (int idx = 0; idx < listSize; idx++)
{
if (idx + 1 < listSize && selectedList[idx] == selectedList[idx + 1])
{
//printf("%d %d \n", selectedList[idx], selectedList[idx + 1]);
boom = true;
selectedList.erase(selectedList.begin() + idx);
selectedList.erase(selectedList.begin() + idx);
answer += 2;
break;
}
}
if (false == boom)
break;
}
//printf("%d \n", answer);
return answer;
}
'Coding Problem > 프로그래머스' 카테고리의 다른 글
[프로그래머스/Lv1] - 완주하지 못한 선수 (0) | 2021.04.24 |
---|---|
[프로그래머스/Lv1/카카오] - 신규 아이디 추천 (0) | 2021.04.20 |
[프로그래머스/Lv1/카카오] - 실패율 (0) | 2021.04.18 |
[프로그래머스/Lv1] - K번째 수 (0) | 2021.04.18 |
[프로그래머스/Lv1] - 문자열 내 p 와 y의 개수 (0) | 2021.04.18 |