// Calm down.
// Think three times, code twice.
#include "bits/stdc++.h"
#define forr(_a,_b,_c) for(int _a = (_b); _a <= (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
#define st first
#define nd second
#define ll long long
#define ull unsigned long long
#define pii pair <ll,ll>
#define pll pair <ll,ll>
#define piii pair <ll,pii>
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7; // 998244353
const ll oo = 1e18;
pii bd, en;
ll n, s, dx[] {1, -1, 0, 0}, dy[] {0, 0, 1, -1}, d[1005][1005], d1[1005][1005];
char a[1005][1005];
queue<piii> q;
bool check(ll x, ll y) {
if(x > n || x < 1 || y > n || y < 1 || a[x][y] == 'T') return false;
return true;
}
bool kt(ll mid) {
memset(d1, 0, sizeof d1);
q.push({mid * s, {bd.st, bd.nd}});
while(q.size()) {
ll dxy = q.front().st, x = q.front().nd.st, y = q.front().nd.nd; q.pop();
if(d1[x][y]) continue;
d1[x][y] = 1;
forr(i, 0, 3) {
ll nx = x + dx[i], ny = y + dy[i];
if(!check(nx, ny)) continue;
if((dxy + 1) / s < d[nx][ny]) {
// cout << nx << " " << ny << " " << dxy + 1 << " " << d[nx][ny] << " " << (dxy + 1) / s + ((dxy + 1) % s != 0) << "\n";
q.push({dxy + 1, {nx, ny}});
}
}
}
return d1[en.st][en.nd];
}
void to_nho_cau() {
cin >> n >> s;
memset(d, 63, sizeof d);
forr(i, 1, n) {
forr(j, 1, n) {
cin >> a[i][j];
if(a[i][j] == 'H') {
d[i][j] = 0;
q.push({0, {i, j}});
}
if(a[i][j] == 'M') bd = {i, j};
if(a[i][j] == 'D') en = {i, j};
}
}
while(q.size()) {
ll dxy = q.front().st, x = q.front().nd.st, y = q.front().nd.nd; q.pop();
if(d[x][y] < dxy) continue;
forr(i, 0, 3) {
ll nx = x + dx[i], ny = y + dy[i];
if(!check(nx, ny) || a[nx][ny] == 'D') continue;
if(dxy + 1 < d[nx][ny]) {
d[nx][ny] = dxy + 1;
q.push({dxy + 1, {nx, ny}});
}
}
}
ll l = 0, r = mod, ans = -1;
while(l <= r) {
ll mid = (l + r) >> 1;
if(kt(mid)) {
ans = mid;
l = mid + 1;
} else r = mid - 1;
}
cout << ans << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
// #ifdef LOCAL
// freopen(file".inp","r",stdin);
// freopen(file".out","w",stdout);
// #endif
int t = 1;
//cin >> t;
while(t--) to_nho_cau();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16984 KB |
Output is correct |
2 |
Correct |
7 ms |
16988 KB |
Output is correct |
3 |
Correct |
7 ms |
16988 KB |
Output is correct |
4 |
Correct |
6 ms |
16856 KB |
Output is correct |
5 |
Correct |
6 ms |
16984 KB |
Output is correct |
6 |
Correct |
7 ms |
16988 KB |
Output is correct |
7 |
Correct |
145 ms |
18008 KB |
Output is correct |
8 |
Correct |
7 ms |
16984 KB |
Output is correct |
9 |
Correct |
7 ms |
16988 KB |
Output is correct |
10 |
Correct |
7 ms |
16984 KB |
Output is correct |
11 |
Correct |
7 ms |
16988 KB |
Output is correct |
12 |
Incorrect |
7 ms |
17152 KB |
Output isn't correct |
13 |
Incorrect |
7 ms |
16988 KB |
Output isn't correct |
14 |
Correct |
8 ms |
17100 KB |
Output is correct |
15 |
Correct |
7 ms |
17360 KB |
Output is correct |
16 |
Correct |
7 ms |
16984 KB |
Output is correct |
17 |
Correct |
7 ms |
16988 KB |
Output is correct |
18 |
Correct |
8 ms |
16984 KB |
Output is correct |
19 |
Correct |
7 ms |
16984 KB |
Output is correct |
20 |
Correct |
7 ms |
17108 KB |
Output is correct |
21 |
Correct |
7 ms |
17240 KB |
Output is correct |
22 |
Correct |
7 ms |
16984 KB |
Output is correct |
23 |
Correct |
7 ms |
16988 KB |
Output is correct |
24 |
Correct |
8 ms |
17140 KB |
Output is correct |
25 |
Correct |
8 ms |
16988 KB |
Output is correct |
26 |
Correct |
7 ms |
16988 KB |
Output is correct |
27 |
Correct |
8 ms |
16988 KB |
Output is correct |
28 |
Correct |
8 ms |
16988 KB |
Output is correct |
29 |
Correct |
7 ms |
16988 KB |
Output is correct |
30 |
Correct |
7 ms |
16988 KB |
Output is correct |
31 |
Correct |
7 ms |
16988 KB |
Output is correct |
32 |
Correct |
7 ms |
17104 KB |
Output is correct |
33 |
Correct |
12 ms |
17244 KB |
Output is correct |
34 |
Correct |
11 ms |
17244 KB |
Output is correct |
35 |
Correct |
36 ms |
17376 KB |
Output is correct |
36 |
Correct |
10 ms |
17240 KB |
Output is correct |
37 |
Correct |
14 ms |
17420 KB |
Output is correct |
38 |
Correct |
46 ms |
17244 KB |
Output is correct |
39 |
Correct |
11 ms |
17240 KB |
Output is correct |
40 |
Correct |
11 ms |
17244 KB |
Output is correct |
41 |
Correct |
55 ms |
17476 KB |
Output is correct |
42 |
Correct |
13 ms |
17496 KB |
Output is correct |
43 |
Correct |
12 ms |
17500 KB |
Output is correct |
44 |
Correct |
78 ms |
17520 KB |
Output is correct |
45 |
Correct |
16 ms |
17544 KB |
Output is correct |
46 |
Correct |
14 ms |
17500 KB |
Output is correct |
47 |
Correct |
90 ms |
17580 KB |
Output is correct |
48 |
Correct |
16 ms |
17500 KB |
Output is correct |
49 |
Correct |
15 ms |
17612 KB |
Output is correct |
50 |
Correct |
101 ms |
17500 KB |
Output is correct |
51 |
Correct |
18 ms |
17496 KB |
Output is correct |
52 |
Correct |
21 ms |
17500 KB |
Output is correct |
53 |
Correct |
120 ms |
17496 KB |
Output is correct |
54 |
Correct |
19 ms |
17488 KB |
Output is correct |
55 |
Correct |
20 ms |
17500 KB |
Output is correct |
56 |
Correct |
138 ms |
17752 KB |
Output is correct |
57 |
Correct |
19 ms |
17800 KB |
Output is correct |
58 |
Correct |
16 ms |
17756 KB |
Output is correct |
59 |
Correct |
155 ms |
17752 KB |
Output is correct |
60 |
Correct |
20 ms |
17756 KB |
Output is correct |
61 |
Correct |
19 ms |
17752 KB |
Output is correct |
62 |
Correct |
178 ms |
17756 KB |
Output is correct |
63 |
Correct |
96 ms |
17744 KB |
Output is correct |
64 |
Correct |
168 ms |
17884 KB |
Output is correct |
65 |
Correct |
162 ms |
17744 KB |
Output is correct |
66 |
Correct |
110 ms |
17752 KB |
Output is correct |
67 |
Correct |
101 ms |
17872 KB |
Output is correct |
68 |
Correct |
44 ms |
17744 KB |
Output is correct |
69 |
Correct |
43 ms |
17756 KB |
Output is correct |
70 |
Correct |
29 ms |
18096 KB |
Output is correct |
71 |
Correct |
31 ms |
17748 KB |
Output is correct |
72 |
Incorrect |
28 ms |
17876 KB |
Output isn't correct |
73 |
Incorrect |
36 ms |
18444 KB |
Output isn't correct |
74 |
Correct |
85 ms |
18512 KB |
Output is correct |
75 |
Correct |
110 ms |
18584 KB |
Output is correct |
76 |
Correct |
89 ms |
18516 KB |
Output is correct |
77 |
Correct |
91 ms |
18524 KB |
Output is correct |
78 |
Correct |
112 ms |
18516 KB |
Output is correct |
79 |
Correct |
84 ms |
18516 KB |
Output is correct |
80 |
Correct |
115 ms |
18608 KB |
Output is correct |
81 |
Correct |
107 ms |
18520 KB |
Output is correct |
82 |
Correct |
83 ms |
18612 KB |
Output is correct |
83 |
Correct |
128 ms |
18244 KB |
Output is correct |
84 |
Correct |
108 ms |
18260 KB |
Output is correct |
85 |
Correct |
136 ms |
18228 KB |
Output is correct |
86 |
Correct |
116 ms |
18236 KB |
Output is correct |
87 |
Correct |
111 ms |
18488 KB |
Output is correct |
88 |
Correct |
133 ms |
18268 KB |
Output is correct |
89 |
Correct |
131 ms |
18348 KB |
Output is correct |
90 |
Correct |
145 ms |
18264 KB |
Output is correct |
91 |
Correct |
126 ms |
18256 KB |
Output is correct |
92 |
Correct |
140 ms |
18104 KB |
Output is correct |