슈콩

[BOJ] 14499 주사위 굴리기 본문

Algorithms/Baekjoon

[BOJ] 14499 주사위 굴리기

shukong 2025. 11. 30. 23:24

 

 

[문제]

https://www.acmicpc.net/problem/14499

 

 

[소스 코드]

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

public class Main {
	static int[] dr = {0,0,0,-1,1};
	static int[] dc = {0,1,-1,0,0};
	static int[] dice = new int[6];
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		int x = Integer.parseInt(st.nextToken());
		int y = Integer.parseInt(st.nextToken());
		int k = Integer.parseInt(st.nextToken());
		int[][] map = new int[n][m];
		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());
			}
		}
		st = new StringTokenizer(br.readLine());
		while(k-->0) {
			int dir = Integer.parseInt(st.nextToken());
			int nr = x + dr[dir];
			int nc = y + dc[dir];
			if(nr<0 || nr>=n || nc<0 || nc>=m) continue;
			rotate(dir);
			if(map[nr][nc]==0) map[nr][nc] = dice[5];
			else {
				dice[5] = map[nr][nc];
				map[nr][nc] = 0;
			}
			x = nr; y = nc;
			System.out.println(dice[0]);
		}
	}
	private static void rotate(int dir) {
		int[] temp = new int[6];
		temp = dice.clone();
		if(dir==1) {
			int tmp = dice[0];
			dice[0] = temp[3];
			dice[3] = temp[5];
			dice[5] = temp[2];
			dice[2] = tmp;
		}
		else if(dir==2) {
			int tmp = dice[0];
			dice[0] = temp[2];
			dice[2] = temp[5];
			dice[5] = temp[3];
			dice[3] = tmp;
		}
		else if(dir==3) {
			int tmp = dice[1];
			dice[1] = temp[0];
			dice[0] = temp[4];
			dice[4] = temp[5];
			dice[5] = tmp;
		}
		else {
			int tmp = dice[5];
			dice[5] = temp[4];
			dice[4] = temp[0];
			dice[0] = temp[1];
			dice[1] = tmp;
		}
	}
}

 

회전 : 기준 값에서 회전되는 값 확인 (공간 추론)

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 14501 퇴사  (0) 2025.12.01
[BOJ] 14500 테트로미노  (0) 2025.12.01
[BOJ] 13458 시험 감독  (0) 2025.11.30
[BOJ] 3190 뱀  (0) 2025.11.30
[BOJ] 12100 2024(Easy)  (0) 2025.11.30