Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Study

프로그래머스-크레인 인형뽑기 본문

알고리즘/C++ 문제풀이

프로그래머스-크레인 인형뽑기

^_^? 2021. 7. 14. 22:42
 

코딩테스트 연습 - 크레인 인형뽑기 게임

[[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

 

 

<풀이>

더보기

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

using namespace std;
 
int solution(vector<vector<int>> board, vector<int> moves) {
    int answer = 0;
    stack<int> s;
    for(int i=0;i<moves.size();i++)
    {
        int check = moves[i] - 1;
        for(int j=0;j<board.size();j++)
        {
            if(board[j][check] != 0)
            {
                if(!s.empty() && s.top() == board[j][check])
                {
                    s.pop();
                    board[j][check] = 0;
                    answer += 2;
                    break;//break는 가장 가까운 반복문을 빠져나간다
                  
                }
                    s.push(board[j][check]);
                    board[j][check] = 0;
                    break;
                    
            }
        }
    }
    
    return answer;
}

 

-참고한 사이트

https://iingang.github.io/posts/Programmers-64061/