문제가 길면 일단 풀기 싫어지는데 잘 읽어보면 금방 풀 수 있는 문제.
주사위는 총 4방향으로 나아갈 수 있으며
북쪽으로 갈때는
1번 값은 2번 으로
5번 값은 1번 으로
6번 값은 5번 으로
2번 값은 6번 으로
....
이렇게 동, 서, 남, 북으로 움직였을 때 특정 위치의 값이 어떤 위치로 이동되는지 확인하고 코드를 짜면
가장 중요한 윗면과 아랫면은 항상 1번 면과 6번면이 되기 때문에 계산하기 쉽다.
단, 범위를 벗어났을 때는 아무일도 일어나지 말아야 한다.
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int M = scan.nextInt();
int r = scan.nextInt();
int c = scan.nextInt();
int C = scan.nextInt();
int[][] matrix = new int[N][M];
int[] dice= {0, 0, 0, 0, 0, 0, 0};
for(int i = 0; i<N; i++) {
for(int j = 0; j<M; j++) {
matrix[i][j] = scan.nextInt();
}
}
int[] dir = new int[C];
for(int i = 0; i<C; i++) {
dir[i] = scan.nextInt();
//동
if(dir[i] == 1) {
if(c+1 < M) {
c++;
int tmp = dice[3];
dice[3] = dice[1];
dice[1] = dice[4];
dice[4] = dice[6];
dice[6] = tmp;
}
else
continue;
}
//서
else if(dir[i] == 2) {
if(c-1 >=0 ) {
c--;
int tmp = dice[4];
dice[4] = dice[1];
dice[1] = dice[3];
dice[3] = dice[6];
dice[6] = tmp;
}
else
continue;
}
//북
else if(dir[i] == 3) {
if(r-1 >=0 ) {
r--;
int tmp = dice[2];
dice[2] = dice[1];
dice[1] = dice[5];
dice[5] = dice[6];
dice[6] = tmp;
}
else
continue;
}
//남
else {
if(r+1 < N) {
r++;
int tmp = dice[5];
dice[5] = dice[1];
dice[1] = dice[2];
dice[2] = dice[6];
dice[6] = tmp;
}
else
continue;
}
if(matrix[r][c] == 0) {
matrix[r][c] = dice[6];
}
else {
dice[6] = matrix[r][c];
matrix[r][c] = 0;
}
System.out.println(dice[1]);
}
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[BaekJoon] 백준 14503 로봇청소기 - Java (0) | 2020.05.25 |
---|---|
[BaekJoon] 백준 14501 퇴사 - Java (0) | 2020.05.21 |
[BaekJoon] 백준 2193 이친수 -Java (0) | 2020.05.17 |
[BaekJoon] 백준 2579. 계단 오르기 - Java (0) | 2020.05.14 |
[BaekJoon] 백준 1932. 정수 삼각형- Java (0) | 2020.05.13 |