티스토리 뷰

Algorithm

[백준] G5 14499 주사위 굴리기 (java)

코딩브론즈 2020. 12. 20. 21:43

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

 

풀이

 

1) Dice라는 클래스를 만들거임. 멤버변수는 (행, 렬, 앞, 뒤, 상, 하, 좌, 우)

2) 굴리는 경우는 4가지 밖에 없다. (위, 아래, 오른쪽, 왼쪽) 메소드를 생성한다.

3) 방향을 Queue에 담는다.

4) Queue가 빌 때까지 빼내면서 방향에 따라 다음 좌표를 구한다 (nr, nc)

5) 범위체크(isIn[nr][nc])를 한 뒤 true 이면 dir에 따라서 2번에서 생성한 메소드를 ㄱㄱ

6) map[nr][nc]가 0일 경우와 0이 아닐경우로 나눈다

6-1) 0일경우 : map[nr][nc] = 주사위 바닥면

6-2) 0이 아닐경우 : 주사위 바닥면 = map[nr][nc]; map[nr][nc] = 0;

7) 주사위 윗면을 출력

 

 

주의사항

 

1) 공간지각능력

 

package com.baekJoon;

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

public class BJ_G5_14499_주사위굴리기 {
	static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer tokens;
	static int N,M,x,y,K,map[][];
	static Queue<Integer> queue = new LinkedList<>();
	static Dice dice;
	public static void main(String[] args) throws NumberFormatException, IOException {
		input = new BufferedReader(new StringReader(src));
		tokens = new StringTokenizer(input.readLine());
		N = Integer.parseInt(tokens.nextToken());
		M = Integer.parseInt(tokens.nextToken());
		x = Integer.parseInt(tokens.nextToken());
		y = Integer.parseInt(tokens.nextToken());
		K = Integer.parseInt(tokens.nextToken());
		
		// r, c, front, tail, up, down, left, right
		dice = new Dice(x, y, 0, 0, 0, 0, 0, 0);
		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());
		for(int k=0; k<K; k++) {
			queue.offer(Integer.parseInt(tokens.nextToken()));
		}// 입력 끝
		
		run();
	}

	private static void run() {
		while(!queue.isEmpty()) {
			int dir = queue.poll()-1;
			
			int nr = dice.r + dr[dir];
			int nc = dice.c + dc[dir];
			if(isIn(nr, nc)) {
				dice.r = nr;
				dice.c = nc;
				switch (dir) {
				case 0: // 동
					rightDice();
					change(nr, nc);
					break;
				case 1: // 서
					leftDice();
					change(nr, nc);
					break;
				case 2: // 북
					upDice();
					change(nr, nc);
					break;
				case 3: // 남
					downDice();
					change(nr, nc);
					break;
				}
				System.out.println(dice.front);
			}
			
		}
	}
	private static void change(int nr, int nc) {
		if(map[nr][nc] == 0) {
			map[nr][nc] = dice.tail;
		}else {
			dice.tail = map[nr][nc];
			map[nr][nc] = 0;
		}		
	}

	private static void rightDice() {
		int temp = dice.left;
		dice.left = dice.tail;
		dice.tail = dice.right;
		dice.right = dice.front;
		dice.front = temp;
		
	}

	private static void leftDice() {
		int temp = dice.front;
		dice.front = dice.right;
		dice.right = dice.tail;
		dice.tail = dice.left;
		dice.left = temp;
	}

	private static void upDice() {
		int temp = dice.front;
		dice.front = dice.down;
		dice.down = dice.tail;
		dice.tail = dice.up;
		dice.up = temp;
	}

	private static void downDice() {
		int temp = dice.up;
		dice.up = dice.tail;
		dice.tail = dice.down;
		dice.down = dice.front;
		dice.front = temp;
	}
	
	static int dr[] = {0,0,-1,1};
	static int dc[] = {1,-1,0,0};
	
	static boolean isIn(int r, int c) {
		return (r>=0 && c>=0 && r<N && c<M);
	}

	static class Dice {
		int r, c, front, tail, up, down, left, right;
		public Dice(int r, int c, int front, int tail, int up, int down, int left, int right) {
			super();
			this.r = r;
			this.c = c;
			this.front = front;
			this.tail = tail;
			this.up = up;
			this.down = down;
			this.left = left;
			this.right = right;
		}
	}
	
	static String src =
			"3 3 1 1 9\r\n" + 
			"1 2 3\r\n" + 
			"4 0 5\r\n" + 
			"6 7 8\r\n" + 
			"1 3 2 2 4 4 1 1 3";
}

 

 

후기

 

요근래 푼 문제중에 제일 재미있는 문제였다. 처음 봤을땐 이게 왜 골드5 밖에 안되지.. 했지만 노트에 쓰면서 풀이방안을 정리해보니 생각보다 쉽고 재밌는 문제였다는것을 알게 되었다. 요즘 시뮬레이션 문제를 많이 풀다보니 뇌가 조금 유연해지는것 같아서 좋다. 언제나 초심을 잃지 않고 끝까지 최선을 다하면 빛을 볼 날이 오겠지

'Algorithm' 카테고리의 다른 글

[백준] B1 5212 지구 온난화 (java)  (0) 2020.12.21
[백준] S4 20291 파일 정리 (java)  (0) 2020.12.21
[백준] S2 15662 톱니바퀴2 (java)  (1) 2020.12.20
[백준] S2 2290 LCD Test (java)  (0) 2020.12.20
[백준] S5 3568 iSharp (java)  (0) 2020.12.20
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/03   »
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 31
글 보관함