๋ฌธ์ ๋งํฌ
23288๋ฒ: ์ฃผ์ฌ์ ๊ตด๋ฆฌ๊ธฐ 2 (acmicpc.net)
์ง๋์์ ๋์ธ ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆฌ๋ฉฐ ์ ์๋ฅผ ํ๋ํ๋ ๊ฒ์.
ํ์ด
N x M ํฌ๊ธฐ์ ์ง๋์ (1,1) ์ขํ์ ์ฃผ์ฌ์๊ฐ ๋์ฌ์ ธ์๋ค.
2
4 1 3
5
6
์ฃผ์ฌ์์ ์ด๊ธฐ ์ํ ์ ๊ฐ๋๋ ์์ ๊ฐ๋ค. ์๋ฉด์ด 1์ด๊ณ , ์๋ซ๋ฉด์ด 6 ์ค๋ฅธ์ชฝ๋ฉด์ด 3์ด๋ค.
1. ์ฃผ์ฌ์๊ฐ ์ด๋ ๋ฐฉํฅ์ผ๋ก ํ ์นธ ๊ตด๋ฌ๊ฐ๋ค. ๋ง์ฝ, ์ด๋ ๋ฐฉํฅ์ ์นธ์ด ์๋ค๋ฉด, ์ด๋ ๋ฐฉํฅ์ ๋ฐ๋๋ก ํ ๋ค์ ํ ์นธ ๊ตด๋ฌ๊ฐ๋ค.
2. ์ฃผ์ฌ์๊ฐ ๋์ฐฉํ ์นธ (x, y)์ ๋ํ ์ ์๋ฅผ ํ๋ํ๋ค.
3. ์ฃผ์ฌ์์ ์๋ซ๋ฉด์ ์๋ ์ ์ A์ ์ฃผ์ฌ์๊ฐ ์๋ ์นธ (x, y)์ ์๋ ์ ์ B๋ฅผ ๋น๊ตํด ์ด๋ ๋ฐฉํฅ์ ๊ฒฐ์ ํ๋ค.
ใ
A > B์ธ ๊ฒฝ์ฐ ์ด๋ ๋ฐฉํฅ์ 90๋ ์๊ณ ๋ฐฉํฅ์ผ๋ก ํ์ ์ํจ๋ค.
ใ
A < B์ธ ๊ฒฝ์ฐ ์ด๋ ๋ฐฉํฅ์ 90๋ ๋ฐ์๊ณ ๋ฐฉํฅ์ผ๋ก ํ์ ์ํจ๋ค.
ใ
A = B์ธ ๊ฒฝ์ฐ ์ด๋ ๋ฐฉํฅ์ ๋ณํ๋ ์๋ค.
์ฃผ์ฌ์๋ ๋ค์๊ณผ ๊ฐ์ ๊ท์น์ผ๋ก ์ด๋ํ๊ณ ์ ์๋ฅผ ํ๋ํ๋ค.
๋จผ์ ์ฐ๋ฆฌ๊ฐ ๊ฐ์ง๊ณ ์์ด์ผ ํ ์ ๋ณด๋ ์ด๋ค ๊ฒ๋ค์ด ์์์ง ์๊ฐํด๋ณด์.
1. ์ง๋
์ง๋์ ๊ฐ ์นธ์ด ์ด๋ค ์ซ์๊ฐ ์๋์ง ์์์ผํ๋ค. Map[20][20] ์ int ๋ฐฐ์ด์ ๋ง๋ค์ด์ฃผ์๊ณ ๊ฐ์ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ค.
2. ์ฃผ์ฌ์์ ์ํ
ํ์ฌ ์ฃผ์ฌ์ ์ ๊ฐ๋์ ์ํ๋ฅผ ์ ์ฅํ๊ณ ์์ด์ผํ๋ค.
๋๋ dice[6] ๋ผ๋ 1์ฐจ์ ๋ฐฐ์ด์ ๋ง๋ค์ด์ ์ฃผ์ฌ์์ ์ ๊ฐ๋๋ฅผ ํํํ๋ค.
0๋ถํฐ 5๋ฒ ์ธ๋ฑ์ค๊น์ง ์์๋๋ก ์, ๋ค, ์ค, ์ผ, ์, ์๋ ์์๋๋ก ์ง์ ํ๋ค.
๊ทธ๋ฆฌ๊ณ ์ฃผ์ด์ง ๊ฐ์ ๋ฐ๋ผ ์์๋๋ก 1, 2, 3, 4, 5, 6 ์ผ๋ก ์ด๊ธฐ๊ฐ์ ๋ฃ์ด์คฌ๋ค.
์ฌ์ค, ์์๋ ๊ฐ์ ์ ํ๊ธฐ ๋๋ฆ์ด๋ค.
3. ๋ฐฉํฅ
๋ฐฉํฅ๋ ์ฌ์ค ์์ ์๊ด์์ด ๋์๋จ๋ถ๋ง ํํํ๊ธฐ๋ง ํ๋ฉด ๋๋ค.
ํ์ง๋ง ์ฃผ์ฌ์์ ์ด๋ ๋ฐฉํฅ์ด 90๋๋ก ์๊ณ ๋๋ ๋ฐ์๊ณ๋ฐฉํฅ์ผ๋ก ํ์ ํ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ dy[], dx[] ๋ฐฐ์ด์ ๋ง๋ค ๋, ๋->๋จ->์->๋ถ ์์๋๋ก ๋ฐฉํฅ์ ์ ํ๋ฉด ๋ฐฉํฅ ์ธ๋ฑ์ค๋ฅผ ํ๋๋ฅผ ๋ํ๊ฑฐ๋ ๋นผ์ ์๊ณ ๋๋ ๋ฐ์๊ณ ๋ฐฉํฅ์ผ๋ก ํ์ ์ํค๊ธฐ ์ฝ๋ค.
์ผ๋จ ๊ธฐ๋ณธ์ ์ผ๋ก ํ์ํ ์ ๋ณด๋ ์ด ์ ๋๊ฐ ๋ ๊ฒ ๊ฐ๋ค.
- ์ฃผ์ฌ์ ๋ฐฉํฅ ์ ํ
1๋ฒ ๊ณผ์ ์ด๋ 3๋ฒ ๊ณผ์ ์์ ์ฃผ์ฌ์์ ๋ฐฉํฅ์ด ์ ํ๋๋ค.
1๋ฒ์์ ์ฃผ์ฌ์๊ฐ ๋ฒ์๋ฅผ ๋ฒ์ด๋๊ฒ ๋๋ฉด ๋ฐฉํฅ์ด ๋ฐ๋๋ก ๋ฐ๋๊ฒ ๋๋๋ฐ
๋๋ 0๋ฒ = ๋, 1๋ฒ = ๋จ, 2๋ฒ = ์, 3๋ฒ = ๋ถ ์ผ๋ก ์ ํด๋์๊ธฐ ๋๋ฌธ์ ์๋ก ๋ฐ๋๋ฐฉํฅ์ธ ๋,์์ ๋จ,๋ถ์ ๋ฒํธ์ 2๊ฐ๊ฐ ์ฐจ์ด๋๋ ๊ฒ์ ์ ์ ์๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ๋ฐฉํฅ์ ๋ฐ๋๋ก ๋ฐ๊พธ๊ธฐ ์ํด์๋ ํ์ฌ ๋ฐฉํฅ์ + 2๋ฅผ ํด์ค๋ค์์ 3์ ๋์ด๊ฐ๋ ๊ฒฝ์ฐ๋ ๋ชจ๋๋ฌ๋ฅผ ์ด์ฉํด์ %4 ํด์ฃผ๋ฉด ๋๋ค.
๋ 3๋ฒ ๊ณผ์ ์์๋ ์๊ณ ๋๋ ๋ฐ์๊ณ ๋ฐฉํฅ์ผ๋ก ํ์ ํ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค.
์๊ณ ๋ฐฉํฅ์ผ๋ก ํ์ ํ ๋๋ ๋ฐฉํฅ ์ธ๋ฑ์ค๋ฅผ + 1 ํด์ฃผ๊ณ , ๋ฐ์๊ณ ๋ฐฉํฅ์ผ๋ก ํ์ ํ ๋๋ ๋ฐฉํฅ ์ธ๋ฑ์ค๋ฅผ - 1 ํด์ฃผ๋ฉด ๋๋ค.
์ด๋ ์ญ์ ๋ฐฉํฅ ์ธ๋ฑ์ค๊ฐ ๋ฒ์ 0~3์ ๋ฒ์ด๋๋ ๊ฒฝ์ฐ์๋ ๊ฐ์ ์กฐ์ ํด์ค๋ค.
์ด๊ฒ์ด ๋ด๊ฐ ๋ฐฉํฅ์ ๋๋จ์๋ถ ์์ผ๋ก ์ ํด์ค ์ด์ ์ด๋ค.
- ์ ์ ํ๋
์ ์๋ ํ์ฌ ์ฃผ์ฌ์๊ฐ ์๋ ์นธ์ ์ซ์์ธ B์ ์ฃผ์ฌ์๊ฐ ํ์ฌ ์นธ๊ณผ ๊ฐ์ ์ซ์๋ง ๋ฐ์ผ๋ฉด์ ์ด๋ํ ์ ์๋ ์ต๋ ์นธ ์์ธ C๋ฅผ ๊ณฑํ ๋งํผ ์ ์๋ฅผ ํ๋ํ๊ฒ ๋๋ค.
B ๊ฐ์ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง ์ ๋ณด์ด๋ฏ๋ก ๋ฐ๋ก ๊ตฌํ ์ ์๋๋ฐ, C๋ BFS๋ฅผ ์ด์ฉํด์ ์ง์ ๊ตฌํด์ค์ผํ๋ค.
์ฃผ์ฌ์๊ฐ ์ํ์ข์ฐ ์ธ์ ํ ์นธ ์ค์ ๊ฐ์ ์ซ์๊ฐ ์๋ ์นธ์ผ๋ก๋ง ์ด๋ํด์ ์ต๋ ๋ช ์นธ์ ์ด๋ํ ์ ์๋ ์ง ๊ตฌํด์ฃผ๋ฉด ๋๋ค.
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <deque>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 20 + 5;
int N, M, K;
int dx[] = { 1,0,-1,0 };
int dy[] = { 0,1,0,-1 };
int Map[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
int dice[6]; // ์, ๋ค, ์ฐ, ์ข, ์, ์๋
int Score;
void Input();
void Solve();
bool is_ok(pair<int, int>);
void get_score(pair<int, int>);
void roll_dice(int);
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Input();
Solve();
cout << Score << "\n";
return 0;
}
void Input() {
cin >> N >> M >> K;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++)
cin >> Map[i][j];
for (int i = 0; i < 6; i++)
dice[i] = i + 1;
}
// 0, 1, 2, 3 ์์๋๋ก ๋๋จ์๋ถ
void Solve() {
int direction = 0;
pair<int, int> now = { 1,1 };
for (int i = 1; i <= K; i++){
pair<int, int> next = { now.first + dy[direction], now.second + dx[direction] };
if (!is_ok(next)) {
direction += 2;
direction %= 4;
}
roll_dice(direction);
next = { now.first + dy[direction], now.second + dx[direction] };
now = next;
get_score(now);
int A = dice[5];
int B = Map[now.first][now.second];
if (A > B)
direction ++;
else if (A < B)
direction += 3;
direction %= 4;
}
}
bool is_ok(pair<int, int> next) {
if (next.first < 1 || next.first> N || next.second < 1 || next.second > M)
return false;
return true;
}
void get_score(pair<int, int> now) {
int B = Map[now.first][now.second];
queue<pair<int, int>> q;
q.push(now);
memset(visited, false, sizeof(visited));
int C = 0;
while (!q.empty()) {
pair<int, int> position = q.front();
q.pop();
if (visited[position.first][position.second])
continue;
C++;
visited[position.first][position.second] = true;
for (int i = 0; i < 4; i++) {
pair<int, int> next = { position.first + dy[i], position.second + dx[i] };
if (is_ok(next) && Map[next.first][next.second] == B)
q.push(next);
}
}
Score += B * C;
}
void roll_dice(int direction) {
// ์, ๋ค, ์ค, ์ผ, ์, ์๋
int temp[6];
for (int i = 0; i < 6; i++)
temp[i] = dice[i];
if (direction == 0) {
//๋์ชฝ์ผ๋ก ๊ตด๋ฌ๊ฐ
dice[0] = temp[3];
dice[2] = temp[0];
dice[3] = temp[5];
dice[5] = temp[2];
}
else if (direction == 1) {
//๋จ์ชฝ์ผ๋ก ๊ตฌ๋ฆ
dice[0] = temp[1];
dice[4] = temp[0];
dice[5] = temp[4];
dice[1] = temp[5];
}
else if (direction == 2) {
//์์ชฝ์ผ๋ก ๊ตฌ๋ฆ
dice[0] = temp[2];
dice[3] = temp[0];
dice[5] = temp[3];
dice[2] = temp[5];
}
else if (direction == 3) {
//๋ถ์ชฝ์ผ๋ก ๊ตฌ๋ฆ
dice[0] = temp[4];
dice[1] = temp[0];
dice[5] = temp[1];
dice[4] = temp[5];
}
}
|
cs |
์์ ํ์๋ ์์ด ๋ฌธ์ ๋ค์ ๋นํ๋ฉด ๊ทธ๋ ๊ฒ ๊ตฌํ์ด ๋ณต์กํ์ง๋ ์์ ๊ฒ ๊ฐ๋ค.
'๋ฐฑ์ค > Gold' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 21611 : ๋ง๋ฒ์ฌ ์์ด์ ๋ธ๋ฆฌ์๋ (0) | 2022.06.03 |
---|---|
[BOJ] 21610 : ๋ง๋ฒ์ฌ ์์ด์ ๋น๋ฐ๋ผ๊ธฐ (0) | 2022.06.02 |
[BOJ] 21609 : ์์ด ์คํ๊ต (0) | 2022.06.02 |
[BOJ] 21608 : ์์ด ์ด๋ฑํ๊ต (0) | 2022.05.31 |
[BOJ] 20058 : ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด์คํฐ (0) | 2022.05.31 |