백준|14499번|주사위 굴리기

JSK·2022년 7월 31일
0

자바 PS풀이

목록 보기
43/51

문제설명
주사위가 있고 지도가 있을 때 주사위를 지도 위에서 굴려서 맨 윗칸의 숫자들을 순서대로 출력하는 문제입니다. 이때 지도에는 숫자가 쓰여져있고 주사위가 칸을 밟은 경우 주사위의 바닥칸의 숫자를 칸의 숫자로 바꾸고 숫자칸의 숫자는 0으로 바꿉니다. 만약 주사위가 밟은 칸의 수가 0인경우 그 칸의 숫자는 주사위의 바닥칸의 숫자가 됩니다.

작동 순서
1. 맵의 크기와 시작점 그리고 명령의 개수를 입력받습니다.
2. 맵에 적혀있는 숫자를 입력받습니다.
3. 명령의 순서를 입력받습니다.
4. 각 주사위를 굴려가며 해당 칸에 있는 숫자에 맞는 연산을 수행합니다.
5. 주사위를 굴릴 경우 동쪽이나 서쪽으로 이동시 다음 이동시의 동쪽과 서쪽 칸을 변경하고 북쪽이나 남쪽으로 이동시 다음 이동시의 북쪽과 서쪽 칸을 변경합니다.
6. 명령이 맵의 범위를 벗어나는 경우 그 명령은 무시합니다.
7. 모든 명령을 수행하고 난뒤 각 명령을 수행한뒤의 맨 윗칸의 숫자들을 차례대로 출력합니다.

소스코드

import java.io.*;
import java.util.*;

public class 백준_14499번_주사위굴리기 {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static StringBuilder sb=new StringBuilder();
    static Queue<Integer> qX = new LinkedList<>();
    static Queue<Integer> qY = new LinkedList<>();
    static Queue<Integer> qCmd = new LinkedList<>();
    static int N, M, K;
    static int[][] map;
    static int[][] move = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
    static int[] diceMove={3, 4, 2, 5};//동서남북
    static int[] dice = new int[7];
    static int topside=1;

    static void inputSetting() throws IOException{
        st = new StringTokenizer(br.readLine());
        N=Integer.parseInt(st.nextToken());
        M=Integer.parseInt(st.nextToken());
        qX.add(Integer.parseInt(st.nextToken()));
        qY.add(Integer.parseInt(st.nextToken()));
        K=Integer.parseInt(st.nextToken());
    }

    static void inputMap() throws IOException{
        for(int i=0;i<N;i++){
            st = new StringTokenizer(br.readLine());
            for(int j=0;j<M;j++) map[i][j]=Integer.parseInt(st.nextToken());
        }
    }

    static void inputCmd() throws IOException{
        st = new StringTokenizer(br.readLine());
        for(int i=0;i<K;i++) qCmd.add(Integer.parseInt(st.nextToken()));
    }

    static void selectDirection(int direction){
        int temp;
        if(direction==0){//동쪽
            temp=diceMove[1];
            diceMove[0]=topside;
            diceMove[1]=7-topside;
        }
        else if(direction==1){//서쪽
            temp=diceMove[0];
            diceMove[0]=7-topside;
            diceMove[1]=topside;
        }
        else if(direction==2){//북쪽
            temp=diceMove[3];
            diceMove[2]=topside;
            diceMove[3]=7-topside;
        }
        else{//남쪽
            temp=diceMove[2];
            diceMove[2]=7-topside;
            diceMove[3]=topside;
        }
        topside=temp;
    }

    static void copy(int x, int y, int direction){
        if(map[x+move[direction][0]][y+move[direction][1]]==0){
            map[x+move[direction][0]][y+move[direction][1]]=dice[7-topside];
        }
        else {
            dice[7-topside]=map[x+move[direction][0]][y+move[direction][1]];
            map[x+move[direction][0]][y+move[direction][1]]=0;
        }
    }

    static void moveDice(){
        while(!qCmd.isEmpty()){
            int x=qX.poll(), y=qY.poll();
            int direction = qCmd.poll()-1;
            if(N > x+move[direction][0] & x+move[direction][0] >= 0 & M > y+move[direction][1] & y+move[direction][1] >= 0){
                selectDirection(direction);
                copy(x,y,direction);
                qX.add(x+move[direction][0]);
                qY.add(y+move[direction][1]);
                sb.append(dice[topside]).append("\n");
            }
            else{//명령이 범위를 벗어나면 해당 명령 무시
                qX.add(x);
                qY.add(y);
            }
        }
    }

    public static void main(String[] args) throws IOException{
        inputSetting();
        map = new int[N][M];
        inputMap();
        inputCmd();
        moveDice();
        System.out.print(sb);
    }
}
profile
학사지만 AI하고 싶어요...

0개의 댓글