답안 #697472

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
697472 2023-02-10T02:39:08 Z europium Mecho (IOI09_mecho) C++17
100 / 100
187 ms 6336 KB
// accepted - 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});
        }
    }

    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;

    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 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 320 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 103 ms 6204 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 316 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 324 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 320 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 324 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 1556 KB Output is correct
34 Correct 7 ms 1552 KB Output is correct
35 Correct 21 ms 1608 KB Output is correct
36 Correct 9 ms 1868 KB Output is correct
37 Correct 9 ms 1876 KB Output is correct
38 Correct 40 ms 1884 KB Output is correct
39 Correct 12 ms 2272 KB Output is correct
40 Correct 14 ms 2316 KB Output is correct
41 Correct 37 ms 2264 KB Output is correct
42 Correct 13 ms 2708 KB Output is correct
43 Correct 14 ms 2692 KB Output is correct
44 Correct 45 ms 2728 KB Output is correct
45 Correct 15 ms 3160 KB Output is correct
46 Correct 17 ms 3196 KB Output is correct
47 Correct 78 ms 3312 KB Output is correct
48 Correct 19 ms 3748 KB Output is correct
49 Correct 22 ms 3728 KB Output is correct
50 Correct 64 ms 3716 KB Output is correct
51 Correct 21 ms 4292 KB Output is correct
52 Correct 24 ms 4308 KB Output is correct
53 Correct 88 ms 4228 KB Output is correct
54 Correct 25 ms 4892 KB Output is correct
55 Correct 26 ms 4888 KB Output is correct
56 Correct 94 ms 5012 KB Output is correct
57 Correct 29 ms 5524 KB Output is correct
58 Correct 31 ms 5532 KB Output is correct
59 Correct 105 ms 5528 KB Output is correct
60 Correct 37 ms 6204 KB Output is correct
61 Correct 45 ms 6232 KB Output is correct
62 Correct 141 ms 6200 KB Output is correct
63 Correct 120 ms 6204 KB Output is correct
64 Correct 181 ms 6276 KB Output is correct
65 Correct 187 ms 6276 KB Output is correct
66 Correct 143 ms 6208 KB Output is correct
67 Correct 133 ms 6204 KB Output is correct
68 Correct 66 ms 6244 KB Output is correct
69 Correct 57 ms 6236 KB Output is correct
70 Correct 52 ms 6244 KB Output is correct
71 Correct 49 ms 6308 KB Output is correct
72 Correct 43 ms 6232 KB Output is correct
73 Correct 43 ms 6232 KB Output is correct
74 Correct 66 ms 6220 KB Output is correct
75 Correct 71 ms 6188 KB Output is correct
76 Correct 68 ms 6192 KB Output is correct
77 Correct 73 ms 6200 KB Output is correct
78 Correct 88 ms 6212 KB Output is correct
79 Correct 60 ms 6220 KB Output is correct
80 Correct 75 ms 6228 KB Output is correct
81 Correct 80 ms 6228 KB Output is correct
82 Correct 69 ms 6192 KB Output is correct
83 Correct 95 ms 6336 KB Output is correct
84 Correct 77 ms 6208 KB Output is correct
85 Correct 73 ms 6196 KB Output is correct
86 Correct 82 ms 6216 KB Output is correct
87 Correct 79 ms 6220 KB Output is correct
88 Correct 108 ms 6216 KB Output is correct
89 Correct 87 ms 6208 KB Output is correct
90 Correct 90 ms 6216 KB Output is correct
91 Correct 95 ms 6208 KB Output is correct
92 Correct 89 ms 6220 KB Output is correct