1
- // Authored by : BaaaaaaaaaaarkingDog
2
- // Co-authored by : -
3
- // http://boj.kr/****************
1
+ // Authored by : HJPark
2
+ // Co-authored by : BaaaaaaaaaaarkingDog
3
+ // http://boj.kr/0b99159aaaba43379606d5aefce0b753
4
4
#include < bits/stdc++.h>
5
5
using namespace std ;
6
6
7
- int main (void ){
7
+ int n, m, k;
8
+ int board[22 ][22 ];
9
+ int dx[4 ] = {0 , 1 , 0 , -1 };
10
+ int dy[4 ] = {1 , 0 , -1 , 0 };
11
+ int dir = 0 ; // 동 남 서 북
12
+ int cur_r = 1 , cur_c = 1 ; // 현 위치
13
+ int dice[6 ] = {1 , 2 , 3 , 4 , 5 , 6 };
14
+ int ans = 0 ;
15
+
16
+ bool OOB (int r, int c) {
17
+ return r <= 0 || r > n || c <= 0 || c > m;
18
+ }
19
+
20
+ void next_dir () {
21
+ int dice_bottom = dice[5 ];
22
+ if (board[cur_r][cur_c] < dice_bottom)
23
+ dir = (dir + 1 ) % 4 ;
24
+ else if (board[cur_r][cur_c] > dice_bottom)
25
+ dir = (dir + 3 ) % 4 ;
26
+
27
+ if (OOB (cur_r + dx[dir], cur_c + dy[dir]))
28
+ dir = (dir + 2 ) % 4 ;
29
+ }
30
+
31
+ void get_score () {
32
+ int tile = 1 ;
33
+ bool vis[22 ][22 ] = {};
34
+ queue<pair<int , int >> q;
35
+ q.push ({cur_r, cur_c});
36
+ vis[cur_r][cur_c] = 1 ;
37
+
38
+ while (!q.empty ()) {
39
+ pair<int , int > pos = q.front ();
40
+ q.pop ();
41
+ for (int i = 0 ; i < 4 ; i++) {
42
+ int nxt_r = pos.first + dx[i];
43
+ int nxt_c = pos.second + dy[i];
44
+ if (!OOB (nxt_r, nxt_c) && !vis[nxt_r][nxt_c] && board[nxt_r][nxt_c] == board[cur_r][cur_c]) {
45
+ q.push ({nxt_r, nxt_c});
46
+ vis[nxt_r][nxt_c] = 1 ;
47
+ tile++;
48
+ }
49
+ }
50
+ }
51
+
52
+ ans += tile * board[cur_r][cur_c];
53
+ }
54
+
55
+ void move () {
56
+ int tmp[6 ];
57
+ copy (begin (dice), end (dice), begin (tmp));
58
+
59
+ if (dir == 0 ) { // 동
60
+ dice[0 ] = tmp[3 ];
61
+ dice[2 ] = tmp[0 ];
62
+ dice[5 ] = tmp[2 ];
63
+ dice[3 ] = tmp[5 ];
64
+ } else if (dir == 1 ) { // 남
65
+ dice[0 ] = tmp[1 ];
66
+ dice[4 ] = tmp[0 ];
67
+ dice[5 ] = tmp[4 ];
68
+ dice[1 ] = tmp[5 ];
69
+ } else if (dir == 2 ) { // 서
70
+ dice[0 ] = tmp[2 ];
71
+ dice[2 ] = tmp[5 ];
72
+ dice[5 ] = tmp[3 ];
73
+ dice[3 ] = tmp[0 ];
74
+ } else if (dir == 3 ) { // 북
75
+ dice[0 ] = tmp[4 ];
76
+ dice[4 ] = tmp[5 ];
77
+ dice[5 ] = tmp[1 ];
78
+ dice[1 ] = tmp[0 ];
79
+ }
80
+
81
+ cur_r += dx[dir];
82
+ cur_c += dy[dir];
83
+ }
84
+
85
+ int main (void ) {
8
86
ios::sync_with_stdio (0 );
9
87
cin.tie (0 );
10
-
11
- }
88
+
89
+ cin >> n >> m >> k;
90
+
91
+ for (int i = 1 ; i <= n; i++) {
92
+ for (int j = 1 ; j <= m; j++) {
93
+ cin >> board[i][j];
94
+ }
95
+ }
96
+
97
+ while (k--) {
98
+ move ();
99
+ get_score ();
100
+ next_dir ();
101
+ }
102
+
103
+ cout << ans;
104
+ }
105
+
106
+ /*
107
+ 현 위치와 주사위 전개도를 갱신한다.
108
+ queue를 이용한 bfs를 통해 같은 수를 가진 타일 수를 구한 뒤 점수를 계산한다.
109
+ 다음 이동 방향 갱신한다.
110
+ 만약 다음에 갱신된 방향으로 이동 시 지도의 범위를 벗어난다면, 이동 방향을 반대로 해 준다.
111
+ */
0 commit comments