Last active
October 25, 2020 10:51
-
-
Save ekal901/c1b62f801fdf196753d1ce182073d917 to your computer and use it in GitHub Desktop.
크레인인형뽑기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Oct; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class 크레인인형뽑기 { | |
| public static void main(String[] args) { | |
| int[][] board = new int[5][]; | |
| board[0] = new int[]{0,0,0,0,0}; | |
| board[1] = new int[]{0,0,1,0,3}; | |
| board[2] = new int[]{0,2,5,0,1}; | |
| board[3] = new int[]{4,2,4,4,2}; | |
| board[4] = new int[]{3,5,1,3,1}; | |
| int[] moves = new int[]{1,5,3,5,1,2,1,4}; | |
| int answer = solution(board, moves); | |
| } | |
| public static int solution(int[][] board, int[] moves) { | |
| List<List<Integer>> newBoard = makeNewBoard(board); | |
| int answer = 0; | |
| List<Integer> basket = new ArrayList<>(); | |
| for (int idx = 0; idx < moves.length; idx++) { | |
| List<Integer> column = newBoard.get(moves[idx] -1); | |
| for(int i = 0; i < column.size(); i++) { | |
| if(column.get(i) != 0) { | |
| basket.add(column.get(i)); | |
| column.set(i, 0); // 바구니에 담으면 0으로 변경 | |
| break; | |
| } | |
| } | |
| // System.out.println(basket.toString()); | |
| answer = countResult(basket); | |
| } | |
| System.out.println(answer); | |
| return answer; | |
| } | |
| public static int countResult(List<Integer> basket) { | |
| int tmp = basket.get(0); | |
| int answer = 0; | |
| for (Integer doll : basket) { | |
| if (tmp != doll) { | |
| tmp = doll; | |
| } else { | |
| answer += 2; // 터지니까 두개씩 | |
| } | |
| } | |
| return answer; | |
| } | |
| public static List<List<Integer>> makeNewBoard(int[][] board) { | |
| List<List<Integer>> newBoard = new ArrayList<>(); | |
| for (int i = 0; i < board.length; i++) { | |
| newBoard.add(makeColumn(board, i)); | |
| } | |
| return newBoard; | |
| } | |
| public static List<Integer> makeColumn(int[][] board, int rowidx) { | |
| List<Integer> column = new ArrayList<>(); | |
| for (int[] row : board) { | |
| column.add(row[rowidx]); | |
| } | |
| return column; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment