๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/9879
9879๋ฒ: Cross Country Skiing
The cross-country skiing course at the winter Moolympics is described by an M x N grid of elevations (1 <= M,N <= 500), each elevation being in the range 0 .. 1,000,000,000. Some of the cells in this grid are designated as waypoints for the course. The org
www.acmicpc.net
๊ทธ๋ํ ํ์ ๋ฌธ์
๋ฌธ์ ํด์
๊ฒจ์ธ Moolympics ํฌ๋ก์ค ์ปจํธ๋ฆฌ ์ข ๋ชฉ ์ฝ์ค๋ M x N ํฌ๊ธฐ์ ๊ณ ๋๋ก ์ด๋ฃจ์ด์ง ์ขํํ๋ฉด์ผ๋ก ๋ํ๋ธ๋ค.
๊ฐ ๊ณ ๋๋ 0๋ถํฐ 1,000,000,000 ๊น์ง ๊ฐ๋ฅํ๋ค.
์ขํํ๋ฉด์ ๋ช ์นธ์ waypoints๋ก ์ ํด์ ธ์๋ค. Moolympic ์ ์ฃผ์ต์๋ ์๋ค์ด(์๊ฐ ์ฐธ๊ฐ์์ธ๋ฏ) ๋ชจ๋ waypoints๋ฅผ ๋๋ฌํ ์ ์๊ฒํ๋ ๊ณ ๋์ ์ฐจ์ด D์ ์ต์๋ฅผ ๊ตฌํ๋ ค๊ณ ํ๋ค. ์ด๋ํ ์นธ์ ๊ณ ๋ ์ฐจ์ด๊ฐ ์ต์๊ฐ ๋๋๋ก ๋ชจ๋ waypoints๋ฅผ ๋ฐฉ๋ฌธํ์ ๋ ๊ณ ๋์ ์ฐจ์ด๊ฐ ์ต์ D๋ ๋ผ์ผ ๋ชจ๋ waypoints์ ๋๋ฌํ ์ ์๋ค๋ ๋ป์ด๋ค. ์ธ์ ํ ๋ค ๋ฐฉํฅ ์ํ์ข์ฐ๋ก ์ด๋ํ ์ ์๋ค. ์ด๋ D์ ์ต์๋ฅผ ์ฐพ์๋ผ.
ํ์ด
์ด๋ถํ์์ ํตํด์ D ๊ฐ์ ์ง์ ํด์ค๋ค. ๋ฒ์๋ Left๋ 0, Right๋ ์ต๋๊ณ ๋์ ์ต์๊ณ ๋์ ์ฐจ์ด๊ฐ ๋๋ค.
D ๊ฐ์ mid๊ฐ์ผ๋ก ์ง์ ํด์ฃผ๊ณ , BFS๋ก ํ์ํ๋๋ฐ ๊ณ ๋์ ์ฐจ์ด๊ฐ mid๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์นธ์ผ๋ก๋ง ์ด๋ํ ์ ์๋ค. BFS ํ์์ ๋ชจ๋ ๋ง์น ํ ๋ชจ๋ waypoints๋ฅผ ๋ฐฉ๋ฌธํ๋ค๋ฉด ๊ทธ๋์ mid๊ฐ์ ์ ํจํ ๊ฐ์ด ๋๋ค. ๊ทธ๋ฌ๋ฉด ๋ฒ์๋ฅผ Left ~ mid - 1๋ก ๋ฒ์๋ฅผ ์ค์ฌ์ ๋ ์์ ์ ํจํ D๊ฐ์ ์ฐพ์๋ณธ๋ค. ์ด๋ waypoint๋ค ์ค์ ์์์ ํ ์ง์ ์ ์์์ ์ผ๋ก ์ ๋ ฅํ๋ค.
๋๋ ๋ชจ๋ waypoints๋ฅผ ๋ฐฉ๋ฌธํ๋์ง๋ฅผ ์ฒดํฌํ๊ธฐ ์ํด์, ํ์ ๋์ค์ waypoints๋ฅผ ๋ช ๊ตฐ๋ฐ ๋ฐฉ๋ฌธํ๋์ง ๊ธฐ๋กํ๊ณ ํ์ ์ข ๋ฃ ํ์ ๋ชจ๋ waypoint์ ์์ ๋ฐฉ๋ฌธํ waypoint์ ์๊ฐ ๊ฐ์ผ๋ฉด ๋ชจ๋ waypoint๋ฅผ ๋ฐฉ๋ฌธํ๋ค๊ณ ํ๋จํ๋ค.
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
|
#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 <algorithm>
#include <iomanip>
#include <map>
#define mod 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 500 + 5;
int Map[MAX_N][MAX_N];
int dx[] = { 1,0,-1,0 };
int dy[] = { 0,1,0,-1 };
int N, M;
vector <pair<int, int>> waypoints;
bool is_waypoint[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
bool is_ok(int y, int x) {
if (y < 0 || y >= N || x < 0 || x >= M)
return false;
return true;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
int L = INF;
int R = -1;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> Map[i][j];
L = min(Map[i][j], L);
R = max(Map[i][j], R);
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
int num; cin >> num;
if (num == 1) {
waypoints.push_back({ i,j });
is_waypoint[i][j] = true;
}
}
}
R = R - L;
L = 0;
int ans = INF;
while (L <= R) {
int mid = (L + R) / 2;
queue<pair<int, int>> q;
q.push(waypoints[0]);
int cnt = 0;
memset(visited, false, sizeof(visited));
while (!q.empty()) {
pair<int, int> now = q.front();
q.pop();
if (visited[now.first][now.second])
continue;
visited[now.first][now.second] = true;
if (is_waypoint[now.first][now.second])
cnt++;
if (cnt == waypoints.size())
break;
for (int i = 0; i < 4; i++) {
pair<int, int> next = { now.first + dy[i], now.second + dx[i] };
if (is_ok(next.first, next.second)) {
int Diff = abs(Map[now.first][now.second] - Map[next.first][next.second]);
if (Diff > mid)
continue;
if (visited[next.first][next.second])
continue;
q.push(next);
}
}
}
if (cnt == waypoints.size()) {
ans = min(ans, mid);
R = mid - 1;
}
else L = mid + 1;
}
cout << ans << "\n";
return 0;
}
|
cs |
'๋ฐฑ์ค > Gold' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 15686 : ์นํจ ๋ฐฐ๋ฌ (0) | 2022.05.09 |
---|---|
[BOJ] 15685 : ๋๋๊ณค ์ปค๋ธ (0) | 2022.05.09 |
[BOJ] 14529 : Where's Bessie? (0) | 2022.04.29 |
[BOJ] 10037 : Decorating the Pastures (0) | 2022.04.27 |
[BOJ] 13595 : Mania de Par (0) | 2022.04.27 |