#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
///#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 8e2+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
int n, s, d[N][N], vis[N][N];
char a[N][N];
bool init(int x, int y){
return (x >= 1 && x <= n) && (y >= 1 && y <= n) && a[x][y] != 'T';
}
bool good(int time){
pi st, ed;
FOR(i, 1, n){
FOR(j, 1, n){
if(a[i][j] == 'M'){
st = make_pair(i,j);
vis[i][j] = time*s;
}
else{
if(a[i][j] == 'D'){
ed = make_pair(i, j);
}
vis[i][j] = n*n + time*s;
}
}
}
queue<pair<int,int>> q;
q.push(st);
while(sz(q)){
pi e = q.front(); q.pop();
int x = e.fi, y = e.se;
for(int i = 0; i < 4; i++){
int xx = x + dx[i];
int yy = y + dy[i];
if(init(xx, yy) && vis[xx][yy] == n*n + time*s && a[xx][yy] != 'H'){
int n_dist = (vis[x][y] + 1) / s + ((vis[x][y]+1) % s != 0);
if(n_dist <= d[xx][yy]){
vis[xx][yy] = vis[x][y] + 1;
q.push(make_pair(xx, yy));
}
}
}
}
return vis[ed.fi][ed.se] != n*n + time*s;
}
void solve()
{
cin >> n >> s;
FOR(i, 1, n){
FOR(j, 1, n){
cin >> a[i][j];
}
}
queue<pair<int,int>> q;
FOR(i, 1, n){
FOR(j, 1, n){
if(a[i][j] == 'H'){
q.push(make_pair(i,j));
}
else d[i][j] = n*n + n*n*s;
}
}
while(sz(q)){
pi e = q.front(); q.pop();
int x = e.fi, y = e.se;
for(int i = 0; i < 4; i++){
int xx = x + dx[i];
int yy = y + dy[i];
if(init(xx, yy) && d[xx][yy] == n*n && a[xx][yy] != 'D'){
d[xx][yy] = d[x][y] + 1;
q.push(make_pair(xx, yy));
}
}
}
int l = -1, r = n*n+1;
while(l+1 < r){
int m = l+r>>1;
if(good(m)){
l = m;
}
else r = m;
}
cout << l <<"\n";
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
Compilation message
mecho.cpp: In function 'void solve()':
mecho.cpp:114:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
114 | int m = l+r>>1;
| ~^~
mecho.cpp: In function 'int main()':
mecho.cpp:131:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:132:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | freopen(name".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
464 KB |
Output isn't correct |
7 |
Incorrect |
219 ms |
6612 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
11 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
15 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
18 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
22 |
Incorrect |
0 ms |
716 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
856 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
33 |
Incorrect |
24 ms |
3052 KB |
Output isn't correct |
34 |
Incorrect |
18 ms |
3076 KB |
Output isn't correct |
35 |
Incorrect |
27 ms |
2908 KB |
Output isn't correct |
36 |
Incorrect |
29 ms |
3416 KB |
Output isn't correct |
37 |
Incorrect |
25 ms |
3420 KB |
Output isn't correct |
38 |
Incorrect |
40 ms |
3420 KB |
Output isn't correct |
39 |
Incorrect |
39 ms |
3832 KB |
Output isn't correct |
40 |
Incorrect |
33 ms |
3676 KB |
Output isn't correct |
41 |
Incorrect |
47 ms |
3792 KB |
Output isn't correct |
42 |
Incorrect |
46 ms |
4188 KB |
Output isn't correct |
43 |
Incorrect |
37 ms |
4188 KB |
Output isn't correct |
44 |
Incorrect |
58 ms |
4440 KB |
Output isn't correct |
45 |
Incorrect |
59 ms |
4440 KB |
Output isn't correct |
46 |
Incorrect |
47 ms |
4444 KB |
Output isn't correct |
47 |
Incorrect |
75 ms |
4444 KB |
Output isn't correct |
48 |
Incorrect |
69 ms |
4956 KB |
Output isn't correct |
49 |
Incorrect |
56 ms |
4956 KB |
Output isn't correct |
50 |
Incorrect |
89 ms |
4956 KB |
Output isn't correct |
51 |
Incorrect |
84 ms |
5468 KB |
Output isn't correct |
52 |
Incorrect |
70 ms |
5464 KB |
Output isn't correct |
53 |
Incorrect |
101 ms |
5468 KB |
Output isn't correct |
54 |
Incorrect |
96 ms |
5720 KB |
Output isn't correct |
55 |
Incorrect |
78 ms |
5852 KB |
Output isn't correct |
56 |
Incorrect |
121 ms |
5724 KB |
Output isn't correct |
57 |
Incorrect |
116 ms |
6236 KB |
Output isn't correct |
58 |
Incorrect |
104 ms |
6236 KB |
Output isn't correct |
59 |
Incorrect |
143 ms |
6236 KB |
Output isn't correct |
60 |
Incorrect |
129 ms |
6764 KB |
Output isn't correct |
61 |
Incorrect |
102 ms |
6744 KB |
Output isn't correct |
62 |
Incorrect |
161 ms |
6716 KB |
Output isn't correct |
63 |
Incorrect |
254 ms |
6748 KB |
Output isn't correct |
64 |
Incorrect |
260 ms |
6748 KB |
Output isn't correct |
65 |
Incorrect |
258 ms |
6744 KB |
Output isn't correct |
66 |
Incorrect |
257 ms |
6744 KB |
Output isn't correct |
67 |
Incorrect |
293 ms |
6748 KB |
Output isn't correct |
68 |
Incorrect |
255 ms |
6744 KB |
Output isn't correct |
69 |
Incorrect |
267 ms |
6756 KB |
Output isn't correct |
70 |
Incorrect |
266 ms |
6744 KB |
Output isn't correct |
71 |
Incorrect |
276 ms |
6748 KB |
Output isn't correct |
72 |
Incorrect |
261 ms |
6752 KB |
Output isn't correct |
73 |
Incorrect |
248 ms |
6748 KB |
Output isn't correct |
74 |
Incorrect |
222 ms |
6772 KB |
Output isn't correct |
75 |
Incorrect |
217 ms |
6748 KB |
Output isn't correct |
76 |
Incorrect |
220 ms |
6748 KB |
Output isn't correct |
77 |
Incorrect |
227 ms |
6780 KB |
Output isn't correct |
78 |
Incorrect |
215 ms |
6744 KB |
Output isn't correct |
79 |
Incorrect |
218 ms |
6768 KB |
Output isn't correct |
80 |
Incorrect |
227 ms |
6760 KB |
Output isn't correct |
81 |
Incorrect |
215 ms |
7000 KB |
Output isn't correct |
82 |
Incorrect |
234 ms |
6744 KB |
Output isn't correct |
83 |
Incorrect |
221 ms |
6744 KB |
Output isn't correct |
84 |
Incorrect |
249 ms |
6760 KB |
Output isn't correct |
85 |
Incorrect |
231 ms |
6748 KB |
Output isn't correct |
86 |
Incorrect |
247 ms |
6748 KB |
Output isn't correct |
87 |
Incorrect |
224 ms |
6748 KB |
Output isn't correct |
88 |
Incorrect |
231 ms |
6748 KB |
Output isn't correct |
89 |
Incorrect |
220 ms |
6748 KB |
Output isn't correct |
90 |
Incorrect |
218 ms |
6752 KB |
Output isn't correct |
91 |
Incorrect |
224 ms |
6748 KB |
Output isn't correct |
92 |
Incorrect |
225 ms |
6752 KB |
Output isn't correct |