#include <algorithm>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using ch = char;
using str = string;
#define pb push_back
#define elif else if
#define sz(C) (ll) C.size()
#define mp make_pair
#define mt make_tuple
#define flip(C) reverse(C.begin(), C.end())
#define ssort(C) sort(C.begin(), C.end())
#define rsort(C) sort(C.begin(), C.end(), greater<>())
#define FOR(x, e) for(ll x = 0; x < (ll) e; x++)
#define FORR(x, e) for(ll x = (ll) e - 1; x >= 0; x--)
#define FOB(x, b, e) for(auto x = b; x < e; x++)
#define FOI(x, e, i) for(ll x = 0; x < (ll) e; x += (ll) i)
#define FOBI(x, b, e, i) for(ll x = (ll) b; x < (ll) e; x += (ll) i)
#define FORE(x, C) for(auto &x : C)
#ifdef LOCAL
#include "tester.cpp"
#define main test_main
extern istringstream fin;
extern ostringstream fout;
string test_file_name = "tests";
#define cin fin
#define cout fout
#endif
vector<int> di { -1, 0, 1, 0};
vector<int> dj { 0, -1, 0, 1};
void fill_b(ll &n, vector<vector<char>> &g, queue<pair<ll, ll>> &bfs_b, vector<vector<ll>> &d_b) {
while(sz(bfs_b)) {
ll i0 = bfs_b.front().first, j0 = bfs_b.front().second;
bfs_b.pop();
FOR(k, 4) {
ll i1 = i0 + di[k], j1 = j0 + dj[k];
if(i1 < 0 || i1 >= n || j1 < 0 || j1 >= n) continue;
if(g[i1][j1] == 'T' || g[i1][j1] == 'D') continue;
if(d_b[i1][j1] != 1e18) continue;
d_b[i1][j1] = d_b[i0][j0] + 1;
bfs_b.push({i1, j1});
}
}
}
bool valid(ll &mid, ll &n, ll &s, vector<vector<ll>> &d_b, vector<vector<char>> &g, queue<pair<ll, ll>> bfs_m, vector<vector<ll>> d_m) {
while(sz(bfs_m)) {
ll i0 = bfs_m.front().first, j0 = bfs_m.front().second, d = d_m[i0][j0] + 1;
bfs_m.pop();
FOR(k, 4) {
ll i1 = i0 + di[k], j1 = j0 + dj[k];
if(i1 < 0 || i1 >= n || j1 < 0 || j1 >= n) continue;
if(g[i1][j1] == 'T') continue;
if(d_m[i1][j1] != 1e18) continue;
if(d_b[i1][j1] < mid + (d + s - 1) / s) continue;
if(g[i1][j1] == 'D') return true;
d_m[i1][j1] = d;
bfs_m.push({i1, j1});
}
}
return false;
}
int main() {
ll n, s, lo = 0, hi = 1e18;
cin >> n >> s;
vector<vector<char>> g(n, vector<char>(n));
vector<vector<ll>> d_b(n, vector<ll>(n, 1e18)), d_m(n, vector<ll>(n, 1e18));
queue<pair<ll, ll>> bfs_b, bfs_m;
FOR(i, n) FOR(j, n) {
cin >> g[i][j];
if(g[i][j] == 'H') {
bfs_b.push({i, j});
d_b[i][j] = 0;
}
elif(g[i][j] == 'M') {
bfs_m.push({i, j});
d_m[i][j] = 0;
}
}
fill_b(n, g, bfs_b, d_b);
while(hi - lo > 0) {
ll mid = lo + (hi - lo + 1) / 2;
if(valid(mid, n, s, d_b, g, bfs_m, d_m)) lo = mid;
else hi = mid - 1;
}
cout << lo << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
300 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
321 ms |
16836 KB |
Output is correct |
8 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
32 |
Correct |
1 ms |
432 KB |
Output is correct |
33 |
Incorrect |
27 ms |
3648 KB |
Output isn't correct |
34 |
Correct |
28 ms |
3464 KB |
Output is correct |
35 |
Correct |
44 ms |
3660 KB |
Output is correct |
36 |
Incorrect |
36 ms |
4612 KB |
Output isn't correct |
37 |
Correct |
44 ms |
4620 KB |
Output is correct |
38 |
Correct |
69 ms |
4436 KB |
Output is correct |
39 |
Incorrect |
48 ms |
5484 KB |
Output isn't correct |
40 |
Correct |
47 ms |
5464 KB |
Output is correct |
41 |
Correct |
94 ms |
5644 KB |
Output is correct |
42 |
Incorrect |
57 ms |
6864 KB |
Output isn't correct |
43 |
Correct |
80 ms |
6816 KB |
Output is correct |
44 |
Correct |
99 ms |
6868 KB |
Output is correct |
45 |
Incorrect |
65 ms |
8204 KB |
Output isn't correct |
46 |
Correct |
82 ms |
8176 KB |
Output is correct |
47 |
Correct |
166 ms |
8156 KB |
Output is correct |
48 |
Incorrect |
100 ms |
9748 KB |
Output isn't correct |
49 |
Correct |
95 ms |
9716 KB |
Output is correct |
50 |
Correct |
188 ms |
9704 KB |
Output is correct |
51 |
Incorrect |
125 ms |
11268 KB |
Output isn't correct |
52 |
Correct |
115 ms |
11276 KB |
Output is correct |
53 |
Correct |
202 ms |
11220 KB |
Output is correct |
54 |
Incorrect |
152 ms |
13032 KB |
Output isn't correct |
55 |
Correct |
134 ms |
12944 KB |
Output is correct |
56 |
Correct |
207 ms |
13012 KB |
Output is correct |
57 |
Incorrect |
162 ms |
14812 KB |
Output isn't correct |
58 |
Correct |
183 ms |
14884 KB |
Output is correct |
59 |
Correct |
251 ms |
14888 KB |
Output is correct |
60 |
Incorrect |
198 ms |
16784 KB |
Output isn't correct |
61 |
Correct |
176 ms |
16900 KB |
Output is correct |
62 |
Correct |
306 ms |
16844 KB |
Output is correct |
63 |
Correct |
281 ms |
16772 KB |
Output is correct |
64 |
Correct |
369 ms |
16844 KB |
Output is correct |
65 |
Correct |
395 ms |
16820 KB |
Output is correct |
66 |
Incorrect |
282 ms |
16860 KB |
Output isn't correct |
67 |
Incorrect |
300 ms |
16840 KB |
Output isn't correct |
68 |
Correct |
225 ms |
16796 KB |
Output is correct |
69 |
Correct |
219 ms |
16880 KB |
Output is correct |
70 |
Correct |
205 ms |
17004 KB |
Output is correct |
71 |
Correct |
219 ms |
16952 KB |
Output is correct |
72 |
Incorrect |
199 ms |
16884 KB |
Output isn't correct |
73 |
Incorrect |
236 ms |
16916 KB |
Output isn't correct |
74 |
Correct |
255 ms |
16852 KB |
Output is correct |
75 |
Correct |
237 ms |
17032 KB |
Output is correct |
76 |
Correct |
255 ms |
16836 KB |
Output is correct |
77 |
Correct |
226 ms |
17016 KB |
Output is correct |
78 |
Incorrect |
252 ms |
16844 KB |
Output isn't correct |
79 |
Correct |
236 ms |
16772 KB |
Output is correct |
80 |
Correct |
229 ms |
16916 KB |
Output is correct |
81 |
Correct |
306 ms |
16804 KB |
Output is correct |
82 |
Correct |
235 ms |
16852 KB |
Output is correct |
83 |
Correct |
265 ms |
16964 KB |
Output is correct |
84 |
Correct |
298 ms |
16836 KB |
Output is correct |
85 |
Correct |
251 ms |
16860 KB |
Output is correct |
86 |
Correct |
298 ms |
16812 KB |
Output is correct |
87 |
Correct |
252 ms |
16844 KB |
Output is correct |
88 |
Correct |
267 ms |
16928 KB |
Output is correct |
89 |
Correct |
331 ms |
16852 KB |
Output is correct |
90 |
Correct |
284 ms |
16972 KB |
Output is correct |
91 |
Correct |
367 ms |
16828 KB |
Output is correct |
92 |
Correct |
272 ms |
16876 KB |
Output is correct |