๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐฑ์ค€/Gold

[BOJ] 23288 : ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฌ๊ธฐ

by Jaeguk 2022. 6. 3.
๋ฌธ์ œ ๋งํฌ

23288๋ฒˆ: ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฌ๊ธฐ 2 (acmicpc.net)

 

23288๋ฒˆ: ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฌ๊ธฐ 2

ํฌ๊ธฐ๊ฐ€ N×M์ธ ์ง€๋„๊ฐ€ ์กด์žฌํ•œ๋‹ค. ์ง€๋„์˜ ์˜ค๋ฅธ์ชฝ์€ ๋™์ชฝ, ์œ„์ชฝ์€ ๋ถ์ชฝ์ด๋‹ค. ์ง€๋„์˜ ์ขŒํ‘œ๋Š” (r, c)๋กœ ๋‚˜ํƒ€๋‚ด๋ฉฐ, r๋Š” ๋ถ์ชฝ์œผ๋กœ๋ถ€ํ„ฐ ๋–จ์–ด์ง„ ์นธ์˜ ๊ฐœ์ˆ˜, c๋Š” ์„œ์ชฝ์œผ๋กœ๋ถ€ํ„ฐ ๋–จ์–ด์ง„ ์นธ์˜ ๊ฐœ์ˆ˜์ด๋‹ค. ๊ฐ€์žฅ ์™ผ

www.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<intint>vector<pair<intint>>, greater<pair<intint>>> 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<intint>);
void get_score(pair<intint>);
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<intint> now = { 1,1 };
    for (int i = 1; i <= K; i++){
        pair<intint> 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<intint> next) {
    if (next.first < 1 || next.first> N || next.second < 1 || next.second > M)
        return false;
    return true;
}
 
void get_score(pair<intint> now) {
    int B = Map[now.first][now.second];
    queue<pair<intint>> q;
    q.push(now);
    memset(visited, falsesizeof(visited));
    int C = 0;
    while (!q.empty()) {
        pair<intint> 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<intint> 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

์•ž์„œ ํ’€์—ˆ๋˜ ์ƒ์–ด ๋ฌธ์ œ๋“ค์— ๋น„ํ•˜๋ฉด ๊ทธ๋ ‡๊ฒŒ ๊ตฌํ˜„์ด ๋ณต์žกํ•˜์ง€๋Š” ์•Š์€ ๊ฒƒ ๊ฐ™๋‹ค.

728x90