본문 바로가기

시뮬레이션과 구현

주사위 굴리기 2

https://www.acmicpc.net/problem/23288

 

23288번: 주사위 굴리기 2

크기가 N×M인 지도가 존재한다. 지도의 오른쪽은 동쪽, 위쪽은 북쪽이다. 지도의 좌표는 (r, c)로 나타내며, r는 북쪽으로부터 떨어진 칸의 개수, c는 서쪽으로부터 떨어진 칸의 개수이다. 가장 왼

www.acmicpc.net

 

칸 (x, y)에 대한 점수는 다음과 같이 구할 수 있다.

(x, y)에 있는 정수를 B라고 했을때, (x, y)에서 

동서남북 방향으로 연속해서 이동할 수 있는 칸의 수 C를 모두 구한다.

이때 이동할 수 있는 칸에는 모두 정수 B가 있어야 한다. 

 

BFS로 하면 되겠다

 

주사위 구현

주사위를 그려보자.

 

<소스 코드>

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
int dx[] = { -110 ,0 };
int dy[] = { 0,0,-11 };
 
int n, m, k;
pair<intint> now;
// 처음엔 동쪽을 보고있다
int dir = 1;
 
int dice[7= { 0123456 };
int c_dice[7];
int map[25][25];
bool visited[25][25];
 
/* 동: 1 서: 2 북: 3 남: 4*/
int judge(int d)
{
    if (d == 1)
    {
        if (now.second == m)
            return 2;
        else
            return d;
    }
    else if (d == 2)
    {
        if (now.second == 1)
            return 1;
        else
            return d;
    }
    else if (d == 3)
    {
        if (now.first == 1)
            return 4;
        else
            return d;
    }
    else if (d == 4)
    {
        if (now.first == n)
            return 3;
        else
            return d;
    }
 
}
 
void movement(int d)
{
    // c_dice에 dice 복사
    memcpy(c_dice, dice, sizeof(dice));
    // 동
    if (d == 1)
    {
        now.second++;
        dice[1= c_dice[4];
        dice[3= c_dice[1];
        dice[4= c_dice[6];
        dice[6= c_dice[3];
    }
    // 서
    else if (d == 2)
    {
        now.second--;
        dice[1= c_dice[3];
        dice[3= c_dice[6];
        dice[4= c_dice[1];
        dice[6= c_dice[4];
    }
    // 북
    else if (d == 3)
    {
        now.first--;
        dice[1= c_dice[5];
        dice[2= c_dice[1];
        dice[5= c_dice[6];
        dice[6= c_dice[2];
    }
    // 남
    else if (d == 4)
    {
        now.first++;
        dice[1= c_dice[2];
        dice[2= c_dice[6];
        dice[5= c_dice[1];
        dice[6= c_dice[5];
    }
}
 
/* 동: 1 서: 2 북: 3 남: 4*/
void dir_change()
{
    // 시계 방향
    if (dice[6> map[now.first][now.second])
    {
        if (dir == 1)
            dir = 4;
        else if (dir == 2)
            dir = 3;
        else if (dir == 3)
            dir = 1;
        else if (dir == 4)
            dir = 2;
    }
    // 반시계 방향
    else if (dice[6< map[now.first][now.second])
    {
        if (dir == 1)
            dir = 3;
        else if (dir == 2)
            dir = 4;
        else if (dir == 3)
            dir = 2;
        else if (dir == 4)
            dir = 1;
    }
    else
    {
        return;
    }
}
 
int bfs(int r, int c)
{
    memset(visited, falsesizeof(visited));
    queue<pair<intint>> q;
    q.push({ r, c });
    visited[r][c] = true;
 
    int gaesu = 1;
 
    while (!q.empty())
    {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
 
        for (int k = 0; k < 4; k++)
        {
            int nx = x + dx[k];
            int ny = y + dy[k];
            if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && !visited[nx][ny])
            {
                if (map[nx][ny] == map[r][c])
                {
                    visited[nx][ny] = true;
                    gaesu++;
                    q.push({ nx, ny });
                }
            }
        }
    }
 
    return gaesu;
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> n >> m >> k;
 
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> map[i][j];
        }
    }
 
    int ans = 0;
    now = { 11 };
 
 
    for (int i = 0; i < k; i++)
    {
        dir = judge(dir);
        movement(dir);
        ans += (map[now.first][now.second] * bfs(now.first, now.second));
        dir_change();
    }
 
    cout << ans << '\n';
    return 0;
}
cs

'시뮬레이션과 구현' 카테고리의 다른 글

어른 상어  (0) 2023.10.06
마법사 상어와 토네이도  (0) 2023.10.03
나무 재테크  (0) 2023.09.30
미세먼지 안녕!  (0) 2023.09.27
이차원 배열과 연산  (0) 2023.09.26