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

[BOJ] 17825 : ์ฃผ์‚ฌ์œ„ ์œท๋†€์ด

by Jaeguk 2022. 5. 19.
๋ฌธ์ œ ๋งํฌ

17825๋ฒˆ: ์ฃผ์‚ฌ์œ„ ์œท๋†€์ด (acmicpc.net)

 

17825๋ฒˆ: ์ฃผ์‚ฌ์œ„ ์œท๋†€์ด

์ฃผ์‚ฌ์œ„ ์œท๋†€์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒŒ์ž„ํŒ์—์„œ ํ•˜๋Š” ๊ฒŒ์ž„์ด๋‹ค. ์ฒ˜์Œ์—๋Š” ์‹œ์ž‘ ์นธ์— ๋ง 4๊ฐœ๊ฐ€ ์žˆ๋‹ค. ๋ง์€ ๊ฒŒ์ž„ํŒ์— ๊ทธ๋ ค์ง„ ํ™”์‚ดํ‘œ์˜ ๋ฐฉํ–ฅ๋Œ€๋กœ๋งŒ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ง์ด ํŒŒ๋ž€์ƒ‰ ์นธ์—์„œ ์ด๋™์„ ์‹œ์ž‘ํ•˜๋ฉด

www.acmicpc.net

์ฃผ์‚ฌ์œ„๋กœ ์ง„ํ–‰๋˜๋Š” ์œท๋†€์ด๋ฅผ ๊ทœ์น™๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ.

ํ’€์ด

์™ผ์ชฝ๊ณผ ๊ฐ™์€ ์œท๋†€์ด ํŒ์„ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ• ์ง€๊ฐ€ ์ผ๋‹จ ์ฒซ๋ฒˆ์งธ ๋ฌธ์ œ์˜€๋‹ค. ํŒŒ๋ž€์ƒ‰ ์นธ์„ ๋ฐŸ์•˜์„ ๋•Œ ๊ฒฝ๋กœ๊ฐ€ ๋ฐ”๋€Œ๋Š” ๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ํ‘œํ˜„ํ•ด์•ผํ•˜์ง€ ์ƒ๊ฐํ•˜๋‹ค๊ฐ€, ์ฒ˜์Œ์—” 10, 20, 30 ์นธ์„ ๋ฐŸ์„๋•Œ๋งˆ๋‹ค ๊ฒฝ๋กœ๊ฐ€ ๋ฐ”๋€Œ๋‹ˆ๊นŒ 4๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ ๊ฒฝ๋กœ๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค๋ ค๊ณ  ํ–ˆ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒํ•˜๋ฉด ์ด๋™ํ•  ์นธ์— ์ด๋ฏธ ๋ง์ด์žˆ๋Š”์ง€ ํŒŒ์•…ํ•˜๊ธฐ๊ฐ€ ํž˜๋“ค์—ˆ๋‹ค.

๊ทธ๋ž˜์„œ ๋‚˜๋Š” ๋‚ด ์ž„์˜๋Œ€๋กœ ๊ฐ ์นธ๋งˆ๋‹ค ๋ฒˆํ˜ธ๋ฅผ ๋ถ€์—ฌํ–ˆ๋‹ค.

 

 

 

์ด๋ ‡๊ฒŒ ๊ฐ ์นธ๋งˆ๋‹ค ๋‚ด ๋งˆ์Œ๋Œ€๋กœ ๋ฒˆํ˜ธ๋ฅผ ๋ถ€์—ฌํ•˜๊ณ 

๋‹ค์Œ ์นธ์˜ ๋ฒˆํ˜ธ๋ฅผ Next๋ผ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ๋ฏธ๋ฆฌ ์ •ํ•ด๋‘์—ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด 31๋ฒˆ์นธ ๋‹ค์Œ์นธ์€ 24๋ฒˆ์นธ์ธ๋ฐ ์ด๋Ÿฐ ๊ฒฝ์šฐ๋Š” ์ด๋™์ด ๋ถˆ๊ทœ์น™์ ์ด๊ธฐ ๋•Œ๋ฌธ์— 31๋ฒˆ ๋‹ค์Œ์นธ์€ 24๋ฒˆ์ด๋ผ๊ณ  ๋ฏธ๋ฆฌ ๊ฒฝ๋กœ๋ฅผ ์ •ํ•ด์„œ ๊ตฌํ˜„ํ–ˆ๋‹ค.

