#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
using ll = long long;
using ii = pair<ll, ll>;
const int N = 1e6+5;
const int INF = 1e9;
ll d[1000][1000],m[1000][1000],n,s;
char a[1000][1000];
ll dx[4]={1,0,0,-1};
ll dy[4]={0,1,-1,0};
ii pos,h;
bool bfs(ll x) {
if (d[pos.fi][pos.se] - x * s <= 0) return 0;
queue<ii> q;
q.push(pos);
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
m[i][j]=0;
}
}
while(!q.empty()) {
ii u=q.front();
q.pop();
for(int i=0;i<4;i++) {
ii v={u.fi+dx[i],u.se+dy[i]};
if(v.fi<0 || v.fi>=n || v.se<0 || v.se>=n) continue;
if(m[v.fi][v.se]==0 && a[v.fi][v.se]!='T' &&
d[v.fi][v.se]-x*s>m[u.fi][u.se]+1 && a[v.fi][v.se]!='M') {
m[v.fi][v.se]=m[u.fi][u.se]+1;
q.push(v);
}
}
}
if(m[h.fi][h.se]>0) return 1; else return 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
queue<ii> q;
cin >> n >> s;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
cin >> a[i][j];
if(a[i][j]=='H') {
q.push({i,j});
}
if(a[i][j]=='M') pos={i,j};
if(a[i][j]=='D') h={i,j};
}
}
while(!q.empty()) {
ii u=q.front();
q.pop();
for(int i=0;i<4;i++) {
ii v={u.fi+dx[i],u.se+dy[i]};
if(v.fi<0 || v.fi>=n || v.se<0 || v.se>=n) continue;
if(d[v.fi][v.se]==0 && a[v.fi][v.se]!='T' && a[v.fi][v.se]!='H' && a[v.fi][v.se]!='D') {
d[v.fi][v.se]=d[u.fi][u.se]+s;
q.push(v);
}
}
}
d[h.fi][h.se] = (int)1e18;
ll l=1,r=1,ans=-1;
while(l<=r) {
ll m=(l+r)/2;
if(bfs(m)) {
ans=m;
l=m+1;
}
else {
r=m-1;
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Incorrect |
25 ms |
15196 KB |
Output isn't correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
16 |
Incorrect |
2 ms |
2396 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
2916 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
33 |
Incorrect |
4 ms |
6492 KB |
Output isn't correct |
34 |
Incorrect |
5 ms |
6748 KB |
Output isn't correct |
35 |
Incorrect |
8 ms |
7128 KB |
Output isn't correct |
36 |
Incorrect |
5 ms |
6748 KB |
Output isn't correct |
37 |
Incorrect |
8 ms |
7260 KB |
Output isn't correct |
38 |
Incorrect |
7 ms |
7516 KB |
Output isn't correct |
39 |
Incorrect |
6 ms |
9308 KB |
Output isn't correct |
40 |
Incorrect |
6 ms |
9564 KB |
Output isn't correct |
41 |
Incorrect |
13 ms |
10076 KB |
Output isn't correct |
42 |
Incorrect |
7 ms |
9612 KB |
Output isn't correct |
43 |
Incorrect |
8 ms |
10076 KB |
Output isn't correct |
44 |
Incorrect |
12 ms |
10588 KB |
Output isn't correct |
45 |
Incorrect |
8 ms |
9820 KB |
Output isn't correct |
46 |
Incorrect |
9 ms |
10588 KB |
Output isn't correct |
47 |
Incorrect |
17 ms |
11100 KB |
Output isn't correct |
48 |
Incorrect |
10 ms |
10332 KB |
Output isn't correct |
49 |
Incorrect |
11 ms |
10840 KB |
Output isn't correct |
50 |
Incorrect |
14 ms |
11352 KB |
Output isn't correct |
51 |
Incorrect |
11 ms |
10452 KB |
Output isn't correct |
52 |
Incorrect |
12 ms |
11100 KB |
Output isn't correct |
53 |
Incorrect |
16 ms |
11868 KB |
Output isn't correct |
54 |
Incorrect |
13 ms |
12892 KB |
Output isn't correct |
55 |
Incorrect |
14 ms |
13660 KB |
Output isn't correct |
56 |
Incorrect |
19 ms |
14348 KB |
Output isn't correct |
57 |
Incorrect |
15 ms |
13148 KB |
Output isn't correct |
58 |
Incorrect |
15 ms |
13864 KB |
Output isn't correct |
59 |
Incorrect |
23 ms |
14684 KB |
Output isn't correct |
60 |
Incorrect |
16 ms |
13400 KB |
Output isn't correct |
61 |
Incorrect |
18 ms |
14172 KB |
Output isn't correct |
62 |
Incorrect |
23 ms |
15064 KB |
Output isn't correct |
63 |
Incorrect |
28 ms |
14936 KB |
Output isn't correct |
64 |
Incorrect |
36 ms |
14928 KB |
Output isn't correct |
65 |
Incorrect |
35 ms |
14940 KB |
Output isn't correct |
66 |
Incorrect |
39 ms |
15124 KB |
Output isn't correct |
67 |
Correct |
28 ms |
15140 KB |
Output is correct |
68 |
Incorrect |
28 ms |
14936 KB |
Output isn't correct |
69 |
Incorrect |
34 ms |
14936 KB |
Output isn't correct |
70 |
Incorrect |
33 ms |
15148 KB |
Output isn't correct |
71 |
Incorrect |
25 ms |
14940 KB |
Output isn't correct |
72 |
Incorrect |
27 ms |
14940 KB |
Output isn't correct |
73 |
Correct |
24 ms |
15452 KB |
Output is correct |
74 |
Incorrect |
33 ms |
15672 KB |
Output isn't correct |
75 |
Incorrect |
24 ms |
15400 KB |
Output isn't correct |
76 |
Incorrect |
26 ms |
15512 KB |
Output isn't correct |
77 |
Incorrect |
25 ms |
15448 KB |
Output isn't correct |
78 |
Correct |
26 ms |
15584 KB |
Output is correct |
79 |
Incorrect |
32 ms |
15444 KB |
Output isn't correct |
80 |
Incorrect |
30 ms |
15532 KB |
Output isn't correct |
81 |
Incorrect |
24 ms |
15540 KB |
Output isn't correct |
82 |
Incorrect |
25 ms |
15444 KB |
Output isn't correct |
83 |
Incorrect |
24 ms |
15532 KB |
Output isn't correct |
84 |
Incorrect |
31 ms |
15500 KB |
Output isn't correct |
85 |
Incorrect |
26 ms |
15552 KB |
Output isn't correct |
86 |
Incorrect |
27 ms |
15416 KB |
Output isn't correct |
87 |
Incorrect |
24 ms |
15452 KB |
Output isn't correct |
88 |
Incorrect |
24 ms |
15196 KB |
Output isn't correct |
89 |
Incorrect |
31 ms |
15196 KB |
Output isn't correct |
90 |
Incorrect |
29 ms |
15180 KB |
Output isn't correct |
91 |
Incorrect |
28 ms |
15196 KB |
Output isn't correct |
92 |
Incorrect |
27 ms |
15404 KB |
Output isn't correct |