#include <bits/stdc++.h>
using namespace std;
#define ld long double
#define ll long long int
#define pi pair<int,int>
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n,s;cin >> n >> s;
vector<vector<char>> a(n + 1,vector<char>(n + 1));
queue<pair<int,int>> qu;
vector<vector<int>> bdist(n + 1,vector<int>(n + 1));
vector<vector<bool>> bvis(n + 1,vector<bool>(n + 1));
int si = -1,sj = -1,ei = -1,ej = -1;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin >> a[i][j];
if(a[i][j] == 'H'){
bvis[i][j] = true;
qu.push({i,j});
}
if(a[i][j] == 'M'){
si = i;
sj = j;
}
if(a[i][j] == 'D'){
ei = i;
ej = j;
}
}
}
while(sz(qu)){
int ni = qu.front().first,nj = qu.front().second;
//cout << ni << ' ' << nj << '\n';
qu.pop();
for(int i=-1;i<=1;i++) for(int j=-1;j<=1;j++){
if(abs(i) == abs(j)) continue;
int ii = ni + i,jj = nj + j;
if(ii < 1 || ii > n || jj < 1 || jj > n) continue;
if(bvis[ii][jj] || a[ii][jj] == 'T' || a[ii][jj] == 'D') continue;
bvis[ii][jj] = true;
bdist[ii][jj] = bdist[ni][nj] + 1;
qu.push({ii,jj});
}
}
/*
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout << (bdist[i][j] == 1e9 ? -1 : bdist[i][j]) << ' ';
}
cout << '\n';
}
cout << "---------------------------------------------\n";
*/
int ans = -1;
int l = 0,r = n * n;
while(l <= r){
int mid = (l + r) >> 1;
if(bdist[si][sj] < mid) continue;
//cout << mid << '\n';
vector<vector<int>> dist(n + 1,vector<int>(n + 1));
vector<vector<bool>> vis(n + 1,vector<bool>(n + 1));
queue<pair<int,int>> q;
q.push({si,sj});
vis[si][sj] = true;
bool ok = false;
while(sz(q)){
int ni = q.front().first,nj = q.front().second;
q.pop();
for(int i=-1;i<=1;i++) for(int j=-1;j<=1;j++){
if(abs(i) == abs(j)) continue;
int ii = ni + i,jj = nj + j;
if(ii < 1 || ii > n || jj < 1 || jj > n) continue;
if(vis[ii][jj]) continue;
if(a[ii][jj] == 'T' || a[ii][jj] == 'H') continue;
if(bdist[ii][jj] <= mid + (dist[ni][nj] + 1) / s) continue;
vis[ii][jj] = true;
dist[ii][jj] = dist[ni][nj] + 1;
q.push({ii,jj});
}
}
for(int i=-1;i<=1;i++) for(int j=-1;j<=1;j++){
if(abs(i) == abs(j)) continue;
int ii = ei + i,jj = ej + j;
if(ii < 1 || ii > n || jj < 1 || jj > n) continue;
if(a[ii][jj] == 'T' || a[ii][jj] == 'H') continue;
if(!vis[ii][jj]) continue;
if(dist[ii][jj] / s + mid >= bdist[ii][jj]) continue;
ok = true;
}
/*
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
cout << dist[i][j] << ' ';
}
cout << '\n';
}
*/
if(ok){
l = mid + 1;
ans = mid;
}
else r = mid - 1;
}
cout << ans;
/*
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout << bdist[i][j] << ' ';
}
cout << '\n';
}
*/
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1066 ms |
336 KB |
Time limit exceeded |
2 |
Execution timed out |
1057 ms |
336 KB |
Time limit exceeded |
3 |
Execution timed out |
1075 ms |
336 KB |
Time limit exceeded |
4 |
Execution timed out |
1043 ms |
336 KB |
Time limit exceeded |
5 |
Execution timed out |
1056 ms |
336 KB |
Time limit exceeded |
6 |
Execution timed out |
1068 ms |
336 KB |
Time limit exceeded |
7 |
Execution timed out |
1080 ms |
3664 KB |
Time limit exceeded |
8 |
Execution timed out |
1062 ms |
336 KB |
Time limit exceeded |
9 |
Execution timed out |
1054 ms |
336 KB |
Time limit exceeded |
10 |
Execution timed out |
1061 ms |
336 KB |
Time limit exceeded |
11 |
Execution timed out |
1062 ms |
336 KB |
Time limit exceeded |
12 |
Execution timed out |
1058 ms |
336 KB |
Time limit exceeded |
13 |
Execution timed out |
1066 ms |
336 KB |
Time limit exceeded |
14 |
Execution timed out |
1065 ms |
336 KB |
Time limit exceeded |
15 |
Execution timed out |
1071 ms |
336 KB |
Time limit exceeded |
16 |
Execution timed out |
1062 ms |
336 KB |
Time limit exceeded |
17 |
Execution timed out |
1047 ms |
336 KB |
Time limit exceeded |
18 |
Execution timed out |
1044 ms |
336 KB |
Time limit exceeded |
19 |
Execution timed out |
1050 ms |
336 KB |
Time limit exceeded |
20 |
Execution timed out |
1062 ms |
336 KB |
Time limit exceeded |
21 |
Execution timed out |
1061 ms |
336 KB |
Time limit exceeded |
22 |
Execution timed out |
1071 ms |
336 KB |
Time limit exceeded |
23 |
Execution timed out |
1074 ms |
336 KB |
Time limit exceeded |
24 |
Execution timed out |
1063 ms |
336 KB |
Time limit exceeded |
25 |
Execution timed out |
1037 ms |
336 KB |
Time limit exceeded |
26 |
Execution timed out |
1069 ms |
336 KB |
Time limit exceeded |
27 |
Execution timed out |
1066 ms |
336 KB |
Time limit exceeded |
28 |
Execution timed out |
1062 ms |
336 KB |
Time limit exceeded |
29 |
Execution timed out |
1062 ms |
336 KB |
Time limit exceeded |
30 |
Execution timed out |
1037 ms |
336 KB |
Time limit exceeded |
31 |
Execution timed out |
1071 ms |
336 KB |
Time limit exceeded |
32 |
Execution timed out |
1059 ms |
336 KB |
Time limit exceeded |
33 |
Execution timed out |
1060 ms |
1104 KB |
Time limit exceeded |
34 |
Execution timed out |
1072 ms |
1104 KB |
Time limit exceeded |
35 |
Execution timed out |
1045 ms |
1104 KB |
Time limit exceeded |
36 |
Execution timed out |
1062 ms |
1104 KB |
Time limit exceeded |
37 |
Execution timed out |
1066 ms |
1104 KB |
Time limit exceeded |
38 |
Execution timed out |
1053 ms |
1104 KB |
Time limit exceeded |
39 |
Execution timed out |
1044 ms |
1360 KB |
Time limit exceeded |
40 |
Execution timed out |
1057 ms |
1360 KB |
Time limit exceeded |
41 |
Execution timed out |
1049 ms |
1360 KB |
Time limit exceeded |
42 |
Execution timed out |
1060 ms |
1616 KB |
Time limit exceeded |
43 |
Execution timed out |
1048 ms |
1616 KB |
Time limit exceeded |
44 |
Execution timed out |
1059 ms |
1616 KB |
Time limit exceeded |
45 |
Execution timed out |
1053 ms |
1872 KB |
Time limit exceeded |
46 |
Execution timed out |
1061 ms |
1872 KB |
Time limit exceeded |
47 |
Execution timed out |
1075 ms |
1872 KB |
Time limit exceeded |
48 |
Execution timed out |
1053 ms |
2128 KB |
Time limit exceeded |
49 |
Execution timed out |
1045 ms |
2128 KB |
Time limit exceeded |
50 |
Execution timed out |
1076 ms |
2128 KB |
Time limit exceeded |
51 |
Execution timed out |
1052 ms |
2640 KB |
Time limit exceeded |
52 |
Execution timed out |
1053 ms |
2640 KB |
Time limit exceeded |
53 |
Execution timed out |
1074 ms |
2640 KB |
Time limit exceeded |
54 |
Execution timed out |
1056 ms |
2896 KB |
Time limit exceeded |
55 |
Execution timed out |
1074 ms |
2896 KB |
Time limit exceeded |
56 |
Execution timed out |
1067 ms |
2896 KB |
Time limit exceeded |
57 |
Execution timed out |
1071 ms |
3152 KB |
Time limit exceeded |
58 |
Execution timed out |
1062 ms |
3152 KB |
Time limit exceeded |
59 |
Execution timed out |
1070 ms |
3152 KB |
Time limit exceeded |
60 |
Execution timed out |
1070 ms |
3664 KB |
Time limit exceeded |
61 |
Execution timed out |
1071 ms |
3664 KB |
Time limit exceeded |
62 |
Execution timed out |
1054 ms |
3664 KB |
Time limit exceeded |
63 |
Execution timed out |
1025 ms |
3664 KB |
Time limit exceeded |
64 |
Execution timed out |
1073 ms |
3664 KB |
Time limit exceeded |
65 |
Execution timed out |
1057 ms |
3832 KB |
Time limit exceeded |
66 |
Execution timed out |
1065 ms |
3664 KB |
Time limit exceeded |
67 |
Execution timed out |
1061 ms |
3664 KB |
Time limit exceeded |
68 |
Execution timed out |
1048 ms |
3664 KB |
Time limit exceeded |
69 |
Execution timed out |
1073 ms |
3664 KB |
Time limit exceeded |
70 |
Execution timed out |
1053 ms |
3664 KB |
Time limit exceeded |
71 |
Execution timed out |
1043 ms |
3664 KB |
Time limit exceeded |
72 |
Execution timed out |
1053 ms |
3664 KB |
Time limit exceeded |
73 |
Execution timed out |
1058 ms |
3920 KB |
Time limit exceeded |
74 |
Execution timed out |
1077 ms |
4036 KB |
Time limit exceeded |
75 |
Execution timed out |
1070 ms |
3920 KB |
Time limit exceeded |
76 |
Execution timed out |
1060 ms |
3920 KB |
Time limit exceeded |
77 |
Execution timed out |
1071 ms |
3920 KB |
Time limit exceeded |
78 |
Execution timed out |
1060 ms |
3920 KB |
Time limit exceeded |
79 |
Execution timed out |
1045 ms |
3920 KB |
Time limit exceeded |
80 |
Execution timed out |
1070 ms |
3920 KB |
Time limit exceeded |
81 |
Execution timed out |
1062 ms |
3920 KB |
Time limit exceeded |
82 |
Execution timed out |
1053 ms |
3920 KB |
Time limit exceeded |
83 |
Execution timed out |
1075 ms |
3920 KB |
Time limit exceeded |
84 |
Execution timed out |
1090 ms |
3792 KB |
Time limit exceeded |
85 |
Execution timed out |
1051 ms |
3936 KB |
Time limit exceeded |
86 |
Execution timed out |
1068 ms |
3920 KB |
Time limit exceeded |
87 |
Execution timed out |
1061 ms |
4176 KB |
Time limit exceeded |
88 |
Execution timed out |
1054 ms |
3920 KB |
Time limit exceeded |
89 |
Execution timed out |
1073 ms |
3776 KB |
Time limit exceeded |
90 |
Execution timed out |
1088 ms |
3896 KB |
Time limit exceeded |
91 |
Execution timed out |
1057 ms |
3920 KB |
Time limit exceeded |
92 |
Execution timed out |
1094 ms |
3784 KB |
Time limit exceeded |