문제 링크
알고리즘 분류
시뮬레이션
풀이
문제에 나와있는 모든 조건을 구현해야 하는 시뮬레이션 문제입니다.
동,서,남,북 이동 방향에 따라 주사위의 면을 옮겨주는 함수를 구현하고,
조건에 맞춰 이동한 칸에 수가 있을 때와 없을 때의 조건을 구현해주면 됩니다.
코드
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
#include <iostream>
using namespace std;
int dy[5] = { 0,0,0,-1,1 };
int dx[5] = { 0,1,-1,0,0 };
int n, m, sy, sx , k;
int map[21][21];
int d[7],cd[7];
void dmove(int dir)
{
for (int i = 1; i <= 6; i++)
cd[i] = d[i];
int ny, nx;
ny = sy + dy[dir];
nx = sx + dx[dir];
if (ny < 0 || nx < 0 || ny >= n || nx >= m)
return;
sy = ny;
sx = nx;
if (dir == 1) // 동쪽
{
d[3] = cd[6];
d[1] = cd[3];
d[4] = cd[1];
d[6] = cd[4];
d[2] = cd[2];
d[5] = cd[5];
}
if (dir == 2) // 서쪽
{
d[1] = cd[4];
d[3] = cd[1];
d[6] = cd[3];
d[4] = cd[6];
d[5] = cd[5];
d[2] = cd[2];
}
if (dir == 3) // 북쪽
{
d[1] = cd[2];
d[2] = cd[6];
d[6] = cd[5];
d[5] = cd[1];
d[3] = cd[3];
d[4] = cd[4];
}
if (dir == 4) // 남쪽
{
d[1] = cd[5];
d[5] = cd[6];
d[6] = cd[2];
d[2] = cd[1];
d[3] = cd[3];
d[4] = cd[4];
}
if (map[ny][nx] == 0) // 이동한 칸에 쓰여 있는 수가 0이면
{
map[ny][nx] = d[6];//주사위의 바닥면에 쓰여 있는 수 복사
}
else // 0이 아닌 경우
{
d[6] = map[ny][nx]; // 칸에 쓰여 있는 수를 주사위 바닥면으로 복사
map[ny][nx] = 0; // 칸에 쓰여 있는 수 0으로
}
printf("%d\n", d[1]);
}
int main()
{
//freopen("input.txt", "r", stdin);
//setbuf(stdout, NULL);
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> sy >> sx >> k;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> map[i][j];
}
}
int temp;
for (int i = 0; i < k; i++)
{
cin >> temp;
dmove(temp); // 주사위 이동
}
return 0;
}
|
'Algorithm > boj' 카테고리의 다른 글
백준 2110번 공유기 설치 (1) | 2021.10.24 |
---|---|
백준 14890번 경사로 (0) | 2019.11.11 |
백준 14503 로봇 청소기 (0) | 2019.11.11 |
백준 15662번 톱니바퀴 (2) (0) | 2019.11.10 |
백준 14891번 톱니바퀴 (0) | 2019.11.10 |