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

[BOJ] 9879 : Cross Country Skiing

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

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<intpair<intint>>vector<pair<intpair<intint>>>, greater<pair<intpair<intint>>>> 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<intint>> 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<intint>> q;
        q.push(waypoints[0]);
        int cnt = 0;
        memset(visited, falsesizeof(visited));
        while (!q.empty()) {
            pair<intint> 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<intint> 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

728x90

'๋ฐฑ์ค€ > 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