답안 #697460

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
697460 2023-02-10T00:18:32 Z europium Mecho (IOI09_mecho) C++17
100 / 100
186 ms 6220 KB
// - 9/2/23
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <cmath>
#include <iterator>
#include <set>
#include <map>
#include <math.h>
#include <iomanip>
#include <unordered_set>
#include <queue>
#include <climits>
using namespace std;
// clang++ -std=c++17 IOI2009_mecho.cpp && ./a.out

using ll = long long;
const int INF = 1e9;
const int dx[] {0, 1, 0, -1};
const int dy[] {1, 0, -1, 0};
int n, s;
int start_x, start_y, end_x, end_y;
vector<vector<char>> grid;
vector<vector<int>> bee;

bool valid(int x, int y){
    return (x >= 0 && x < n && y >= 0 && y < n);
}

void get_bee_dist(){
    queue<tuple<int,int,int>> q;

    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            if (grid[i][j] == 'H'){
                bee[i][j] = 0;
                q.push({i, j, 0});
            }
        }
    }

    while (!q.empty()){
        int x, y, d;
        tie(x, y, d) = q.front();
        q.pop();

        for (int k = 0; k < 4; k++){
            int i = x + dx[k], j = y + dy[k];

            if (!valid(i, j) || grid[i][j] == 'T' || grid[i][j] == 'D' || bee[i][j] <= d + 1) continue;

            bee[i][j] = min(bee[i][j], d + 1);
            q.push({i, j, d + 1});
        }
    }
}

bool f(int p){
    vector<vector<int>> mecho(n, vector<int> (n, INF));

    queue<tuple<int,int,int>> q;
    mecho[start_x][start_y] = s;
    q.push({start_x, start_y, s});

    if (p + 1 > bee[start_x][start_y]) return true;

    while (!q.empty()){
        int x, y, d;
        tie(x, y, d) = q.front();
        q.pop();

        for (int k = 0; k < 4; k++){
            int i = x + dx[k], j = y + dy[k];

            if (!valid(i, j) || grid[i][j] == 'T' || mecho[i][j] <= d + 1) continue;
            if ((i != end_x || j != end_y) && ((d + 1) / s) + p > bee[i][j]) continue;

            mecho[i][j] = min(mecho[i][j], d + 1);
            q.push({i, j, d + 1});
        }
    }

    // cout << p << '\n';
    //
    // for (int i = 0; i < n; i++){
    //     for (int j = 0; j < n; j++) cout << (mecho[i][j] / s) + p << ' ';
    //     cout << '\n';
    // }

    return mecho[end_x][end_y] == INF;
}

void solve(){
    cin >> n >> s;

    grid.resize(n, vector<char>(n));
    bee.resize(n, vector<int> (n, INF));

    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            cin >> grid[i][j];
            if (grid[i][j] == 'M') {start_x = i; start_y = j;}
            if (grid[i][j] == 'D') {end_x = i; end_y = j;}
        }
    }

    get_bee_dist();

    int l = 0, r = n * n, ans, mid;

    while (l < r){
        mid = l + (r - l) / 2;
    	f(mid) ? r = mid : l = mid + 1;
    }

    ans = l;

    // for (int i = 0; i < n; i++){
    //     for (int j = 0; j < n; j++) cout << bee[i][j] << ' ';
    //     cout << '\n';
    // }

    cout << ans - 1;
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    // freopen("input.txt", "r", stdin);
    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 93 ms 6092 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 288 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 6 ms 1436 KB Output is correct
34 Correct 6 ms 1428 KB Output is correct
35 Correct 20 ms 1436 KB Output is correct
36 Correct 8 ms 1748 KB Output is correct
37 Correct 9 ms 1748 KB Output is correct
38 Correct 33 ms 1748 KB Output is correct
39 Correct 11 ms 2140 KB Output is correct
40 Correct 10 ms 2148 KB Output is correct
41 Correct 35 ms 2148 KB Output is correct
42 Correct 12 ms 2572 KB Output is correct
43 Correct 13 ms 2572 KB Output is correct
44 Correct 47 ms 2580 KB Output is correct
45 Correct 15 ms 3108 KB Output is correct
46 Correct 21 ms 3028 KB Output is correct
47 Correct 71 ms 3044 KB Output is correct
48 Correct 18 ms 3548 KB Output is correct
49 Correct 20 ms 3548 KB Output is correct
50 Correct 62 ms 3600 KB Output is correct
51 Correct 21 ms 4152 KB Output is correct
52 Correct 26 ms 4140 KB Output is correct
53 Correct 82 ms 4120 KB Output is correct
54 Correct 26 ms 4784 KB Output is correct
55 Correct 29 ms 4716 KB Output is correct
56 Correct 97 ms 4712 KB Output is correct
57 Correct 29 ms 5412 KB Output is correct
58 Correct 32 ms 5400 KB Output is correct
59 Correct 119 ms 5388 KB Output is correct
60 Correct 33 ms 6188 KB Output is correct
61 Correct 35 ms 6096 KB Output is correct
62 Correct 150 ms 6116 KB Output is correct
63 Correct 123 ms 6076 KB Output is correct
64 Correct 184 ms 6060 KB Output is correct
65 Correct 186 ms 6104 KB Output is correct
66 Correct 137 ms 6072 KB Output is correct
67 Correct 152 ms 6064 KB Output is correct
68 Correct 73 ms 6088 KB Output is correct
69 Correct 63 ms 6080 KB Output is correct
70 Correct 61 ms 6108 KB Output is correct
71 Correct 52 ms 6164 KB Output is correct
72 Correct 49 ms 6220 KB Output is correct
73 Correct 58 ms 6216 KB Output is correct
74 Correct 71 ms 6088 KB Output is correct
75 Correct 88 ms 6072 KB Output is correct
76 Correct 67 ms 6172 KB Output is correct
77 Correct 80 ms 6044 KB Output is correct
78 Correct 96 ms 6096 KB Output is correct
79 Correct 81 ms 6088 KB Output is correct
80 Correct 66 ms 6060 KB Output is correct
81 Correct 78 ms 6068 KB Output is correct
82 Correct 94 ms 6096 KB Output is correct
83 Correct 91 ms 6088 KB Output is correct
84 Correct 77 ms 6072 KB Output is correct
85 Correct 72 ms 6088 KB Output is correct
86 Correct 77 ms 6080 KB Output is correct
87 Correct 78 ms 6120 KB Output is correct
88 Correct 90 ms 6100 KB Output is correct
89 Correct 83 ms 6088 KB Output is correct
90 Correct 85 ms 6068 KB Output is correct
91 Correct 94 ms 6092 KB Output is correct
92 Correct 82 ms 6076 KB Output is correct