알고리즘/BOJ

[BJ]백준 14499 주사위굴리기

  • -
반응형

백준 14499 주사위굴리기

 

 

문제링크

14499번: 주사위 굴리기 (acmicpc.net)

 

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

* 일단 문제를 정독 하고 1시간 이상 반드시 고민이 필요합니다.

 

동영상 설명

1시간 이상 고민 했지만 아이디어가 떠오르지 않는다면 동영상에서 약간의 힌트를 얻어봅시다.

 

소스보기

동영상 설명을 보고도 전혀 구현이 안된다면 연습 부족입니다.
소스를 보고 작성해 본 후 스스로 백지 상태에서 3번 작성해 볼 의지가 있다면 소스를 살짝 보세요.

더보기
package bj.gold.l4;

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

/**
 * @author 은서파
 * @since 2021. 10. 15.
 * @see https://www.acmicpc.net/problem/14499
 * @performance 11900 84
 * @category #시뮬레이션
 * @memo
 */

public class BJ_G4_14499_주사위굴리기 {

	static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	static StringBuilder output = new StringBuilder();
	static StringTokenizer tokens;
	static int N, M, R, C, K; // 지도의 세로, 가로, 주사위의 R, C, 명령의 개수
	static int[][] map;

	// 방향에 따른 좌표 변화: 동(1)/서(2)/북(3)/남(4)
	static int[][] deltas = { {}, { 0, 1 }, { 0, -1 }, { -1, 0 }, { 1, 0 } };

	public static void main(String[] args) throws IOException {
		input = new BufferedReader(new StringReader(src));
		tokens = new StringTokenizer(input.readLine());
		N = Integer.parseInt(tokens.nextToken());
		M = Integer.parseInt(tokens.nextToken());
		R = Integer.parseInt(tokens.nextToken());
		C = Integer.parseInt(tokens.nextToken());
		K = Integer.parseInt(tokens.nextToken());

		map = new int[N][M];
		for (int r = 0; r < N; r++) {
			tokens = new StringTokenizer(input.readLine());
			for (int c = 0; c < M; c++) {
				map[r][c] = Integer.parseInt(tokens.nextToken());
			}
		}// 지도 입력 완료

		tokens = new StringTokenizer(input.readLine());
		Dice dice = new Dice(R, C);
		for (int k = 0; k < K; k++) {
			int dir = Integer.parseInt(tokens.nextToken());
			// 이 방향으로 굴리면서 정답 출력
			int nr = dice.r + deltas[dir][0];
			int nc = dice.c + deltas[dir][1];

			if (isIn(nr, nc)) {
				dice.r = nr;
				dice.c = nc;

				int top = dice.roll(dir);
				output.append(top).append('\n');
			}
		}
		System.out.println(output);
	}

	private static boolean isIn(int r, int c) {
		return 0 <= r && r < N && 0 <= c && c < M;
	}

	static class Dice {
		int r, c;
		// 처음에 주사위의 모든 면은 0
		int[] dice = new int[6];

		public Dice(int r, int c) {
			this.r = r;
			this.c = c;
		}

		// 주사위의 방향을 미리 변수로 만들어두자.
		int TOP = 0, BOTTOM = 1, FRONT = 2, REAR = 3, LEFT = 4, RIGHT = 5;

		public int roll(int d) {
			if (d == 1) { // 동쪽으로 돌릴 때
				rotate(BOTTOM, LEFT, TOP, RIGHT);
			} else if (d == 2) { // 서쪽으로 돌릴 때
				rotate(BOTTOM, RIGHT, TOP, LEFT);
			} else if (d == 3) { // 북쪽으로 돌릴 때
				rotate(BOTTOM, FRONT, TOP, REAR);
			} else { // 남쪽으로 돌릴 때
				rotate(BOTTOM, REAR, TOP, FRONT);
			}
			// 바닥의 값에 따라서 주사위와 상호 작용
			if (map[r][c] == 0) { // 주사위의 값을 map에 복사해주기
				map[r][c] = dice[BOTTOM];
			} else { //map의 값을 주사위에 복사하기, 지도는 0
				dice[BOTTOM] = map[r][c];
				map[r][c] = 0;
			}
			// 꼭대기에 있는 값을 반환!
			return dice[TOP];
		}

		private void rotate(int a, int b, int c, int d) {
			int temp = dice[d];
			dice[d] = dice[c];
			dice[c] = dice[b];
			dice[b] = dice[a];
			dice[a] = temp;
		}
	}

	// REMOVE_START
	private static String src = "3 3 0 0 16\r\n"	+
								"0 1 2\r\n" +
								"3 4 5\r\n" +
								"6 7 8\r\n" +
								"4 4 1 1 3 3 2 2 4 4 1 1 3 3 2 2";
	// REMOVE_END		
}

 

반응형

'알고리즘 > BOJ' 카테고리의 다른 글

[BJ]백준 15486. 퇴사2  (0) 2021.10.20
[BJ]14501 퇴사  (0) 2021.10.20
[BJ]BJ G5 3190 뱀  (0) 2021.10.12
[BJ]BJ G2 12100 2048(Easy)  (0) 2021.10.04
[BJ]BJ_G2_13460_구슬탈출2  (0) 2021.09.26
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.