• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

크레인 인형뽑기(카카오) 왜 오답이 뜨는지 모르겠어요 ㅠㅠ

23.04.15 10:02 작성 23.04.15 10:03 수정 조회수 172

0

package hello;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class Main {
	
	
	public int solution(int n, int[][] board, int m, int[] moves) {
		Stack<Integer> baguny = new Stack<>();
		int count = 0;
		
		//크레인
		for(int i=0; i<moves.length; i++) {
			//크레인 작동위치에서 인형빼와서 바구니에 담기
			for(int j=0; j<board.length; j++) {
				if(board[j][moves[i]-1] != 0) {
					if(baguny.contains(board[j][moves[i]-1])) {
						baguny.pop();
						count += 2;
						board[j][moves[i]-1] = 0;
						break;
					} else {
						baguny.push(board[j][moves[i]-1]);
						board[j][moves[i]-1] = 0;
						break;
					}
					
				}
			}
			
			
			
		}
		
		return count;
	}

	public static void main(String[] args) {
		Main t = new Main();
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		
		int[][] board = new int[n][n];
		for(int i=0; i<n; i++) {
			for(int j=0; j<n; j++) {
				board[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(n, board, m, moves));
	}
}

답변 1

답변을 작성해보세요.

1

안녕하세요^^

같은 모양의 인형 두 개가 연속해서 바구니에 쌓였을 때 두 인형이 터트려지는 것입니다.

즉 현재 크레인으로 가져오는 인형과 스택의 최상단 인형이 같아야 두 인형이 터트려지는 것입니다.