[BOJ] 19238 : ์คํํธ ํ์
๋ฌธ์ ๋งํฌ
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<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, 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<int, int>> goal;
void BFS(pair<int, int> st);
bool is_ok(pair<int, int> next);
int Take_Client(pair<int, int> st, pair<int, int> 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<int, int> Taxi; //ํ์ ์์น
cin >> Taxi.first >> Taxi.second;
goal.push_back({ 0, 0 });
for (int i = 1; i <= M; i++) {
pair<int, int> position;
cin >> position.first >> position.second;
pair<int, int> 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<int, int> 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<int, int> st) {
queue<pair<pair<int, int>, int>> q;
q.push({ st,0 });
int Min_distance = INF;
memset(visited, false, sizeof(visited));
client.clear();
while (!q.empty()) {
pair<int, int> 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<int, int> next = { now.first + dy[i], now.second + dx[i] };
if (is_ok(next))
q.push({ next, distance + 1});
}
}
}
int Take_Client(pair<int, int> st, pair<int, int> ed) {
queue<pair<pair<int, int>, int>> q;
q.push({ st, 0 });
memset(visited, false, sizeof(visited));
while (!q.empty()) {
pair<int, int> 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<int, int> next = { now.first + dy[i], now.second + dx[i] };
if (is_ok(next))
q.push({ next, distance + 1 });
}
}
return -1;
}
bool is_ok(pair<int, int> 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 |