Submission #952038

# Submission time Handle Problem Language Result Execution time Memory
952038 2024-03-23T04:02:30 Z Yell0 Mecho (IOI09_mecho) C++17
100 / 100
117 ms 9308 KB
#include <bits/stdc++.h>
 
using namespace std;
typedef pair<int,int> pii;
const int MN=802;
int N, S, bee[MN][MN], best[MN][MN], dist[MN][MN];
char gr[MN][MN];
pii start, dest;

bool chk(int r, int c) {
  return 0 <= r && r < N && 0 <= c && c < N && gr[r][c] != 'T' && gr[r][c] != 'H';
}

bool bschk(int wait) {
  queue<pii> q;
  q.emplace(start);
  dist[start.first][start.second] = 0;
  if(wait > bee[start.first][start.second]) return 0;
  while(!q.empty()) {
    int r = q.front().first, c = q.front().second;
    q.pop();
    int d = dist[r][c] + 1;
    if(chk(r+1,c) && dist[r+1][c] == -1 && bee[r+1][c] - d/S - wait > -1) {
      dist[r+1][c] = d;
      q.emplace(r+1, c);
    }
    if(chk(r-1,c) && dist[r-1][c] == -1 && bee[r-1][c] - d/S - wait > -1) {
      dist[r-1][c] = d;
      q.emplace(r-1, c);
    }
    if(chk(r,c+1) && dist[r][c+1] == -1 && bee[r][c+1] - d/S - wait > -1) {
      dist[r][c+1] = d;
      q.emplace(r, c+1);
    }
    if(chk(r,c-1) && dist[r][c-1] == -1 && bee[r][c-1] - d/S - wait > -1) {
      dist[r][c-1] = d;
      q.emplace(r, c-1);
    }
  }

  return dist[dest.first][dest.second] > -1;
}
 
