[알고리즘] 크레인 인형뽑기

Welcome to Seoyun Dev Log·2023년 4월 17일
0

🧮 알고리즘

목록 보기
1/1

1. 문제

  • 입력
5
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
8
1 5 3 5 1 2 1 4
  • 답 : 4

1) 풀이


package inflearn.algorithm.stack;
import java.util.*;

public class Algorithm39 {
    public int solution(int[][] arr, int[] moves){
        int answer = 0;
        //1. 바구니에 담기
        Stack<Integer> basket = new Stack<>();

        //2. 이차원 배열에서 moves 뽑기
        for(int pos: moves){//열
            for(int i = 0; i < arr.length; i++){//행
                if (arr[i][pos - 1] != 0) {
                    int tmp = arr[i][pos - 1]; //인형 임시 보관
                    arr[i][pos - 1] = 0; //인형 빼주기
                    if (!basket.isEmpty() && basket.peek() == tmp) {//인형이 일치할 경우
                        answer += 2;
                        basket.pop();
                    } else {
                        basket.push(tmp); // 중복이 아니면 바구니에 담기
                    }
                    break;//⭐️
                }
            }
        }

        return answer;
    }
    public static void main(String[] args){
        Algorithm39 T = new Algorithm39();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] arr = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                arr[i][j] = sc.nextInt();
            }
        }
        int m = sc.nextInt();
        int[] moves = new int[m];
        for (int i = 0; i < m; i++) {
            moves[i] = sc.nextInt();
        }
        System.out.println(T.solution(arr, moves));
    }
}

profile
하루 일지 보단 행동 고찰 과정에 대한 개발 블로그

0개의 댓글