백준[백준][java] 주사위 굴리기삼성SW역량테스트 기출문제Yulha0982021.04.21 · 조회 4 · 좋아요 0 백준 삼성SW역량테스트 기출문제주사위 굴리기 풀이import java.util.Scanner; public class 주사위_굴리기 { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); // Scanner sc = new Scanner(new FileInputStream("src\\input.txt")); int N = sc.nextInt(); int M = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); int K = sc.nextInt(); int[][] map = new int[N][M]; int[] move = new int[K]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { map[i][j] = sc.nextInt(); } } for (int i = 0; i < K; i++) { // 인덱스 계산이 쉽게 -1로 받음 move[i] = sc.nextInt() - 1; } rollDice(move, x, y, N, M, map); } private static void rollDice(int[] move, int x, int y, int n, int m, int[][] map) { Dice dice = new Dice(0, 5, 4, 1, 2, 3); int[] dice_val = new int[6]; for (int i = 0; i < move.length; i++) { if (move[i] == 0) { // 동쪽으로 y++; if (y >= m) { y--; continue; } } if (move[i] == 1) { // 서쪽으로 y--; if (y < 0) { y++; continue; } } if (move[i] == 2) { // 북쪽으로 x--; if (x < 0) { x++; continue; } } if (move[i] == 3) { // 남쪽으로 x++; if (x >= n) { x--; continue; } } dice.roll(move[i]); if (map[x][y] == 0) { // 칸이 0이면 // 주사위 바닥 값이 칸으로 복사 map[x][y] = dice_val[dice.bottom]; } else { // 칸이 0이 아니면 // 칸 값이 주사위 바닥으로 복사 dice_val[dice.bottom] = map[x][y]; // 칸은 0 map[x][y] = 0; } System.out.println(dice_val[dice.top]); } } private static class Dice { int top; int bottom; int front; int back; int right; int left; public Dice(int tp, int bm, int ft, int bk, int rt, int lt) { this.top = tp; this.bottom = bm; this.front = ft; this.back = bk; this.right = rt; this.left = lt; } public void roll(int dir) { int tp = this.top; int bm = this.bottom; int ft = this.front; int bk = this.back; int rt = this.right; int lt = this.left; if (dir == 0) { this.top = lt; this.bottom = rt; this.left = bm; this.right = tp; } if (dir == 1) { this.top = rt; this.bottom = lt; this.left = tp; this.right = bm; } if (dir == 2) { this.top = ft; this.bottom = bk; this.front = bm; this.back = tp; } if (dir == 3) { this.top = bk; this.bottom = ft; this.front = tp; this.back = bm; } } } }정리 단순 구현 문제Dice 클래스에서 지시한 방향으로 굴리는 메소드를 정의해서 주어진 조건에 맞게 시뮬레이션Yulha098 Yulha098님의 창작활동을 응원하고 싶으세요?후원하기 태그백준알고리즘시뮬레이션java코딩테스트이전글[백준][java] 연구소삼성SW역량테스트 기출문제다음글[백준][java] 로봇 청소기삼성SW역량테스트 기출문제Code_Yulha구독자 2명0개의 댓글