[C++] BOJ 19238 - ์คํํธ ํ์Problem Solving/Baekjoon Online Judge (BOJ)2025. 4. 21. 16:33
Table of Contents

01. ๐ ๋ฌธ์ ํ์
N: ํ๋ ์์ญ (NxN์ ์์ญ, N <= 20)
์์ญ์ ์นธ ์ํ (0: ๋น ์นธ, 1: ๋ฒฝ)
M: ๋ชฉํ ์น๊ฐ ์ ($M <= N^2$)
์ด๊ธฐ์ฐ๋ฃ <= 500,000
- ํ์๋ ํญ์ ์ต๋จ๊ฒฝ๋ก๋ก ์ด๋
- ํ์ฌ ์์น์์ ๊ฐ์ฅ ๊ฑฐ๋ฆฌ๊ฐ ์งง์ ์น๊ฐ ์ฐ์ (๊ฑฐ๋ฆฌ๊ฐ ๊ฐ์ ๊ฒฝ์ฐ ์๋์ ์ฐ์ ์์๋ฅผ ๋ฐ๋ฆ)
- ํ ๋ฒํธ๊ฐ ๊ฐ์ฅ ์์ ์น๊ฐ ์ฐ์
- ์ด ๋ฒํธ๊ฐ ๊ฐ์ฅ ์์ ์น๊ฐ ์ฐ์
- ์์น๊ฐ ๊ฐ์ ๊ฒฝ์ฐ, ๊ฑฐ๋ฆฌ๋ 0
- ์ฐ๋ฃ๋ 1์นธ๋น 1์ฉ ์๋ชจ
- ๋ชฉ์ ์ง์ ๋์ฐฉ ์, ์น๊ฐ์ ํ์ฐ๊ณ ์๋ชจํ ์ฐ๋ฃ์ 2๊ฐ๊ฐ ์ถฉ์ (์ด๊ธฐ ์ฐ๋ฃ๋ฅผ ์ด๊ณผํ์ฌ ์ถฉ์ ๋ ๊ฐ๋ฅ)
- ์ด๋๋์ค, ์ฐ๋ฃ๊ฐ ๋ฐ๋ฅ๋๋ฉด ์ด๋ ์คํจ -> ํ๋ฃจ ์ ๋ฌด ์ข ๋ฃ
- ๋ง์ฝ, ๋ชฉ์ ์ง ๋์ฐฉ๊ณผ ๋์์ ์ฐ๋ฃ ๋ฐ๋ฅ ์์ ์ด๋ ์ฑ๊ณต ํ์
- ํ์ฌ ์์น์์ ๊ฐ์ฅ ๊ฑฐ๋ฆฌ๊ฐ ์งง์ ์น๊ฐ ์ฐ์ (๊ฑฐ๋ฆฌ๊ฐ ๊ฐ์ ๊ฒฝ์ฐ ์๋์ ์ฐ์ ์์๋ฅผ ๋ฐ๋ฆ)
- OUTPUT
๋ชจ๋ ์น๊ฐ์ ์ฑ๊ณต์ ์ผ๋ก ๋ฐ๋ ค๋ค์ฃผ๊ณ ๋จ์ ์ฐ๋ฃ์ ์์ ์ถ๋ ฅ
๋ง์ฝ, ์คํจ ์ -1์ ์ถ๋ ฅ
01-1. ๊ฐ๋ฅํ ์๊ฐ ๋ณต์ก๋
- ์๊ฐ ์ ํ 1์ด, N์ ์ต๋๊ฐ 20์ผ๋ก ์๊ฐ ๋ณต์ก๋ $O(N^k)$ ๋ ๊ฐ๋ฅ
01-2. ์๊ณ ๋ฆฌ์ฆ ์ ํ
- BFS๋ฅผ ์ด์ฉํ ์๋ฎฌ๋ ์ด์ ๋ฌธ์
- ๊ฐ๊น์ด ์น๊ฐ ํ์ ์ต์ ์ ๊ฒฝ์ฐ, (400 + 4) * 400 ์ ์ฐ์ฐ ํ์๋ก ์๊ฐ ๋ณต์ก๋ $O((V + E) * N^2)$
- ์น๊ฐ์ ์์น์์ ๋ชฉ์ ์ง ์ต๋จ ๊ฑฐ๋ฆฌ ํ์, (400 + 4)๋ก ์๊ฐ ๋ณต์ก๋ $O(V+E)$
- ์ต์ ์ ๊ฒฝ์ฐ, 400๋ช ์ ์น๊ฐ์ ํ์ฐ๋ ๋์, BFS ์ฐ์ฐ์ด 2๋ฒ ํ์ 400*2*(400 + 4) = 323,200 ๋ฒ์ ์ฐ์ฐ์ผ๋ก ์ถฉ๋ถํ ๊ฐ๋ฅ
02. ๐ ์ฝ๋ ์ค๊ณํ๊ธฐ
- ์์ญ์ ๋ํ ์ ๋ณด (0: ๋น ์นธ, 1: ๋ฒฝ)์ ์ด๊ธฐ ์ฐ๋ฃ์ ๋ํ ์ ๋ณด๋ฅผ ์ ๋ ฅ๋ฐ์
- ํ์ ์์น๋ฅผ ์ ๋ ฅ๋ฐ์ ํ์ฌ ์์น๋ก ์ ์ฅ -> taxi
- ๊ณ ๊ฐ์ ํ์ฌ ์์น์ ๊ณ ๊ฐ์ ๋ชฉ์ ์ง๋ฅผ ๋ฒกํฐ์ ๊ฐ๊ฐ ์ ์ฅ -> [customers, goal]
- ํ์ฌ ํ์ ์์น์์ BFS๋ก ๊ฐ ์นธ์ ๋ํ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐ
- ์์ง ํ์นํ์ง ์์ ๊ณ ๊ฐ์ ์์น๋ฅผ ์์ฐจ์ ์ผ๋ก ํ์ํ์ฌ ๊ฐ์ฅ ๊ฐ๊น์ด ๊ณ ๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ์ ์ฅํ๊ณ ,
๊ฐ์ฅ ๊ฐ๊น์ด ๊ณ ๊ฐ์ ์ฐพ์ ์ ์์ ๊ฒฝ์ฐ, ํ์๊ฐ ๋๋ฌํ ์ ์๋ ๊ณณ์ ์๋ค๊ณ ํ๋จํ์ฌ ์ฆ์ ์ข ๋ฃ - ํ์ฌ ๋จ์ ์ฐ๋ฃ๋ฅผ ํ์ธํ์ฌ ๊ณ ๊ฐ์ ์์น๊น์ง ์ถฉ๋ถํ๋ค๋ฉด ์ฐ๋ฃ๋ฅผ ์๋นํ์ฌ ์ด๋ํ๊ณ , ์๋๋ผ๋ฉด ์ฆ์ ์ข ๋ฃ
- ์์ง ํ์นํ์ง ์์ ๊ณ ๊ฐ์ ์์น๋ฅผ ์์ฐจ์ ์ผ๋ก ํ์ํ์ฌ ๊ฐ์ฅ ๊ฐ๊น์ด ๊ณ ๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ์ ์ฅํ๊ณ ,
- ๊ณ ๊ฐ ์์น๊น์ง ๋์ฐฉํ๊ณ BFS๋ฅผ ํตํด ํ์ฌ ์์น์์ ๊ฐ ์นธ์ ๋ํ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐ
- ํด๋น ๊ณ ๊ฐ์ ์ธ๋ฑ์ค๋ก ๋ชฉ์ ์ง๋ฅผ ์ฐพ์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ์ ธ์ด
๋ง์ฝ, BFS๋ฅผ ์งํํ๋ฉฐ ๋ชฉ์ ์ง์ ๋ฐฉ๋ฌธํ ์ ์ด ์์ ๊ฒฝ์ฐ, ํ์ฌ ์์น์์ ๋๋ฌํ ์ ์๋ ๊ณณ์ ์๋ค๊ณ ํ๋จํ์ฌ ์ฆ์ ์ข ๋ฃ - ํ์ฌ ๋จ์ ์ฐ๋ฃ๋ฅผ ํ์ธํ์ฌ ๋ชฉ์ ์ง๊น์ง ์ถฉ๋ถํ๋ค๋ฉด ์ฐ๋ฃ๋ฅผ ์๋นํ์ฌ ์ด๋ํ๊ณ ๊ณ ๊ฐ์ ์ํ๋ฅผ ์๋ฃ๋ก ์ ์ฅ,
์๋๋ผ๋ฉด ์ฆ์ ์ข ๋ฃ - ์๋นํ ์ฐ๋ฃ์ 2๋ฐฐ๋ฅผ ํ์ฌ ์ฐ๋ฃ์ ์ถ๊ฐ
- ํด๋น ๊ณ ๊ฐ์ ์ธ๋ฑ์ค๋ก ๋ชฉ์ ์ง๋ฅผ ์ฐพ์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ์ ธ์ด
- ๋ง์ฝ, ๋ชฉ์ ์ง๊น์ง ๋ฐ๋ ค๋ค ์ค ๊ณ ๊ฐ์ ์๊ฐ M๊ณผ ๊ฐ์ ๊ฒฝ์ฐ ๋ฐ๋ณต์ ์ข
๋ฃํ๊ณ ๋จ์ ์ฐ๋ฃ๋ฅผ ์ถ๋ ฅํ๊ณ ,
์๋๋ผ๋ฉด 4๋ฒ๋ถํฐ ๋ค์ ์์
03. ๐ ๏ธ ์๋๋ณ ์์
1ํ์ฐจ
- ํ์๊ฐ ๊ณ ๊ฐ์ด ์๋ ๊ณณ์ ๋๋ฌํ ์ ์๋ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํ์ง ๋ชป ํ์
2ํ์ฐจ
- ํ์๊ฐ ๊ณ ๊ฐ์ ๋ชฉ์ ์ง์ ๋๋ฌํ ์ ์๋ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํ์ง ๋ชป ํ์
3ํ์ฐจ
04. ๐๏ธ ์ ๋ต ์ฝ๋
#include <bits/stdc++.h>
using namespace std;
template <typename T>
using matrix = vector<vector<T>>;
int N, M, fuel;
matrix<int> board;
matrix<int> dists;
matrix<bool> visited;
vector<pair<int, int>> customers;
vector<pair<int, int>> goals;
vector<bool> is_done;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
bool is_valid(int x, int y){ return (x > 0 && x <= N && y > 0 && y <= N); }
void mark_dists(pair<int, int> cur){
queue<pair<int, int>> q;
dists.assign(N + 1, vector<int>(N + 1, 0));
visited.assign(N + 1, vector<bool>(N + 1, false));
q.emplace(cur.first, cur.second);
visited[cur.first][cur.second] = true;
dists[cur.first][cur.second] = 0;
while(!q.empty()){
auto& [x, y] = q.front(); q.pop();
for(int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
if(!is_valid(nx, ny) || board[nx][ny] == 1 || visited[nx][ny]) continue;
q.emplace(nx, ny);
visited[nx][ny] = true;
dists[nx][ny] = dists[x][y] + 1;
}
}
}
int get_customer(){
int near_idx = -1;
for(int i = 0; i < customers.size(); i++){
auto& [nx, ny] = customers[i];
if(is_done[i] || !visited[nx][ny]) continue;
else if(near_idx == -1) near_idx = i;
else{
auto& [x, y] = customers[near_idx];
if(dists[x][y] > dists[nx][ny]) near_idx = i;
else if(dists[x][y] == dists[nx][ny]){
if(nx < x) near_idx = i;
else if(nx == x && ny < y) near_idx = i;
}
}
}
return near_idx;
}
int solution(pair<int, int> taxi){
int res = 0;
while(res < M){
mark_dists(taxi);
int idx = get_customer();
if(idx == -1) break;
auto& [cx, cy] = customers[idx];
auto& [gx, gy] = goals[idx];
if(!visited[cx][cy]) break;
else if((fuel - dists[cx][cy]) >= 0){
taxi = customers[idx];
fuel -= dists[cx][cy];
} else break;
mark_dists(taxi);
if(!visited[gx][gy]) break;
else if((fuel - dists[gx][gy]) >= 0){
taxi = goals[idx];
fuel += dists[gx][gy];
is_done[idx] = true;
res++;
} else break;
}
return (res == M ? fuel : -1);
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> N >> M >> fuel;
board.resize(N + 1, vector<int>(N + 1));
for(int i = 1; i <= N; i++)
for(int j = 1; j <= N; j++) cin >> board[i][j];
int x, y;
cin >> x >> y;
for(int i = 0; i < M; i++){
int cx, cy, nx, ny;
cin >> cx >> cy >> nx >> ny;
customers.emplace_back(cx, cy);
goals.emplace_back(nx, ny);
is_done.push_back(false);
}
cout << solution({x, y}) << "\n";
return 0;
}
'Problem Solving > Baekjoon Online Judge (BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [C++] BOJ 15685 - ๋๋๊ณค ์ปค๋ธ (0) | 2025.04.23 |
|---|---|
| [C++] BOJ 9328 - ์ด์ (1) | 2025.04.22 |
| [C++] BOJ 12850 - ๋ณธ๋ ์ฐ์ฑ 2 (0) | 2025.04.20 |
| [C++] BOJ 11444 - ํผ๋ณด๋์น ์ 6 (0) | 2025.04.20 |
| [C++] BOJ 2638 - ์น์ฆ (0) | 2025.04.20 |
@ONE_ :: ์ ํธ์
์๋ชป๋ ์ ๋ณด๊ฐ ์๋ค๋ฉด ๋ง์ํด์ฃผ์ธ์!