๋ฐฑ์ค€/Gold

[BOJ] 19238 : ์Šคํƒ€ํŠธ ํƒ์‹œ

Jaeguk 2022. 5. 23. 15:26
๋ฌธ์ œ ๋งํฌ

19238๋ฒˆ: ์Šคํƒ€ํŠธ ํƒ์‹œ (acmicpc.net)

 

19238๋ฒˆ: ์Šคํƒ€ํŠธ ํƒ์‹œ

์ฒซ ์ค„์— N, M, ๊ทธ๋ฆฌ๊ณ  ์ดˆ๊ธฐ ์—ฐ๋ฃŒ์˜ ์–‘์ด ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 20, 1 ≤ M ≤ N2, 1 ≤ ์ดˆ๊ธฐ ์—ฐ๋ฃŒ ≤ 500,000) ์—ฐ๋ฃŒ๋Š” ๋ฌดํ•œํžˆ ๋งŽ์ด ๋‹ด์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ดˆ๊ธฐ ์—ฐ๋ฃŒ์˜ ์–‘์„ ๋„˜์–ด์„œ ์ถฉ์ „๋  ์ˆ˜๋„ ์žˆ๋‹ค. ๋‹ค

www.acmicpc.net

๋ชจ๋“  ์†๋‹˜์„ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋กœ ํƒœ์šฐ๊ณ  ์‹ถ์–ดํ•˜๋Š” ๋ฐฑ์ค€์ด๋ฅผ ๋„์™€์ฃผ์ž.

 

ํ’€์ด

๋ฐฑ์ค€์ด๊ฐ€ ์šด์ „ํ•˜๋Š” ํƒ์‹œ๋Š” ํ˜„์žฌ ์œ„์น˜์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์†๋‹˜์„ ํƒœ์šฐ๊ณ , ๊ทธ ์†๋‹˜์„ ๋ชฉ์ ์ง€๋กœ ๋ฐ๋ ค๋‹ค์ค€๋‹ค.

๋งŒ์•ฝ ์—ฐ๋ฃŒ๊ฐ€ ๋ถ€์กฑํ•ด์„œ ์†๋‹˜์„ ํƒœ์šฐ์ง€ ๋ชปํ•˜๊ฑฐ๋‚˜, ์†๋‹˜์„ ํƒœ์šฐ๊ณ  ๋ชฉ์ ์ง€๊นŒ์ง€ ๊ฐ€์ง€ ๋ชปํ•œ๋‹ค๋ฉด -1์„ ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

์—ฐ๋ฃŒ๊ฐ€ ์ถฉ๋ถ„ํ•ด์„œ ์†๋‹˜์„ ํƒœ์›Œ์„œ ๋ชฉ์ ์ง€์— ๋ฐ๋ ค๋‹ค ์ค„ ์ˆ˜ ์žˆ๋‹ค๋ฉด, ์†๋‹˜์˜ ์œ„์น˜์—์„œ ๋ชฉ์ ์ง€๊นŒ์ง€ ์‚ฌ์šฉํ•œ ์—ฐ๋ฃŒ x 2 ๋งŒํผ์˜ ์—ฐ๋ฃŒ๊ฐ€ ์ถฉ์ „๋œ๋‹ค.

๋ชจ๋“  ์†๋‹˜์„ ๋‹ค ํƒœ์›Œ๋‹ค ์ค„ ์ˆ˜ ์žˆ๋‹ค๋ฉด, ๋‹ค ํƒœ์šด ํ›„ ๋‚จ์€ ์—ฐ๋ฃŒ์˜ ์–‘์„ ์ถœ๋ ฅํ•˜๊ณ  ๊ทธ๋Ÿด ์ˆ˜ ์—†๋‹ค๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

  1. ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์†๋‹˜ ์ฐพ๊ธฐ

๋ฐฑ์ค€์ด ํƒœ์šธ ์Šน๊ฐ์„ ๊ณ ๋ฅผ ๋•Œ๋Š” ํ˜„์žฌ ์œ„์น˜์—์„œ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์งง์€ ์Šน๊ฐ์„ ๊ณ ๋ฅธ๋‹ค.
๊ทธ๋Ÿฐ ์Šน๊ฐ์ด ์—ฌ๋Ÿฌ ๋ช…์ด๋ฉด ๊ทธ์ค‘ ํ–‰ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์Šน๊ฐ์„, 
๊ทธ๋Ÿฐ ์Šน๊ฐ๋„ ์—ฌ๋Ÿฌ ๋ช…์ด๋ฉด ๊ทธ์ค‘ ์—ด ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์Šน๊ฐ์„ ๊ณ ๋ฅธ๋‹ค.

๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์†๋‹˜์ด 1๋ช…์ด๋ผ๋ฉด ๋‹คํ–‰์ด์ง€๋งŒ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™์€ ์†๋‹˜์ด ์—ฌ๋Ÿฌ๋ช… ์žˆ๋‹ค๋ฉด ์œ„์˜ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์†๋‹˜์˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ •ํ•œ๋‹ค.

BFS๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ฑฐ๋ฆฌ์— ์žˆ๋Š” ์†๋‹˜์„ ํƒ์ƒ‰ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์†๋‹˜๋“ค์˜ ์œ„์น˜๋ฅผ ๋ฒกํ„ฐ์— ์ €์žฅํ•œ๋‹ค. ํƒ์ƒ‰์„ ๋งˆ์นœ ํ›„์— ๋ฒกํ„ฐ์— ๊ฐ’์ด ํ•˜๋‚˜๋งŒ ๋“ค์–ด์žˆ๋‹ค๋ฉด ์ตœ๋‹จ๊ฑฐ๋ฆฌ์˜ ์†๋‹˜์ด 1๋ช… ๋ฟ์ด๋ผ๋Š” ๋œป์ด๋ฏ€๋กœ ๊ทธ ์†๋‹˜์„ ํƒœ์šด๋‹ค. ๋ฒกํ„ฐ์— ๊ฐ’์ด ์—ฌ๋Ÿฌ๊ฐœ ๋“ค์–ด์žˆ๋‹ค๋ฉด ์ตœ๋‹จ๊ฑฐ๋ฆฌ์˜ ์†๋‹˜์ด ์—ฌ๋Ÿฌ๋ช…์ด๋ผ๋Š” ๋œป์ด๋ฏ€๋กœ ์ด ๋ฒกํ„ฐ๋ฅผ ์ •๋ ฌํ•ด ์ค€ ํ›„ ๊ฐ€์žฅ ์•ž์—์žˆ๋Š” ์†๋‹˜์„ ํƒœ์šด๋‹ค.

pair<int,int>์˜ ๋ฒกํ„ฐ๋ฅผ ์ •๋ ฌํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ’์ด ์ž‘์€ ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋งจ ์•ž์— ์žˆ๋Š” ์†๋‹˜์ด ํ–‰๊ณผ ์—ด์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์†๋‹˜์ด๋‹ค.

 2. ์†๋‹˜ ํƒœ์›Œ๋‹ค๋“œ๋ฆฌ๊ธฐ

์†๋‹˜๋งˆ๋‹ค ๋ชฉ์ ์ง€๊ฐ€ ๋‹ค๋ฅด๊ธฐ๋•Œ๋ฌธ์— ์†๋‹˜ ๋ฒˆํ˜ธ๋ณ„๋กœ ์ž˜ ๊ตฌ๋ถ„ํ•ด์„œ ์†๋‹˜์˜ ๋ชฉ์ ์ง€๋ฅผ ๋ฐ›์•„๋‘์–ด์•ผํ•œ๋‹ค.

์ด๋ฒˆ์—๋Š” ๋ชฉ์ ์ง€๊ฐ€ ์ •ํ•ด์ ธ์žˆ๋Š” BFS๋ฅผ ํ†ตํ•ด์„œ ์†๋‹˜์„ ๋ชฉ์ ์ง€๊นŒ์ง€ ๋ฐ๋ ค๋‹ค์ฃผ๋Š”๋ฐ ํ•„์š”ํ•œ ์ตœ์†Œ ์—ฐ๋ฃŒ๋ฅผ ์ฐพ๋Š”๋‹ค.

ํ˜„์žฌ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—ฐ๋ฃŒ๋ณด๋‹ค ํ•„์š”ํ•œ ์—ฐ๋ฃŒ๊ฐ€ ๋” ๋งŽ์œผ๋ฉด ์†๋‹˜์„ ๋ชป๋ฐ๋ ค๋‹ค ์ฃผ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ -1์„ ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด, ํ˜„์žฌ ์—ฐ๋ฃŒ - ํ•„์š”ํ•œ ์—ฐ๋ฃŒ + ํ•„์š”ํ•œ ์—ฐ๋ฃŒ * 2 ๋งŒํผ์˜ ์—ฐ๋ฃŒ๊ฐ€ ๋‚จ๊ฒŒ ๋˜๋ฏ€๋กœ

