[BOJ 14499] 주사위 굴리기
백준 :: BOJ :: 14499 :: 주사위 굴리기
출처 : https://www.acmicpc.net/problem/14499
#include <bits/stdc++.h>
using namespace std;
int N, M, x, y, K;
int arr[20][20];
vector<int>dice = { 0,0,0,0,0,0 };
int dx[] = {0,1,-1,0,0}, dy[] = {0,0,0,-1,1};
inline bool check(int i, int j) { return i >= 0 && j >= 0 && i < N && j < M ? true : false; }
void Rolling(int d) {
vector<int>tmp(dice);
if (d == 1) {
dice[0] = tmp[3]; dice[2] = tmp[0]; dice[3] = tmp[5]; dice[5] = tmp[2];
}
else if (d == 2) {
dice[0] = tmp[2]; dice[2] = tmp[5]; dice[3] = tmp[0]; dice[5] = tmp[3];
}
else if (d == 3) {
dice[1] = tmp[0]; dice[0] = tmp[4]; dice[4] = tmp[5]; dice[5] = tmp[1];
}
else {
dice[1] = tmp[5]; dice[0] = tmp[1]; dice[4] = tmp[0]; dice[5] = tmp[4];
}
}
int main() {
std::ios::sync_with_stdio(false);cin.tie(0);
cin >> N >> M >> y >> x >> K; vector<int> order(K);
for (int i = 0;i < N;i++) for (int j = 0; j < M; j++) cin >> arr[i][j];
for (int i = 0; i < K; i++) cin >> order[i];
for (int k = 0; k < K; k++) {
if (check(y + dy[order[k]], x + dx[order[k]])) {
y += dy[order[k]]; x += dx[order[k]]; Rolling(order[k]);
if (arr[y][x]) {
dice[5] = arr[y][x]; arr[y][x] = 0;
}
else arr[y][x] = dice[5];
cout << dice[0] << '\n';
}
}
return 0;
}