알고리즘
백준 14499번 주사위 굴리기 C++
영춘권의달인
2022. 5. 8. 21:56
주사위의 정보를 담는 구조체를 만들어서 주사위 굴리는 것을 시뮬레이션 하면서 문제를 해결하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using ll = long long;
int dRow[5] = { 0,0,0,-1,1 };
int dCol[5] = { 0,1,-1,0,0 };
struct Dice {
int x;
int y;
int up;
int down;
int left;
int right;
int front;
int back;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m, x, y, k;
cin >> n >> m >> x >> y >> k;
vector<vector<int>> board(n, vector<int>(m));
vector<int> order(k);
Dice dice = { x,y,0,0,0,0,0,0 };
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> board[i][j];
}
}
for (int i = 0; i < k; i++) {
cin >> order[i];
}
for (int i = 0; i < k; i++) {
int dir = order[i];
int nextRow = dice.x + dRow[dir];
int nextCol = dice.y + dCol[dir];
if (nextRow < 0 || nextRow >= n) continue;
if (nextCol < 0 || nextCol >= m) continue;
Dice lastDice = { dice.x,dice.y,dice.up,dice.down,dice.left,dice.right,dice.front,dice.back };
if (dir == 1) {
dice.up = lastDice.left;
dice.down = lastDice.right;
dice.left = lastDice.down;
dice.right = lastDice.up;
}
else if (dir == 2) {
dice.up = lastDice.right;
dice.down = lastDice.left;
dice.left = lastDice.up;
dice.right = lastDice.down;
}
else if (dir == 3) {
dice.up = lastDice.back;
dice.down = lastDice.front;
dice.front = lastDice.up;
dice.back = lastDice.down;
}
else if (dir == 4) {
dice.up = lastDice.front;
dice.down = lastDice.back;
dice.front = lastDice.down;
dice.back = lastDice.up;
}
dice.x = nextRow;
dice.y = nextCol;
if (board[dice.x][dice.y] == 0) {
board[dice.x][dice.y] = dice.down;
}
else {
dice.down = board[dice.x][dice.y];
board[dice.x][dice.y] = 0;
}
cout << dice.up << "\n";
}
};