์ฆ‰ ํ˜„์žฌ ์—ฐ๋ฃŒ + ํ•„์š”ํ•œ ์—ฐ๋ฃŒ ๋งŒํผ ์ถฉ์ „ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

์ด๋ ‡๊ฒŒ M๋ช…์˜ ์†๋‹˜์„ ํƒœ์›Œ๋‹ค ๋“œ๋ฆฐ ํ›„ ๋‚จ์€ ์—ฐ๋ฃŒ๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

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
#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, Fuel;
int Map[MAX_N][MAX_N];
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
int Client[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
vector<pair<pair<int,int>int>> client;
vector<pair<intint>> goal;
 
void BFS(pair<intint> st);
bool is_ok(pair<intint> next);
int Take_Client(pair<intint> st, pair<intint> ed);
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> M >> Fuel;
    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= N; j++)
            cin >> Map[i][j];
 
    pair<intint> Taxi; //ํƒ์‹œ ์œ„์น˜
    cin >> Taxi.first >> Taxi.second;
    goal.push_back({ 00 });
 
    for (int i = 1; i <= M; i++) {
        pair<intint> position;
        cin >> position.first >> position.second;
        pair<intint> Goal;
        cin >> Goal.first >> Goal.second;
        Client[position.first][position.second] = i;
        goal.push_back(Goal);
    }
 
    
    for (int i = 1; i <= M; i++) {
        BFS(Taxi);
        if (client.empty()) {
            cout << "-1\n";
            return 0;
        }
        sort(client.begin(), client.end());
        int Next_Client = Client[client[0].first.first][client[0].first.second];
        Fuel -= client[0].second;
        Taxi = { client[0].first.first, client[0].first.second };
        pair<intint> Goal = goal[Next_Client]; //ํ˜„์žฌ ์†๋‹˜์˜ ๋ชฉ์ ์ง€
        int Require_Fuel = Take_Client(Taxi, Goal);
        if (Require_Fuel == -1) {
            cout << "-1\n";
            return 0;
        }
        if (Fuel >= Require_Fuel) {
            Taxi = Goal;
            Client[client[0].first.first][client[0].first.second] = 0;
            Fuel += Require_Fuel;
            continue;
        }
        else {
            cout << "-1\n";
            return 0;
        }
    }
    cout << Fuel << "\n";
    return 0;
}
 
void BFS(pair<intint> st) {
    queue<pair<pair<intint>int>> q;
    q.push({ st,0 });
    int Min_distance = INF;
    memset(visited, falsesizeof(visited));
    client.clear();
    while (!q.empty()) {
        pair<intint> now = q.front().first;
        int distance = q.front().second;
        q.pop();
        if (visited[now.first][now.second])
            continue;
        visited[now.first][now.second] = true;
        if (Min_distance < distance)
            continue;
        if (Client[now.first][now.second]) {
            if (Min_distance > distance) {
                Min_distance = distance;
                client.clear();
                client.push_back({now,distance});
            }
            else if (Min_distance == distance) {
                client.push_back({ now,distance });
            }
            continue;
        }
        for (int i = 0; i < 4; i++) {
            pair<intint> next = { now.first + dy[i], now.second + dx[i] };
            if (is_ok(next))
                q.push({ next, distance + 1});
        }
    }
}
 
int Take_Client(pair<intint> st, pair<intint> ed) {
    queue<pair<pair<intint>int>> q;
    q.push({ st, 0 });
    memset(visited, falsesizeof(visited));
    while (!q.empty()) {
        pair<intint> now = q.front().first;
        int distance = q.front().second;
        q.pop();
        if (visited[now.first][now.second])
            continue;
        visited[now.first][now.second] = true;
        if (now == ed)
            return distance;
        for (int i = 0; i < 4; i++) {
            pair<intint> next = { now.first + dy[i], now.second + dx[i] };
            if (is_ok(next))
                q.push({ next, distance + 1 });
        }
    }
    return -1;
}
 
bool is_ok(pair<intint> next) {
    if (next.first < 1 || next.first > N || next.second < 1 || next.second > N
        || visited[next.first][next.second] || Map[next.first][next.second])
        return false;
    return true;
}
cs

728x90