|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.BufferedWriter; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.io.OutputStreamWriter; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + |
| 8 | + static int[][] map; |
| 9 | + |
| 10 | + public static void main(String[] args) throws Exception { |
| 11 | + |
| 12 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 14 | + |
| 15 | + //초기화 |
| 16 | + String[] line = br.readLine().split(" "); |
| 17 | + int N = Integer.parseInt(line[0]); //지도의 세로 크기 (1 ≤ N ≤ 20) |
| 18 | + int M = Integer.parseInt(line[1]); //지도의 가로 크기 (1 ≤ M ≤ 20) |
| 19 | + int x = Integer.parseInt(line[2]); //주사위를 놓은 곳의 좌표 x (0 ≤ x ≤ N-1) |
| 20 | + int y = Integer.parseInt(line[3]); //주사위를 놓은 곳의 좌표 y (0 ≤ y ≤ M-1) |
| 21 | + int K = Integer.parseInt(line[4]); //명령의 개수 (1 ≤ K ≤ 1,000) |
| 22 | + |
| 23 | + map = new int[N][M]; |
| 24 | + |
| 25 | + for (int i = 0; i < N; i++) { |
| 26 | + line = br.readLine().split(" "); |
| 27 | + |
| 28 | + for (int j = 0; j < M; j++) { |
| 29 | + map[i][j] = Integer.parseInt(line[j]); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + String[] orders = br.readLine().split(" "); //이동하는 명령 (동쪽은 1, 서쪽은 2, 북쪽은 3, 남쪽은 4) |
| 34 | + |
| 35 | + int[] dice = {0, 0, 0, 0, 0, 0}; |
| 36 | + |
| 37 | + int[] dx = {0, 0, -1, 1}; //동서북남 |
| 38 | + int[] dy = {1, -1, 0, 0}; |
| 39 | + |
| 40 | + |
| 41 | + //알고리즘 |
| 42 | + for (int i = 0; i < K; i++) { |
| 43 | + int order = Integer.parseInt(orders[i]); |
| 44 | + |
| 45 | + int newX = x + dx[order - 1]; |
| 46 | + int newY = y + dy[order - 1]; |
| 47 | + |
| 48 | + if (newX >= 0 && newX < N && newY >= 0 && newY < M) { |
| 49 | + x = newX; |
| 50 | + y = newY; |
| 51 | + int[] newDice = null; |
| 52 | + |
| 53 | + switch(order) { |
| 54 | + case 1: //동 |
| 55 | + newDice = goEast(dice); |
| 56 | + break; |
| 57 | + case 2: //서 |
| 58 | + newDice = goWest(dice); |
| 59 | + break; |
| 60 | + case 3: //북 |
| 61 | + newDice = goNorth(dice); |
| 62 | + break; |
| 63 | + case 4: //남 |
| 64 | + newDice = goSouth(dice); |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + dice = newDice; |
| 69 | + copyNum(x, y, dice); |
| 70 | + bw.write(dice[0] + "\n"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + //출력 |
| 76 | + bw.flush(); |
| 77 | + bw.close(); |
| 78 | + br.close(); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + static int[] goEast(int[] dice) { |
| 83 | + return new int[] {dice[3], dice[1], dice[0], dice[5], dice[4], dice[2]}; |
| 84 | + } |
| 85 | + |
| 86 | + static int[] goWest(int[] dice) { |
| 87 | + return new int[] {dice[2], dice[1], dice[5], dice[0], dice[4], dice[3]}; |
| 88 | + } |
| 89 | + |
| 90 | + static int[] goNorth(int[] dice) { |
| 91 | + return new int[] {dice[4], dice[0], dice[2], dice[3], dice[5], dice[1]}; |
| 92 | + } |
| 93 | + |
| 94 | + static int[] goSouth(int[] dice) { |
| 95 | + return new int[] {dice[1], dice[5], dice[2], dice[3], dice[0], dice[4]}; |
| 96 | + } |
| 97 | + |
| 98 | + static void copyNum(int x, int y, int[] dice) { |
| 99 | + if (map[x][y] == 0) { |
| 100 | + map[x][y] = dice[5]; |
| 101 | + } else { |
| 102 | + dice[5] = map[x][y]; |
| 103 | + map[x][y] = 0; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments