본문 바로가기

Java/Java 알고리즘 인프런

[Ch.05 - StackQueue] 03. 크레인 인형뽑기(카카오)

반응형
3. 크레인 인형뽑기(카카오)
 

 

설명

게임개발자인 죠르디는 크레인 인형뽑기 기계를 모바일 게임으로 만들려고 합니다.

죠르디는 게임의 재미를 높이기 위해 화면 구성과 규칙을 다음과 같이 게임 로직에 반영하려고 합니다.

게임 화면은 1 x 1 크기의 칸들로 이루어진 N x N 크기의 정사각 격자이며 위쪽에는 크레인이 있고 오른쪽에는 바구니가 있습니다.

(위 그림은 5 x 5 크기의 예시입니다). 각 격자 칸에는 다양한 인형이 들어 있으며 인형이 없는 칸은 빈칸입니다.

모든 인형은 1 x 1 크기의 격자 한 칸을 차지하며 격자의 가장 아래 칸부터 차곡차곡 쌓여 있습니다.

게임 사용자는 크레인을 좌우로 움직여서 멈춘 위치에서 가장 위에 있는 인형을 집어 올릴 수 있습니다. 집어 올린 인형은 바구니에 쌓이게 되는 데,

이때 바구니의 가장 아래 칸부터 인형이 순서대로 쌓이게 됩니다.

다음 그림은 [1번, 5번, 3번] 위치에서 순서대로 인형을 집어 올려 바구니에 담은 모습입니다.

만약 같은 모양의 인형 두 개가 바구니에 연속해서 쌓이게 되면 두 인형은 터뜨려지면서 바구니에서 사라지게 됩니다.

위 상태에서 이어서 [5번] 위치에서 인형을 집어 바구니에 쌓으면 같은 모양 인형 두 개가 없어집니다.

크레인 작동 시 인형이 집어지지 않는 경우는 없으나 만약 인형이 없는 곳에서 크레인을 작동시키는 경우에는 아무런 일도 일어나지 않습니다.

또한 바구니는 모든 인형이 들어갈 수 있을 만큼 충분히 크다고 가정합니다. (그림에서는 화면표시 제약으로 5칸만으로 표현하였음)

게임 화면의 격자의 상태가 담긴 2차원 배열 board와 인형을 집기 위해 크레인을 작동시킨 위치가 담긴 배열 moves가 매개변수로 주어질 때,

크레인을 모두 작동시킨 후 터트려져 사라진 인형의 개수를 구하는 프로그램을 작성하세요.

 

 

입력

첫 줄에 자연수 N(5<=N<=30)이 주어집니다.

두 번째 줄부터 N*N board 배열이 주어집니다.

board의 각 칸에는 0 이상 100 이하인 정수가 담겨있습니다.

0은 빈 칸을 나타냅니다.

1 ~ 100의 각 숫자는 각기 다른 인형의 모양을 의미하며 같은 숫자는 같은 모양의 인형을 나타냅니다.

board배열이 끝난 다음줄에 moves 배열의 길이 M이 주어집니다.

마지막 줄에는 moves 배열이 주어집니다.

moves 배열의 크기는 1 이상 1,000 이하입니다.

moves 배열 각 원소들의 값은 1 이상이며 board 배열의 가로 크기 이하인 자연수입니다.

 

 

출력

첫 줄에 터트려져 사라진 인형의 개수를 출력합니다.

 


# 스택이 비어있을 때와 비어있지 않을 때로 분기

# 스택에 넣은 경우 -> 기존의 배열에서 0으로 변경

 

import java.util.*;

public class Main {
	public int solution(int n, int[] arr2, int[][] arr1) {
		int answer = 0;
		Stack<Integer> stack = new Stack<>();
		for (int i = 0; i < arr2.length; i++) {
			for (int j = 0; j < n; j++) {
				if (arr1[j][arr2[i] - 1] != 0) {
					if (!stack.isEmpty() && stack.peek() == arr1[j][arr2[i] - 1]) {
						stack.pop();
						arr1[j][arr2[i] - 1] = 0;
						answer+=2;
						
					} else {
						stack.push(arr1[j][arr2[i] - 1]);
						arr1[j][arr2[i] - 1] = 0;
					}
					break;
				}
			}
		}

		return answer;

	}

	public static void main(String[] args) {
		Main T = new Main();
		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt();
		int[][] arr1 = new int[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				arr1[i][j] = kb.nextInt();
			}
		}
		int m = kb.nextInt();
		int[] arr2 = new int[m];
		for (int i = 0; i < m; i++) {
			arr2[i] = kb.nextInt();
		}
		System.out.println(T.solution(n, arr2, arr1));
	}
}

 

 

import java.util.Scanner;
import java.util.Stack;

