문제 풀이/Simulation
[백준 14499] 주사위 굴리기
명란파스타
2020. 7. 26. 15:29
14499번: 주사위 굴리기
첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다. 둘째 줄부터 N개의 줄에 지도
www.acmicpc.net
문제 이해만 제대로 하면 쉽게 구현이 가능한 문제이다.
특별한 알고리즘이 필요없다!
주사위를 동, 서, 남, 북으로 굴릴 때 주사위의 번호 위치가 어떻게 바뀌는지만 생각해주면 된다.
change_dice 함수에서 주사위의 번호를 바꾸는 구현을 했다.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int N, M, K;
int arr[20][20];
pair<int, int> start;
vector<int> dir;
int pos[4][2] = { {0,1}, {0,-1}, {-1,0}, {1,0} }; // 동, 서, 북, 남
int dice[6];
bool isIn(int x, int y)
{
if (x >= 0 && x < M && y >= 0 && y < N)
return true;
return false;
}
void change_dice(int y, int x, int d)
{
int nx = x + pos[d][1];
int ny = y + pos[d][0];
if (!isIn(nx, ny))
{
return;
}
int tmp_dice[6];
if (d == 0)
{
tmp_dice[0] = dice[0];
tmp_dice[1] = dice[5];
tmp_dice[2] = dice[2];
tmp_dice[3] = dice[4];
tmp_dice[4] = dice[1];
tmp_dice[5] = dice[3];
}
else if (d == 1)
{
tmp_dice[0] = dice[0];
tmp_dice[1] = dice[4];
tmp_dice[2] = dice[2];
tmp_dice[3] = dice[5];
tmp_dice[4] = dice[3];
tmp_dice[5] = dice[1];
}
else if (d == 2)
{
tmp_dice[0] = dice[1];
tmp_dice[1] = dice[2];
tmp_dice[2] = dice[3];
tmp_dice[3] = dice[0];
tmp_dice[4] = dice[4];
tmp_dice[5] = dice[5];
}
else {
tmp_dice[0] = dice[3];
tmp_dice[1] = dice[0];
tmp_dice[2] = dice[1];
tmp_dice[3] = dice[2];
tmp_dice[4] = dice[4];
tmp_dice[5] = dice[5];
}
for (int i = 0; i < 6; i++)
{
dice[i] = tmp_dice[i];
}
if (arr[ny][nx] == 0)
{
arr[ny][nx] = dice[3]; //밑면
}
else
{
dice[3] = arr[ny][nx];
arr[ny][nx] = 0;
}
start.first = ny;
start.second = nx;
cout << dice[1] << endl;
}
int main()
{
cin >> N >> M >> start.first >> start.second >> 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++)
{
int a;
cin >> a;
change_dice(start.first, start.second, a - 1);
}
}