#include <bits/stdc++.h>
using namespace std;
int mat[1000][1000];
int tajm[1000][1000];
int dist[1000][1000];
int si, sj;
int n, s;
const int INF = 1000000007;
bool moze(int dis, int i, int j, int t){
if(i < 1 || i > n || j < 1 || j > n) return 0;
if(mat[i][j] == -1) return 0;
dis++;
if(dis/s+t >= tajm[i][j]) return 0;
if(dis >= dist[i][j]) return 0;
dist[i][j] = dis;
return 1;
}
bool bfs(int t){
queue <pair <int, int>> q;
q.push({si, sj});
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
dist[i][j] = INF;
}
}
dist[si][sj] = 0;
while(!q.empty()){
int i = q.front().first;
int j = q.front().second;
int dis = dist[i][j];
q.pop();
if(mat[i][j] == 2) return 1;
if(moze(dis, i-1, j, t)) q.push({i-1, j});
if(moze(dis, i+1, j, t)) q.push({i+1, j});
if(moze(dis, i, j-1, t)) q.push({i, j-1});
if(moze(dis, i, j+1, t)) q.push({i, j+1});
}
return 0;
}
int main(){
cin >> n >> s;
queue <pair <int, int>> q;
for(int i=1; i<=n; i++){
string str;
cin >> str;
for(int j=0; j<n; j++){
tajm[i][j+1] = INF;
if(str[j] == 'T') mat[i][j+1] = -1;
if(str[j] == 'H'){mat[i][j+1] = -2; q.push({i, j+1}); tajm[i][j+1] = 0;}
if(str[j] == 'M'){mat[i][j+1] = 1; si = i; sj = j+1;}
if(str[j] == 'D') mat[i][j+1] = 2;
}
}
while(!q.empty()){
int i = q.front().first;
int j = q.front().second;
q.pop();
int tren = tajm[i][j];
if(mat[i-1][j] > -1 && mat[i-1][j] < 2 && tajm[i-1][j] > tren+1){tajm[i-1][j] = tren+1; q.push({i-1, j});}
if(mat[i+1][j] > -1 && mat[i+1][j] < 2 && tajm[i+1][j] > tren+1){tajm[i+1][j] = tren+1; q.push({i+1, j});}
if(mat[i][j-1] > -1 && mat[i][j-1] < 2 && tajm[i][j-1] > tren+1){tajm[i][j-1] = tren+1; q.push({i, j-1});}
if(mat[i][j+1] > -1 && mat[i][j+1] < 2 && tajm[i][j+1] > tren+1){tajm[i][j+1] = tren+1; q.push({i, j+1});}
}
int maxres=-1;
int l=0, r = tajm[si][sj]-1;
while(l <= r){
int mid = (l+r)/2;
if(bfs(mid)){maxres = mid; l = mid+1;}
else r = mid-1;
}
cout << maxres;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
137 ms |
9156 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
972 KB |
Output is correct |
13 |
Correct |
1 ms |
844 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
688 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
1 ms |
716 KB |
Output is correct |
24 |
Correct |
1 ms |
844 KB |
Output is correct |
25 |
Correct |
1 ms |
844 KB |
Output is correct |
26 |
Correct |
1 ms |
972 KB |
Output is correct |
27 |
Correct |
1 ms |
972 KB |
Output is correct |
28 |
Correct |
1 ms |
972 KB |
Output is correct |
29 |
Correct |
1 ms |
972 KB |
Output is correct |
30 |
Correct |
1 ms |
1100 KB |
Output is correct |
31 |
Correct |
1 ms |
972 KB |
Output is correct |
32 |
Correct |
1 ms |
1100 KB |
Output is correct |
33 |
Correct |
8 ms |
4064 KB |
Output is correct |
34 |
Correct |
8 ms |
4428 KB |
Output is correct |
35 |
Correct |
26 ms |
4428 KB |
Output is correct |
36 |
Correct |
10 ms |
4684 KB |
Output is correct |
37 |
Correct |
10 ms |
5080 KB |
Output is correct |
38 |
Correct |
35 ms |
5128 KB |
Output is correct |
39 |
Correct |
12 ms |
5324 KB |
Output is correct |
40 |
Correct |
12 ms |
5692 KB |
Output is correct |
41 |
Correct |
46 ms |
5708 KB |
Output is correct |
42 |
Correct |
14 ms |
5964 KB |
Output is correct |
43 |
Correct |
14 ms |
6348 KB |
Output is correct |
44 |
Correct |
58 ms |
6412 KB |
Output is correct |
45 |
Correct |
17 ms |
6604 KB |
Output is correct |
46 |
Correct |
17 ms |
6960 KB |
Output is correct |
47 |
Correct |
73 ms |
6988 KB |
Output is correct |
48 |
Correct |
20 ms |
7212 KB |
Output is correct |
49 |
Correct |
19 ms |
7668 KB |
Output is correct |
50 |
Correct |
88 ms |
7700 KB |
Output is correct |
51 |
Correct |
23 ms |
7884 KB |
Output is correct |
52 |
Correct |
23 ms |
8268 KB |
Output is correct |
53 |
Correct |
109 ms |
8340 KB |
Output is correct |
54 |
Correct |
26 ms |
8608 KB |
Output is correct |
55 |
Correct |
25 ms |
8900 KB |
Output is correct |
56 |
Correct |
128 ms |
8908 KB |
Output is correct |
57 |
Correct |
29 ms |
9292 KB |
Output is correct |
58 |
Correct |
31 ms |
9668 KB |
Output is correct |
59 |
Correct |
165 ms |
9656 KB |
Output is correct |
60 |
Correct |
33 ms |
9924 KB |
Output is correct |
61 |
Correct |
32 ms |
10308 KB |
Output is correct |
62 |
Correct |
178 ms |
10312 KB |
Output is correct |
63 |
Correct |
135 ms |
10308 KB |
Output is correct |
64 |
Correct |
225 ms |
10292 KB |
Output is correct |
65 |
Correct |
216 ms |
10312 KB |
Output is correct |
66 |
Correct |
188 ms |
10308 KB |
Output is correct |
67 |
Correct |
150 ms |
10312 KB |
Output is correct |
68 |
Correct |
66 ms |
10284 KB |
Output is correct |
69 |
Correct |
62 ms |
10336 KB |
Output is correct |
70 |
Correct |
56 ms |
10280 KB |
Output is correct |
71 |
Correct |
49 ms |
10284 KB |
Output is correct |
72 |
Correct |
48 ms |
10284 KB |
Output is correct |
73 |
Correct |
62 ms |
10604 KB |
Output is correct |
74 |
Correct |
88 ms |
10564 KB |
Output is correct |
75 |
Correct |
88 ms |
10528 KB |
Output is correct |
76 |
Correct |
91 ms |
10544 KB |
Output is correct |
77 |
Correct |
95 ms |
10588 KB |
Output is correct |
78 |
Correct |
102 ms |
10564 KB |
Output is correct |
79 |
Correct |
82 ms |
10572 KB |
Output is correct |
80 |
Correct |
91 ms |
10508 KB |
Output is correct |
81 |
Correct |
95 ms |
10568 KB |
Output is correct |
82 |
Correct |
84 ms |
10564 KB |
Output is correct |
83 |
Correct |
124 ms |
10528 KB |
Output is correct |
84 |
Correct |
104 ms |
10464 KB |
Output is correct |
85 |
Correct |
105 ms |
10480 KB |
Output is correct |
86 |
Correct |
121 ms |
10464 KB |
Output is correct |
87 |
Correct |
103 ms |
10440 KB |
Output is correct |
88 |
Correct |
134 ms |
10428 KB |
Output is correct |
89 |
Correct |
131 ms |
10452 KB |
Output is correct |
90 |
Correct |
145 ms |
10444 KB |
Output is correct |
91 |
Correct |
132 ms |
10476 KB |
Output is correct |
92 |
Correct |
131 ms |
10460 KB |
Output is correct |