int main() {
  ios::sync_with_stdio(0); cin.tie(0);

  cin>>N>>S;
  queue<pii> beeq;
  for(int i=0; i<N; ++i) {
    cin>>gr[i];
    for(int j=0; j<N; ++j) {
      if(gr[i][j] == 'H') beeq.emplace(i,j);
      if(gr[i][j] == 'M') start = pii(i,j);
      if(gr[i][j] == 'D') dest = pii(i,j);
    }
  }

  memset(bee, -1, sizeof(bee));
  while(!beeq.empty()) {
    int r = beeq.front().first, c = beeq.front().second;
    beeq.pop();
    int dist = bee[r][c];
    if(chk(r+1,c) && bee[r+1][c] == -1 && gr[r+1][c] != 'D') {
      bee[r+1][c] = dist+1;
      beeq.emplace(r+1, c);
    }
    if(chk(r-1,c) && bee[r-1][c] == -1 && gr[r-1][c] != 'D') {
      bee[r-1][c] = dist+1;
      beeq.emplace(r-1, c);
    }
    if(chk(r,c+1) && bee[r][c+1] == -1 && gr[r][c+1] != 'D') {
      bee[r][c+1] = dist+1;
      beeq.emplace(r, c+1);
    }
    if(chk(r,c-1) && bee[r][c-1] == -1 && gr[r][c-1] != 'D') {
      bee[r][c-1] = dist+1;
      beeq.emplace(r, c-1);
    }
  }
  bee[dest.first][dest.second] = 1e9;

  int lo = 0, hi = N*N, ans = -1;
  while(lo <= hi) {
    int mid = (lo+hi)/2;
    memset(dist, -1, sizeof(dist));
    if(bschk(mid)) {
      ans = mid;
      lo = mid+1;
    } else hi = mid-1;
  }
  
  cout<<ans<<'\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8280 KB Output is correct
3 Correct 2 ms 8284 KB Output is correct
4 Correct 2 ms 8284 KB Output is correct
5 Correct 2 ms 8536 KB Output is correct
6 Correct 2 ms 8284 KB Output is correct
7 Correct 43 ms 8808 KB Output is correct
8 Correct 2 ms 8284 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 2 ms 8284 KB Output is correct
11 Correct 3 ms 8284 KB Output is correct
12 Correct 2 ms 8284 KB Output is correct
13 Correct 3 ms 8284 KB Output is correct
14 Correct 2 ms 8284 KB Output is correct
15 Correct 3 ms 8280 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 3 ms 8284 KB Output is correct
18 Correct 2 ms 8284 KB Output is correct
19 Correct 2 ms 8280 KB Output is correct
20 Correct 2 ms 8284 KB Output is correct
21 Correct 2 ms 8284 KB Output is correct
22 Correct 2 ms 8284 KB Output is correct
23 Correct 2 ms 8284 KB Output is correct
24 Correct 2 ms 8284 KB Output is correct
25 Correct 2 ms 8284 KB Output is correct
26 Correct 2 ms 8284 KB Output is correct
27 Correct 2 ms 8284 KB Output is correct
28 Correct 3 ms 8284 KB Output is correct
29 Correct 3 ms 8284 KB Output is correct
30 Correct 3 ms 8284 KB Output is correct
31 Correct 3 ms 8272 KB Output is correct
32 Correct 2 ms 8280 KB Output is correct
33 Correct 4 ms 8540 KB Output is correct
34 Correct 4 ms 8540 KB Output is correct
35 Correct 14 ms 8508 KB Output is correct
36 Correct 5 ms 8540 KB Output is correct
37 Correct 5 ms 8536 KB Output is correct
38 Correct 16 ms 8708 KB Output is correct
39 Correct 5 ms 8540 KB Output is correct
40 Correct 6 ms 8780 KB Output is correct
41 Correct 22 ms 8672 KB Output is correct
42 Correct 6 ms 8792 KB Output is correct
43 Correct 7 ms 8824 KB Output is correct
44 Correct 27 ms 8796 KB Output is correct
45 Correct 6 ms 8796 KB Output is correct
46 Correct 7 ms 8792 KB Output is correct
47 Correct 29 ms 8796 KB Output is correct
48 Correct 7 ms 8876 KB Output is correct
49 Correct 8 ms 9048 KB Output is correct
50 Correct 35 ms 8796 KB Output is correct
51 Correct 7 ms 8792 KB Output is correct
52 Correct 9 ms 8796 KB Output is correct
53 Correct 44 ms 8796 KB Output is correct
54 Correct 11 ms 8796 KB Output is correct
55 Correct 10 ms 8796 KB Output is correct
56 Correct 70 ms 8784 KB Output is correct
57 Correct 11 ms 8796 KB Output is correct
58 Correct 13 ms 8796 KB Output is correct
59 Correct 73 ms 8796 KB Output is correct
60 Correct 11 ms 9052 KB Output is correct
61 Correct 12 ms 8872 KB Output is correct
62 Correct 64 ms 9032 KB Output is correct
63 Correct 63 ms 8792 KB Output is correct
64 Correct 117 ms 8992 KB Output is correct
65 Correct 91 ms 9056 KB Output is correct
66 Correct 70 ms 8788 KB Output is correct
67 Correct 69 ms 9036 KB Output is correct
68 Correct 28 ms 9100 KB Output is correct
69 Correct 27 ms 8960 KB Output is correct
70 Correct 21 ms 9044 KB Output is correct
71 Correct 24 ms 9108 KB Output is correct
72 Correct 18 ms 9044 KB Output is correct
73 Correct 17 ms 9304 KB Output is correct
74 Correct 31 ms 9308 KB Output is correct
75 Correct 39 ms 9300 KB Output is correct
76 Correct 36 ms 9244 KB Output is correct
77 Correct 32 ms 9308 KB Output is correct
78 Correct 48 ms 9052 KB Output is correct
79 Correct 27 ms 9308 KB Output is correct
80 Correct 32 ms 8996 KB Output is correct
81 Correct 36 ms 9048 KB Output is correct
82 Correct 34 ms 9052 KB Output is correct
83 Correct 41 ms 9008 KB Output is correct
84 Correct 37 ms 9084 KB Output is correct
85 Correct 40 ms 9044 KB Output is correct
86 Correct 39 ms 9044 KB Output is correct
87 Correct 40 ms 9040 KB Output is correct
88 Correct 42 ms 9156 KB Output is correct
89 Correct 42 ms 9100 KB Output is correct
90 Correct 44 ms 9040 KB Output is correct
91 Correct 43 ms 9040 KB Output is correct
92 Correct 47 ms 9052 KB Output is correct