CPP 문제풀이/프로그래머스

크레인 인형 뽑기

hjc_ 2020. 7. 26. 20:54

 

문제 링크 

 

#include <string>
#include <vector>
#include <algorithm>
#include <stack>

using namespace std;


int solution(vector<vector<int>> board, vector<int> moves) {
    stack <int> s;
    int i=0,j=0;
    int answer = 0;
 
    for(i=0; i < moves.size(); i++){  //턴 만큼 반복 
        int mline = moves[i]-1; // 1번 위치 = [0]번째 열

        for(j=0; j < board.size(); j++){  //0번째 열의 행을 탐색 
            if(board[j][mline] == 0) continue;
            else{
                if(!s.empty() && board[j][mline] == s.top())
                {
                    s.pop();
                    answer += 2;
                    
                }
                else{
                    s.push(board[j][mline]);
                }
                board[j][mline] = 0;
                break;
            }       
        }  
    }
    return answer;
}