|
| 1 | +```cpp |
| 2 | +#include <bits/stdc++.h> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +class Dice { |
| 6 | + int east, west, south, north, top, bot; |
| 7 | +public: |
| 8 | + Dice() { |
| 9 | + top = 0; |
| 10 | + bot = 0; |
| 11 | + east = 0; |
| 12 | + west = 0; |
| 13 | + south = 0; |
| 14 | + north = 0; |
| 15 | + } |
| 16 | + |
| 17 | + int getTop() { return top; } |
| 18 | + int getBot() { return bot; } |
| 19 | + void setBot(int bot) { this->bot = bot; } |
| 20 | + |
| 21 | + void right() { |
| 22 | + int newEast = top; |
| 23 | + int newWest = bot; |
| 24 | + int newTop = west; |
| 25 | + int newBot = east; |
| 26 | + east = newEast, west = newWest, top = newTop, bot = newBot; |
| 27 | + } |
| 28 | + |
| 29 | + void left() { |
| 30 | + int newEast = bot; |
| 31 | + int newWest = top; |
| 32 | + int newTop = east; |
| 33 | + int newBot = west; |
| 34 | + east = newEast, west = newWest, top = newTop, bot = newBot; |
| 35 | + } |
| 36 | + |
| 37 | + void up() { |
| 38 | + int newTop = south; |
| 39 | + int newBot = north; |
| 40 | + int newSouth = bot; |
| 41 | + int newNorth = top; |
| 42 | + top = newTop, bot = newBot, south = newSouth, north = newNorth; |
| 43 | + } |
| 44 | + |
| 45 | + void down() { |
| 46 | + int newTop = north; |
| 47 | + int newBot = south; |
| 48 | + int newSouth = top; |
| 49 | + int newNorth = bot; |
| 50 | + top = newTop, bot = newBot, south = newSouth, north = newNorth; |
| 51 | + } |
| 52 | + |
| 53 | + void command(int num) { |
| 54 | + if (num == 1) right(); |
| 55 | + else if (num == 2) left(); |
| 56 | + else if (num == 3) up(); |
| 57 | + else down(); |
| 58 | + } |
| 59 | + |
| 60 | + // for debug |
| 61 | + void print() { |
| 62 | + cout << "= INFO =\n"; |
| 63 | + cout << "========\n"; |
| 64 | + cout << " " << north << '\n'; |
| 65 | + cout << west << ' ' << top << ' ' << east << '\n'; |
| 66 | + cout << " " << south << '\n'; |
| 67 | + cout << " " << bot << '\n'; |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +int N, M, x, y, K; |
| 72 | +int arr[20][20]{}; |
| 73 | +int dx[5] = { 0,0,0,-1,1 }; |
| 74 | +int dy[5] = { 0,1,-1,0,0 }; |
| 75 | + |
| 76 | +int main() { |
| 77 | + cin.tie(0)->sync_with_stdio(0); |
| 78 | + |
| 79 | + cin >> N >> M >> x >> y >> K; |
| 80 | + for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) cin >> arr[i][j]; |
| 81 | + |
| 82 | + Dice* dice = new Dice(); |
| 83 | + for (int cmd; K--;) { |
| 84 | + cin >> cmd; |
| 85 | + |
| 86 | + x += dx[cmd], y += dy[cmd]; |
| 87 | + if (x < 0 || x >= N || y < 0 || y >= M) { |
| 88 | + x -= dx[cmd], y -= dy[cmd]; |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + dice->command(cmd); |
| 93 | + cout << dice->getTop() << '\n'; |
| 94 | + |
| 95 | + if (arr[x][y] == 0) { |
| 96 | + arr[x][y] = dice->getBot(); |
| 97 | + } |
| 98 | + else { |
| 99 | + dice->setBot(arr[x][y]); |
| 100 | + arr[x][y] = 0; |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | +} |
| 106 | +``` |
0 commit comments