Algorithm

[Programmers] 크레인 인형뽑기 게임

프로그래민 2020. 4. 27. 23:34
반응형

단순 구현 문제이다.

Stack을 이용한 단순 구현 문제이다.

 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package programmers;
 
 
public class Solution_2019카카오겨울_크레인인형뽑기게임 {
    public static void main(String[] args) {
        
        int[][] board= {{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}};
        int[] moves= {1,5,3,5,1,2,1,4};
        
        System.out.println(solution(board,moves));
        
    }
    
    static int solution(int[][] board,int[] moves) {
        int r=board.length;
        int answer=0;
        Stack<Integer> stack = new Stack<>();
 
        for(int move : moves) {
            int index=move-1;
            
            for(int i=0;i<r;i++) {    //행의 처음부터 확인
                if(board[i][index]!=0) {
                    int num=board[i][index];
                    board[i][index]=0;
                    
                    if(stack.size()!=0) {
                        if(stack.peek()==num) {
                            stack.pop();
                            answer=answer+2;
                        }
                        else {
                            stack.push(num);
                        }
                    }else {
                        stack.push(num);
                    }
                    
                    break;
                }
            }
        }
        return answer;
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
                                                    
반응형

'Algorithm' 카테고리의 다른 글

[Programmers] 불량 사용자  (0) 2020.04.28
[Programmers] 튜플  (0) 2020.04.27
[SWEA] 5653. 줄기세포배양  (0) 2020.04.27
[BOJ] 1748. 수 이어 쓰기1  (0) 2020.04.22
[BOJ] 6087. 레이저 통신  (0) 2020.04.19