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

Sorbet·2021년 4월 18일
0

코테

목록 보기
20/35
  • cs 10때 안풀고 놀다가 이렇게 됬다 ㅋㅋ...

  • 같이 공부하던 노을의 방법

import java.util.ArrayList;
import java.util.List;

class Solution {
    class Basket {
        int answer = 0;
        List<Integer> basket = new ArrayList<>();
        void explosionTrigger() {
            int feed = -1;

            for(int i=0 ; i<basket.size() ; i++) {
                int cur = basket.get(i);
                if(cur == feed) {
                    answer += 2;
                    basket.remove(i-1);
                    basket.remove(i-1);
                    //feed = -1;
                    break;
                }
                else {
                    feed = cur;
                }
            }

        }
        void addBasket(int element) {
            basket.add(element);
        }

    }


    public int solution(int[][] board, int[] moves) {
        Basket b = new Basket();

        for(int i=0 ; i<moves.length ; i++) {
            int pos = moves[i];
            int tar = pick(pos,board,b);
            b.explosionTrigger();
        }



        return b.answer;
    }
    int pick(int pos, int[][]board, Basket b) {
        //pos--;
        int N = board.length;
        int tar = 0;
        for(int i=0 ; i<N ; i++) {
            if(board[i][pos-1] != 0) {
                tar = board[i][pos-1];
                b.addBasket(tar);
                board[i][pos-1] = 0;
                return tar;
            }
        }
        return -1;
    }

    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};
        tester(board, moves, 4);

    }

    public static void tester(int[][] board, int[] moves, int ans) {
        Solution s = new Solution();
        int ret = s.solution(board, moves);
        if(ans == ret) {
            System.out.println("OK");
        }
        else {
            System.out.println("NG");
        }
    }

}
profile
Sorbet is good...!

0개의 댓글