public class Main {
	public int solution(int n, int[][] arr1, int m, int[] arr2) {
		int answer = 0;
		Stack<Integer> stack = new Stack<>();

		for (int x : arr2) {
			for (int j = 0; j < n; j++) {
				if (arr1[j][x] != 0) {
					if (!stack.isEmpty()) {
						if (stack.peek() != arr1[j][x]) {
							stack.push(arr1[j][x]);
							arr1[j][x]=0;
						} else {
							arr1[j][x]=0;
							stack.pop();
							answer+=2;
						}
					} else {
						stack.push(arr1[j][x]);
						arr1[j][x]=0;
					}

					break;
				}
			}
		}

		return answer;
	}

	public static void main(String[] args) {
		Main T = new Main();
		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt();
		int[][] arr1 = new int[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				arr1[i][j] = kb.nextInt();
			}
		}
		int m = kb.nextInt();
		int[] arr2 = new int[m];
		for (int j = 0; j < m; j++)
			arr2[j] = kb.nextInt()-1;
		System.out.println(T.solution(n, arr1, m, arr2));
	}

}

import java.util.Scanner;
import java.util.Stack;

public class Main {
	public int solution(int n, int[][] arr1, int m, int[] arr2) {
		int answer = 0;
		Stack<Integer> stack = new Stack<>();

		for (int x : arr2) {
			for (int j = 0; j < n; j++) {
				if (arr1[j][x] != 0) {
					if (!stack.isEmpty()) {
						if (stack.peek() != arr1[j][x]) {
							stack.push(arr1[j][x]);
							arr1[j][x]=0;
						} else {
							arr1[j][x]=0;
							stack.pop();
							answer+=2;
						}
					} else {
						stack.push(arr1[j][x]);
						arr1[j][x]=0;
					}

					break;
				}
			}
		}

		return answer;
	}

	public static void main(String[] args) {
		Main T = new Main();
		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt();
		int[][] arr1 = new int[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				arr1[i][j] = kb.nextInt();
			}
		}
		int m = kb.nextInt();
		int[] arr2 = new int[m];
		for (int j = 0; j < m; j++)
			arr2[j] = kb.nextInt()-1;
		System.out.println(T.solution(n, arr1, m, arr2));
	}

}

-> 넣을 때부터 -1을 해서 배열에 맞게 변경

 

1. 스택이 비어있을 때와 비어있지 않을 때로 분기

2. 비어있을 때 -> 확인하지 않고 바로 삽입

3. 비어있지 않을 때 -> 같은지 확인 후, 삽입 [배열에서 삽입한 값 0으로 변경]

 

 

+)세련된 풀이 -> 배열 초기화를 한번만 하기 위해서 배열의 값을 tmp에 저장

package stackqueue.ch05_2;

import java.util.Scanner;
import java.util.Stack;

//배열 초기화 한번만 하기위해 tmp라는 임시변수에 배열 값 저장
public class Stackqueue03_2 {
	public int solution(int n, int[][] arr1, int m, int [] arr2) {
		Stack<Integer> stack = new Stack<>();
		int answer=0;
		
		for(int x:arr2) {
			for(int j=0;j<n;j++) {
				if(arr1[j][x]!=0) {
					int tmp=arr1[j][x];
					arr1[j][x]=0;
					if(!stack.isEmpty()&&stack.peek()==tmp) {
						stack.pop();
						answer+=2;
					}
					else stack.push(tmp);
					break;
				}
				
			}
		}
		
		return answer;
	}
	public static void main(String[] args) {
		Stackqueue03_2 T = new Stackqueue03_2();
		Scanner kb = new Scanner(System.in);
		
		int n=kb.nextInt();
		int[][] arr1 = new int[n][n];
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++) {
				arr1[i][j]=kb.nextInt();
			}
		}
		int m=kb.nextInt();
		int[] arr2=new int[m];
		for(int i=0;i<m;i++) arr2[i]=kb.nextInt()-1;
		
		System.out.println(T.solution(n, arr1, m, arr2));
	}

}

#인형은 두개씩 터진다.

 

import java.util.*;
  
public class Main {
  public static void main(String[] args){
    Scanner in=new Scanner(System.in);
    int n = in.nextInt();
    int[][] arr=new int[n][n];
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        arr[i][j]=in.nextInt();
      }
    }
    int k=in.nextInt();
    int[] crane= new int[k];
    for(int i=0;i<k;i++){
      crane[i]=in.nextInt();
    }
    System.out.println(Solution(n,k, arr, crane));
  }
  private static int Solution(int n,int k, int[][]arr, int[] crane){
   int answer=0;
   Stack<Integer> stack = new Stack<>();    
    for(int i=0;i<k; i++){
        for(int j=0; j<n; j++){
          int x=arr[j][crane[i]-1];
          if(x!=0) {
            if(!stack.isEmpty()&&stack.peek() == x){
              stack.pop();
              answer+=2;
            }
            else{
              stack.push(x);
            }
            arr[j][crane[i]-1]=0;
            break;
          }
        }
      }
    
    return answer;
  }
}

 

반응형