Submission #1094702

# Submission time Handle Problem Language Result Execution time Memory
1094702 2024-09-30T10:10:29 Z akamizane Mecho (IOI09_mecho) C++17
100 / 100
170 ms 6884 KB
#include<bits/stdc++.h>
 
using namespace std;
  
using pii = pair<int,int>;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define fi first
#define se second

void solve(){
    int n, k;
    cin >> n >> k;
    string s[n];
    REP(i, n){
        cin >> s[i];
    }
    pii start, des;
    vector<vector<int>> dis(n, vector<int> (n ,1e9));
    queue<pii> q;
    REP(i, n){
        REP(j, n){
            if (s[i][j] == 'M'){
                start = {i, j};
            }
            if (s[i][j] == 'D'){
                des = {i, j};
            }
            if (s[i][j] == 'H'){
                q.push({i, j});
                dis[i][j] = 0;
            }
        }
    }
    int dx[4] = {0, 0, -1, 1};
    int dy[4] = {-1, 1, 0, 0};
    // bfs for bee 
    while(q.size()){
        auto [x, y] = q.front();
        q.pop();
        for (int i = 0; i < 4; i++){
            int a = x + dx[i], b = y + dy[i];
            if (a < 0 || a >= n || b < 0 || b >= n) continue;
            if (s[a][b] == 'T' || s[a][b] == 'D') continue;
            if (dis[x][y] + 1 < dis[a][b]){
                dis[a][b] = dis[x][y] + 1;
                q.push({a, b});
            } 
        }
    }
    auto check = [&](int m){
    
    if (dis[start.fi][start.se] <= m) return false;
    queue<pii> que;
    que.push(start);
    vector<vector<int>> dis2(n, vector<int> (n, 1e9));
    dis2[start.fi][start.se] = 0;
    while(que.size()){
        auto [x, y] = que.front();
        que.pop();
        if (x == des.fi && y == des.se) break;
        for (int i = 0; i < 4; i++){
            int a = x + dx[i], b = y + dy[i];
            if (a < 0 || a >= n || b < 0 || b >= n) continue;
            if (s[a][b] == 'T') continue;
            int cur = dis2[x][y];
            
            if ((cur + 1) / k < dis[a][b] - m && cur + 1 < dis2[a][b]){
                dis2[a][b] = cur + 1;
                que.push({a, b});
            }
        }
    }
    return dis2[des.fi][des.se] != 1e9;
};
  if (!check(0)){
    cout << -1;
    return;
  }
  // time must be lower than the time a bee touch start position
  int l = -1, r = dis[start.fi][start.se];
  while(r - l > 1){
    int mid = (l + r) / 2;
    if (check(mid)) l = mid;
    else r = mid;
  }
  cout << l;
}
 
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 103 ms 6848 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 352 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 456 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 4 ms 1676 KB Output is correct
34 Correct 4 ms 1684 KB Output is correct
35 Correct 18 ms 1692 KB Output is correct
36 Correct 5 ms 2108 KB Output is correct
37 Correct 5 ms 2076 KB Output is correct
38 Correct 25 ms 1884 KB Output is correct
39 Correct 7 ms 2488 KB Output is correct
40 Correct 7 ms 2508 KB Output is correct
41 Correct 34 ms 2480 KB Output is correct
42 Correct 8 ms 3036 KB Output is correct
43 Correct 8 ms 2956 KB Output is correct
44 Correct 44 ms 2976 KB Output is correct
45 Correct 10 ms 3468 KB Output is correct
46 Correct 10 ms 3468 KB Output is correct
47 Correct 51 ms 3488 KB Output is correct
48 Correct 12 ms 4104 KB Output is correct
49 Correct 12 ms 4128 KB Output is correct
50 Correct 60 ms 4048 KB Output is correct
51 Correct 15 ms 4680 KB Output is correct
52 Correct 14 ms 4684 KB Output is correct
53 Correct 77 ms 4692 KB Output is correct
54 Correct 16 ms 5408 KB Output is correct
55 Correct 16 ms 5412 KB Output is correct
56 Correct 86 ms 5356 KB Output is correct
57 Correct 19 ms 6064 KB Output is correct
58 Correct 20 ms 6136 KB Output is correct
59 Correct 95 ms 6036 KB Output is correct
60 Correct 22 ms 6884 KB Output is correct
61 Correct 22 ms 6880 KB Output is correct
62 Correct 117 ms 6844 KB Output is correct
63 Correct 107 ms 6844 KB Output is correct
64 Correct 170 ms 6844 KB Output is correct
65 Correct 160 ms 6828 KB Output is correct
66 Correct 118 ms 6848 KB Output is correct
67 Correct 25 ms 6744 KB Output is correct
68 Correct 45 ms 6856 KB Output is correct
69 Correct 40 ms 6864 KB Output is correct
70 Correct 35 ms 6820 KB Output is correct
71 Correct 30 ms 6852 KB Output is correct
72 Correct 28 ms 6864 KB Output is correct
73 Correct 38 ms 6800 KB Output is correct
74 Correct 73 ms 6856 KB Output is correct
75 Correct 65 ms 6832 KB Output is correct
76 Correct 66 ms 6856 KB Output is correct
77 Correct 72 ms 6864 KB Output is correct
78 Correct 24 ms 6744 KB Output is correct
79 Correct 70 ms 6864 KB Output is correct
80 Correct 66 ms 6872 KB Output is correct
81 Correct 68 ms 6856 KB Output is correct
82 Correct 64 ms 6848 KB Output is correct
83 Correct 89 ms 6832 KB Output is correct
84 Correct 81 ms 6848 KB Output is correct
85 Correct 74 ms 6816 KB Output is correct
86 Correct 88 ms 6824 KB Output is correct
87 Correct 79 ms 6872 KB Output is correct
88 Correct 90 ms 6836 KB Output is correct
89 Correct 96 ms 6848 KB Output is correct
90 Correct 95 ms 6844 KB Output is correct
91 Correct 100 ms 6844 KB Output is correct
92 Correct 96 ms 6856 KB Output is correct