BOJ14499: 주사위 굴리기

https://www.acmicpc.net/problem/14889

  • 연휴동안 100문제 풀기 (6/100)
  • 시뮬레이션 문제를, 잘 못푼다는거는 심각한 문제
  • 생각한대로 구현하는걸 잘해야한다.
  • 전치 후치 증감 연산을 함부로 쓰지 말자. 그냥 한줄 더쓰는게 낫다.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>
using namespace std;

class Dice {
public:
  int T = 0;
  int B = 0;
  int E = 0;
  int W = 0;
  int N = 0;
  int S = 0;

  void north() {
    int _B = N;
    int _S = B;
    int _T = S;
    int _N = T;
    B=_B, S=_S, T=_T, N=_N;
  }

  void south() {
    int _B = S;
    int _N = B;
    int _T = N;
    int _S = T;
    B=_B, S=_S, T=_T, N=_N;
  }

  void east() {
    int _B = E;
    int _W = B;
    int _T = W;
    int _E = T;
    B=_B, W=_W, T=_T, E=_E;
  }

  void west() {
    int _B = W;
    int _E = B;
    int _T = E;
    int _W = T;
    B=_B, W=_W, T=_T, E=_E;
  }
};

void copy(Dice& dice, int arr[][21], int sx, int sy) {
  if(arr[sx][sy]) dice.B = arr[sx][sy], arr[sx][sy] = 0;
  else arr[sx][sy] = dice.B;
}

int main() {
  ios::sync_with_stdio(false);
  int N, M, sx, sy, K, arr[21][21];
  cin >> N >> M >> sx >> sy >> K;
  for(int i = 0; i < N; i++)
    for(int j = 0; j < M; j++)
      cin >> arr[i][j];

  Dice dice = Dice();
  for(int i = 0; i < K; i++) {
    int cmd;
    cin >> cmd;
    if(cmd == 1) {
      if (sy == M-1) continue;
      dice.east();
      sy++;
      copy(dice, arr, sx, sy);
    } else if(cmd == 2) {
      if (sy == 0) continue;
      dice.west();
      sy--;
      copy(dice, arr, sx, sy);
    } else if(cmd == 3) {
      if (sx == 0) continue;
      dice.north();
      sx--;
      copy(dice, arr, sx, sy);
    } else if(cmd == 4) {
      if (sx == N-1) continue;
      dice.south();
      sx++;
      copy(dice, arr, sx, sy);
    }
    cout << dice.T << "\n";
  }
}