반응형
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
</p
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
|
#include <iostream>
#define rep(i,n) for(int i=0;i<n;i++)
#define TOP dice[1][1]
#define BOTTOM dice[3][1]
using namespace std;
int n, m, x, y, k, arr[21][21], dice[4][3], cmd;
int dx[] = { 0, 0, 0, -1, 1 }; // 1: 동, 2: 서, 3: 북, 4: 남
int dy[] = { 0, 1, -1, 0, 0 };
void move(int cmd) { // 주사위 변경
int temp;
// 1. 동쪽
if (cmd == 1) {
temp = dice[3][1];
dice[3][1] = dice[1][2];
dice[1][2] = dice[1][1];
dice[1][1] = dice[1][0];
dice[1][0] = temp;
}
// 2. 서쪽
else if (cmd == 2) {
temp = dice[3][1];
dice[3][1] = dice[1][0];
dice[1][0] = dice[1][1];
dice[1][1] = dice[1][2];
dice[1][2] = temp;
}
// 3. 북쪽
else if (cmd == 3) {
temp = dice[0][1];
dice[0][1] = dice[1][1];
dice[1][1] = dice[2][1];
dice[2][1] = dice[3][1];
dice[3][1] = temp;
}
// 4. 남쪽
else if (cmd == 4) {
temp = dice[3][1];
dice[3][1] = dice[2][1];
dice[2][1] = dice[1][1];
dice[1][1] = dice[0][1];
dice[0][1] = temp;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> x >> y >> k;
rep(i, n) {
rep(j, m)
cin >> arr[i][j];
}
while (k--) {
cin >> cmd;
int nx = x + dx[cmd]; // 이동할 방향 설정
int ny = y + dy[cmd];
if (nx >= 0 && nx < n && ny >= 0 && ny < m) { // 범위 검사
move(cmd);
if (arr[nx][ny] == 0) // 현재 위치의 숫자가 0이면
arr[nx][ny] = BOTTOM; // 주사위 바닥 숫자를 현재 위치에 복사
else { // 현재 위치의 숫자가 0 아니면
BOTTOM = arr[nx][ny]; // 현재 위치의 숫자를 주사위 바닥에 복사하고
arr[nx][ny] = 0; // 현재 위치의 숫자를 0으로 변경
}
cout << TOP << '\n';
x = nx;
y = ny;
}
}
}
|
cs |
반응형