GGym's Practice Notes

프로그래머스 Level1_ 크레인 인형뽑기 게임 (C++) 본문

Algorithm

프로그래머스 Level1_ 크레인 인형뽑기 게임 (C++)

GGym_ 2020. 10. 8. 02:49

Vector로 되어있는 입력 데이터를 Stack으로 옮긴 후 basket Stack을 이용하여 인형이 제거되는 갯수를 센다.

#include <string>   // Level1_ 크레인 인형뽑기 게임 [스택]
#include <vector>
#include <stack>

#include <iostream>

using namespace std;

int solution(vector<vector<int>> board, vector<int> moves) {
    int answer = 0;
    stack<int> doll[board.size()]; 
    stack<int> basket;
    for(int i = board.size()-1; i>=0; i--){
        for(int j =0; j<board[i].size(); j++){
            if(board[i][j] != 0){
                doll[j].push(board[i][j]);
            }
        }
    }
    for(auto m : moves){
        if(!basket.empty() && !doll[m-1].empty() && basket.top() == doll[m-1].top()){
            basket.pop();
            doll[m-1].pop();
            answer+=2;
        }
        else{
            if(!doll[m-1].empty()){
                basket.push(doll[m-1].top());
                doll[m-1].pop();
            }
        }
    }

    return answer;
}