시뮬레이션 문제입니다.
정육면체의 주사위가 있을때 굴려서 윗면에 쓰여 있는 수를 출력하는 문제입니다.
주사위의 각면의 초기값은 0이며, 주사위는 밖으로 나갈 수 없습니다.
바닥과 닿으면 바닥의 값을 닿은 면의 값에 복사하고 바닥의 값은 0이 됩니다.
바닥의 값이 0이면 주사위의 값을 바닥의 값에 복사합니다.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[][] map = new int[n][m];
int[] move = new int[k];
int[] dice = new int[6];
int[] dy = {0, 0, -1, 1};
int[] dx = {1, -1, 0, 0};
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());
}
}
st = new StringTokenizer(br.readLine());
for(int i=0; i<k; ++i) {
move[i] = Integer.parseInt(st.nextToken());
}
for(int i=0; i<k; ++i) {
int moveY = y + dy[move[i] - 1];
int moveX = x + dx[move[i] - 1];
int tmp;
if(moveY < 0 || moveX < 0 || moveY >= n || moveX >= m) {
continue;
}
y = moveY;
x = moveX;
if(move[i] == 1) { // 동쪽
tmp = dice[0];
dice[0] = dice[5];
dice[5] = dice[2];
dice[2] = dice[4];
dice[4] = tmp;
} else if (move[i] == 2) { // 서쪽
tmp = dice[0];
dice[0] = dice[4];
dice[4] = dice[2];
dice[2] = dice[5];
dice[5] = tmp;
} else if (move[i] == 3) { // 북쪽
tmp = dice[3];
dice[3] = dice[2];
dice[2] = dice[1];
dice[1] = dice[0];
dice[0] = tmp;
} else if (move[i] == 4) { // 남쪽
tmp = dice[0];
dice[0] = dice[1];
dice[1] = dice[2];
dice[2] = dice[3];
dice[3] = tmp;
}
if(map[y][x] == 0) {
map[y][x] = dice[0];
} else {
dice[0] = map[y][x];
map[y][x] = 0;
}
bw.write(String.valueOf(dice[2]));
bw.write("\n");
}
bw.flush();
}
}
'알고리즘' 카테고리의 다른 글
[Java] 백준 19236번 청소년 상어 (0) | 2023.02.03 |
---|---|
[Java] 백준 16236번 아기 상어 (0) | 2023.02.02 |
[Java] 백준 14890번 경사로 (0) | 2023.01.30 |
[Java] 백준 12100번 2048 (Eazy) (0) | 2023.01.30 |
[Java] 백준 3190번 뱀 (0) | 2023.01.29 |