백준 14499번: 주사위 굴리기

최창효·2022년 3월 8일
0
post-thumbnail



문제 설명

  • 주사위가 주어진 명령에 따라 게임판을 움직입니다.
  • 게임판을 움직이면서 자신의 숫자와 게임판의 숫자를 바꿔나갑니다.

접근법

  • 주사위 객체를 만들고 실제로 움직여 보면서 값을 바꿔나가는 방식으로 구현했습니다.
  • 주사위 굴리는 법
    • 이전의 윗면 -> 현재의 오른쪽
      이전의 왼쪽 -> 현재의 윗면
      이전의 아랫면 -> 현재의 왼쪽
      이전의 오른쪽 -> 현재의 아랫면

정답

import java.util.*;

public class Main {
	public static void main(String[] args) {
    	// 입력값 처리
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		int x = sc.nextInt();
		int y = sc.nextInt();
		int K = sc.nextInt();
		int[][] board = new int[N][M];
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				board[i][j] = sc.nextInt();
			}
		}
        // 주사위를 만듭니다.
		dice Dice = new dice(x,y,0,0,0,0,0,0);
		for (int k = 0; k < K; k++) {
			int d = sc.nextInt(); // 이동 방향
            // nx,ny는 다음 이동칸을 나타냅니다.
			int nx = 0;
			int ny = 0;
			switch (d) {
			case 1://// 동쪽으로 1칸 
				nx = Dice.x;
				ny = Dice.y+1;
                // 이동할 수 없다면 다음 움직임을 진행합니다.
				if(0>nx || nx >= N || 0>ny || ny>= M) continue; 
                // continue되지 않았다는 건 이동이 가능하다는 의미입니다.
                
                // 주사위의 좌표를 업데이트 합니다.
				Dice.x = nx;
				Dice.y = ny;
                
                // 주사위를 굴립니다.
				Dice.roll_right();
				if(board[nx][ny] == 0) { // 보드값이 0이면
					board[nx][ny] = Dice.bottom; // 보드에 주사위의 바닥값을 복사합니다.
				}else { // 보드값이 0이 아니면
					Dice.bottom = board[nx][ny]; // 보드값을 주사위 바닥에 복사합니다.
					board[nx][ny] = 0; // 보드의 값은 0이 됩니다.
				}
				System.out.println(Dice.top);
				break;
			case 2://서
				nx = Dice.x;
				ny = Dice.y-1;
				if(0>nx || nx >= N || 0>ny || ny>= M) continue;
				Dice.x = nx;
				Dice.y = ny;
				Dice.roll_left();
				if(board[nx][ny] == 0) {
					board[nx][ny] = Dice.bottom;
				}else {
					Dice.bottom = board[nx][ny];
					board[nx][ny] = 0;
				}
				System.out.println(Dice.top);
				break;
			case 3://북
				nx = Dice.x-1;
				ny = Dice.y;
				if(0>nx || nx >= N || 0>ny || ny>= M) continue;
				Dice.x = nx;
				Dice.y = ny;
				Dice.roll_back();
				if(board[nx][ny] == 0) {
					board[nx][ny] = Dice.bottom;
				}else {
					Dice.bottom = board[nx][ny];
					board[nx][ny] = 0;
				}
				System.out.println(Dice.top);
				break;
			case 4://남
				nx = Dice.x+1;
				ny = Dice.y;
				if(0>nx || nx >= N || 0>ny || ny>= M) continue;
				Dice.x = nx;
				Dice.y = ny;
				Dice.roll_front();
				if(board[nx][ny] == 0) {
					board[nx][ny] = Dice.bottom;
				}else {
					Dice.bottom = board[nx][ny];
					board[nx][ny] = 0;
				}
				System.out.println(Dice.top);
				break;
			}
		}

	}
	
	static class dice{
		int x;
		int y;
		int top;
		int bottom;
		int left;
		int right;
		int front;
		int back;
		public dice(int x, int y, int top, int bottom, int left, int right, int front, int back) {
			super();
			this.x = x;
			this.y = y;
			this.top = top;
			this.bottom = bottom;
			this.left = left;
			this.right = right;
			this.front = front;
			this.back = back;
		}
		// prev변수들은 굴리기 전의 주사위 값입니다.
		public  void roll_right() {
			int prev_top = top;
			int prev_bottom = bottom;
			int prev_left = left;
			int prev_right = right;
			int prev_front = front;
			int prev_back = back;
			right = prev_top;
			top = prev_left;
			left = prev_bottom;
			bottom = prev_right;
		}
		public  void roll_left() {
			int prev_top = top;
			int prev_bottom = bottom;
			int prev_left = left;
			int prev_right = right;
			int prev_front = front;
			int prev_back = back;
			right = prev_bottom;
			top = prev_right;
			left = prev_top;
			bottom = prev_left;
		}
		public  void roll_front() {
			int prev_top = top;
			int prev_bottom = bottom;
			int prev_left = left;
			int prev_right = right;
			int prev_front = front;
			int prev_back = back;
			front = prev_top;
			top = prev_back;
			back = prev_bottom;
			bottom = prev_front;
		}
		public  void roll_back() {
			int prev_top = top;
			int prev_bottom = bottom;
			int prev_left = left;
			int prev_right = right;
			int prev_front = front;
			int prev_back = back;
			front = prev_bottom;
			top = prev_front;
			back = prev_top;
			bottom = prev_back;
		}
		
		
		@Override
		public String toString() {
			return "dice [x=" + x + ", y=" + y + ", top=" + top + ", bottom=" + bottom + ", left=" + left + ", right="
					+ right + ", front=" + front + ", back=" + back + "]";
		}
		
	}
}

기타

유사한 문제

profile
기록하고 정리하는 걸 좋아하는 개발자.

0개의 댓글