๋งŒ์•ฝ ์ด๋™ํ•˜๋ คํ•  ๋•Œ ์‹œ์ž‘์นธ์ด 5๋ฒˆ, 10๋ฒˆ, 30๋ฒˆ์ด๋ฉด ๋‹ค์Œ์นธ์ด ๊ฐ 21, 27, 29๋ฒˆ์นธ์ด ๋˜๋„๋ก ํ–ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๊ฒŒ๋˜๋ฉด ๊ฒฝ๋กœ๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๊ณ , ์ฝ”๋“œ๊ฐ€ ์กฐ๊ธˆ ์ง€์ €๋ถ„ํ•ด์ง„๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฒƒ๋ง๊ณ  ๋‹ค๋ฅด๊ฒŒ ๊ตฌํ˜„ํ•  ๋ฐฉ๋ฒ•์ด ์ƒ๊ฐ์ด ์•ˆ๋‚˜์„œ ์ด๋ ‡๊ฒŒ ํ–ˆ๋‹ค.

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
#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 = 30 + 5;
int N, M, T;
int Score[MAX_N];
int Next[MAX_N];
int horse[4];
int Turn[11];
int ans = -1;
 
void Init();
int Move_horse(int horse_num, int moving);
bool is_ok(int next);
void backtracking(int depth, int total_Score);
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    Init();
    for (int i = 0; i < 10; i++)
        cin >> Turn[i];
    backtracking(00);
    cout << ans << "\n";
    return 0;
}
 
void Init() {
    for (int i = 1; i <= 20; i++)
        Score[i] = 2 * i;
    Score[27= 22;
    Score[28= 24;
    Score[21= 13;
    Score[22= 16;
    Score[23= 19;
    for (int i = 0; i <= 2; i++)
        Score[24 + i] = 25 + 5 * i;
    for (int i = 0; i <= 2; i++)
        Score[29 + i] = 28 - i;
    for (int i = 0; i <= 31; i++)
        Next[i] = i + 1;
    Next[20= INF;
    Next[31= Next[28= 24;
    Next[26= 20;
}
 
int Move_horse(int horse_num, int moving) {
    int now = horse[horse_num];
    if (now == 5) {
        now = 21;
        moving--;
    }
    else if (now == 10) {
        now = 27;
        moving--;
    }
    else if (now == 15) {
        now = 29;
        moving--;
    }
 
    while (moving--) {
        now = Next[now];
        if (now == INF) {
            now = -1;
            break;
        }
    }
    return now;
}
 
bool is_ok(int next) {
    for (int i = 0; i < 4; i++)
        if (horse[i] != -1 && horse[i] == next)
            return false;
    return true;
}
 
void backtracking(int depth, int total_Score) {
    if (depth == 10) {
        ans = max(ans, total_Score);
        return;
    }
    for (int i = 0; i < 4; i++) {
        if (horse[i] == -1)
            continue;
        int next = Move_horse(i, Turn[depth]);
        if (is_ok(next)) {
            int temp = horse[i];
            horse[i] = next;
            if (next == -1)
                backtracking(depth + 1, total_Score);
            else backtracking(depth + 1, total_Score + Score[next]);
            horse[i] = temp;
        }
    }
}
cs

์ด๋™์ด ๋๋‚œ ๋ง์€ ์œ„์น˜๋ฅผ -1๋กœ ์ง€์ •ํ•ด์„œ ๋„์ฐฉํ–ˆ์Œ์„ ํ‘œํ˜„ํ–ˆ๋‹ค.

๋ง์˜ ์œ„์น˜๊ฐ€ -1์ธ ๋ง์€ ๋”์ด์ƒ ์ด๋™์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

728x90