[BOJ 14499] 주사위 굴리기
업데이트:
문제
- BOJ 14499
- 문제의 저작권은 Baekjoon Online Judge에 있습니다.
접근방식
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
//주사위 굴리기
public class Main {
static int N, M, K; //세로, 가로, 명령개수
static int x, y; //주사위 좌표
static StringTokenizer st;
static int[][] map; //지도
static int dir; //방향
static int nx, ny;
static int[] dice; //각 주사위 면 값
static int[] di = {0, 0, 0, -1, 1}; //우,좌,상,하
static int[] dj = {0, 1, -1, 0, 0};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
map = new int[N][M];
dice = new int[7];
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());
}
}//input 지도값
st = new StringTokenizer(br.readLine());
for(int i=0; i<K; i++) {
dir = Integer.parseInt(st.nextToken()); //input 방향
nx = x + di[dir]; //다음 방향
ny = y + dj[dir];
//위1 아래6 왼4 우3 앞5 뒤2
//1. 주사위 다음 위치 범위 체크
int tmp = 0;
if(nx>=0 && nx<N && ny>=0 && ny<M) {
//2. 방향에 따라 주사위 위치 바꾸기
if(dir == 1) { //우
tmp = dice[1];
dice[1] = dice[4];
dice[4] = dice[6];
dice[6] = dice[3];
dice[3] = tmp;
}else if(dir == 2) { //좌
tmp = dice[1];
dice[1] = dice[3];
dice[3] = dice[6];
dice[6] = dice[4];
dice[4] = tmp;
}else if(dir == 3) { //상
tmp = dice[1];
dice[1] = dice[5];
dice[5] = dice[6];
dice[6] = dice[2];
dice[2] = tmp;
}else { //하
tmp = dice[1];
dice[1] = dice[2];
dice[2] = dice[6];
dice[6] = dice[5];
dice[5] = tmp;
}
//3. 주사위 상태 변경
//map이 0이면 주사위 바닥면을 map으로
if(map[nx][ny] == 0) map[nx][ny] = dice[6];
//map이 0이 아니면 map을 주사위 바닥면으로
else {
dice[6] = map[nx][ny];
map[nx][ny] = 0;
}
System.out.println(dice[1]); //윗면 출력
x = nx;
y = ny;
}//범위 밖이면 출력x
}
}
}