Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

Life Engineering

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

Problem Solving

[BOJ 14499] 주사위 굴리기 (Java)

흑개 2022. 2. 23. 14:36

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

 

14499번: 주사위 굴리기

첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x, y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다. 둘째 줄부터 N개의 줄에 지

www.acmicpc.net

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main_BOJ_14499 {
	static int N, M, K;
	static int[][] map;
	static int[] dice;	//번호 저장
	static int[] neighbors= {0,3,4,2,5};	//현재 바닥면에서 동, 서, 북, 남 방향 번호
	static int bottom=6;	//현재 바닥면 
	static int[] opposites= {0,6,5,4,3,2,1};
	static int[] dr= {0,0,0,-1,1};
	static int[] dc= {0,1,-1,0,0};
	static int curr_r, curr_c;
	public static void main(String[] args) throws IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer(br.readLine());
		N=Integer.parseInt(st.nextToken());
		M=Integer.parseInt(st.nextToken());
		curr_r=Integer.parseInt(st.nextToken());
		curr_c=Integer.parseInt(st.nextToken());
		K=Integer.parseInt(st.nextToken());
		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());
			}
		}
		dice=new int[7];
		st=new StringTokenizer(br.readLine());
		for (int i=0; i<K; i++) {
			move(Integer.parseInt(st.nextToken()), curr_r, curr_c);
		}

	}
	private static void move(int dir, int r, int c) {
		int nr=r+dr[dir];
		int nc=c+dc[dir];
		int prev=bottom;
		if (nr<0 || nc<0 || nr>=N || nc>=M) return;
		if (dir==1) {		//동
			copyBottom(nr, nc, dir); //바닥면 수 복사처리 해주고
			neighbors[2]=prev; 	//이웃 번호 바꿔주기
			neighbors[1]=opposites[prev];
		}
		else if (dir==2) {		//서
			copyBottom(nr, nc, dir); 
			neighbors[1]=prev;
			neighbors[2]=opposites[prev];
		}
		else if (dir==3) {		//북
			copyBottom(nr, nc, dir); 
			neighbors[4]=prev;
			neighbors[3]=opposites[prev];
		}
		else if (dir==4) {		//남
			copyBottom(nr, nc, dir); 
			neighbors[3]=prev;
			neighbors[4]=opposites[prev];
		}
		//System.out.println(Arrays.toString(dice));
		//System.out.println("bottom : "+bottom);
		System.out.println(dice[opposites[bottom]]);
		curr_r=nr;
		curr_c=nc;	
	}
	
	private static void copyBottom(int nr, int nc, int dir) {
		bottom=neighbors[dir];
		if (map[nr][nc]==0) {
			map[nr][nc]=dice[bottom];
		}
		else {
			dice[bottom]=map[nr][nc];
			map[nr][nc]=0;
		}
	}

}

주사위를 굴릴 때마다

1) 바닥면에 수 옮기고 적어주는 처리를 해주기

2) 현재 수가 bottom 에 왔을 때, 인접 동서남북의 수는 무엇인지 update 해주기

 

움직이는 명령이 들어올 때마다, 해당 방향으로 굴려주고 그때 바닥 수를 bottom에 저장 후, 그를 기준으로 수를 옮기고 인접 동서남북이 직전 bottom과 비교해서 어떤 식으로 변하는지 체크해줘서 그 관계에 따라 수를 update 해주면 된다.

 

다른 분들의 코드를 보니, dice 배열 하나만으로도 가능하다.

https://donggoolosori.github.io/2020/11/09/boj-14499/

아랫면=>6번 인덱스, 윗면=>1번 인덱스, 북=>2번 인덱스, 동=>3번 인덱스, 서=>4번 인덱스, 남=>5번 인덱스로 고정하고, 움직일 때마다, 값을 규칙에 맞게 바꿔준다.

바닥면은 dice[6] 으로 접근한다. 

'Problem Solving' 카테고리의 다른 글

[BOJ 17143] 낚시왕 (Java)  (0) 2022.02.24
[BOJ 16236] 아기 상어 (Java)  (0) 2022.02.23
[BOJ 17837] 새로운 게임 2 (Java)  (0) 2022.02.22
[BOJ 12100] 2048 (Easy) (Java)  (0) 2022.02.21
[BOJ 15685] 드래곤 커브 (Java)  (0) 2022.02.21