#include <bits/stdc++.h>
#define MAX 850
std::string mapa[MAX];
typedef std::pair<int,int> pii;
bool proibido[MAX][MAX]={},nobees[MAX][MAX]={};
int N,K;
typedef std::pair<int,pii> pip;
std::vector<pii> hon;
void avancar(void) {
std::vector<pii> honfut;
for(auto&x:hon) {
#define PUSH(A,B) if(A>=0&&B>=0&&A<N&&B<N&&!proibido[A][B]&&!nobees[A][B]){proibido[A][B]=true;honfut.push_back({A,B});}
PUSH(x.first+1,x.second);
PUSH(x.first-1,x.second);
PUSH(x.first,x.second+1);
PUSH(x.first,x.second-1);
}
hon=honfut;
}
bool vis[MAX][MAX]={};
bool testar(int x,int s) {
hon.clear();
memset(&proibido[0][0],0,sizeof(proibido));
memset(&nobees[0][0],0,sizeof(nobees));
memset(&vis[0][0],0,sizeof(vis));
pii inicio,fim;
for(int i=0;i!=N;++i){
for(int j=0;j!=N;++j){
if(mapa[i][j]=='H'){
proibido[i][j]=true;
hon.push_back({i,j});
}else if(mapa[i][j]=='M') {
inicio={i,j};
}else if(mapa[i][j]=='D') {
nobees[i][j]=true;
fim={i,j};
}else if(mapa[i][j]=='T'){
nobees[i][j]=true;
proibido[i][j]=true;
}
}
}
for(int i=0;i!=x;++i)avancar();
std::queue<pip> queue;
queue.push({0,inicio});
int laste=0;
while(queue.size()) {
auto _ = queue.front();
queue.pop();
int mov = _.first/s;
if(mov>laste){
laste=mov;
avancar();
}
if(vis[_.second.first][_.second.second]||proibido[_.second.first][_.second.second])continue;
vis[_.second.first][_.second.second]=true;
if(_.second==fim)return true;
#define QPUSH(A,B) if(A>=0&&B>=0&&A<N&&B<N)queue.push({_.first+1,{A,B}});
QPUSH(_.second.first+1,_.second.second);
QPUSH(_.second.first-1,_.second.second);
QPUSH(_.second.first,_.second.second+1);
QPUSH(_.second.first,_.second.second-1);
}
return false;
}
int main()
{
std::cin>>N>>K;
for(int i=0;i!=N;++i)std::cin>>mapa[i];
if(!testar(0,K)) {
std::cout<<-1<<"\n";
return 0;
}
int l=0,r=N*N;
while(l<r) {
int m = (l+r)/2 + ((l+r)&1);
if(testar(m,K)) {
l=m;
}else r=m-1;
}
std::cout<<l<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2380 KB |
Output is correct |
2 |
Correct |
2 ms |
2380 KB |
Output is correct |
3 |
Correct |
2 ms |
2344 KB |
Output is correct |
4 |
Correct |
2 ms |
2380 KB |
Output is correct |
5 |
Correct |
2 ms |
2380 KB |
Output is correct |
6 |
Correct |
2 ms |
2380 KB |
Output is correct |
7 |
Correct |
411 ms |
3788 KB |
Output is correct |
8 |
Correct |
1 ms |
2380 KB |
Output is correct |
9 |
Correct |
2 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2380 KB |
Output is correct |
11 |
Correct |
2 ms |
2380 KB |
Output is correct |
12 |
Correct |
3 ms |
2380 KB |
Output is correct |
13 |
Correct |
3 ms |
2380 KB |
Output is correct |
14 |
Correct |
2 ms |
2380 KB |
Output is correct |
15 |
Correct |
2 ms |
2380 KB |
Output is correct |
16 |
Correct |
3 ms |
2380 KB |
Output is correct |
17 |
Correct |
2 ms |
2380 KB |
Output is correct |
18 |
Correct |
2 ms |
2380 KB |
Output is correct |
19 |
Correct |
3 ms |
2380 KB |
Output is correct |
20 |
Correct |
3 ms |
2380 KB |
Output is correct |
21 |
Correct |
3 ms |
2380 KB |
Output is correct |
22 |
Correct |
3 ms |
2380 KB |
Output is correct |
23 |
Correct |
3 ms |
2380 KB |
Output is correct |
24 |
Correct |
3 ms |
2380 KB |
Output is correct |
25 |
Correct |
4 ms |
2400 KB |
Output is correct |
26 |
Correct |
3 ms |
2380 KB |
Output is correct |
27 |
Correct |
4 ms |
2380 KB |
Output is correct |
28 |
Correct |
4 ms |
2380 KB |
Output is correct |
29 |
Correct |
5 ms |
2380 KB |
Output is correct |
30 |
Correct |
4 ms |
2380 KB |
Output is correct |
31 |
Correct |
4 ms |
2380 KB |
Output is correct |
32 |
Correct |
4 ms |
2444 KB |
Output is correct |
33 |
Correct |
49 ms |
2508 KB |
Output is correct |
34 |
Correct |
46 ms |
2508 KB |
Output is correct |
35 |
Correct |
81 ms |
2640 KB |
Output is correct |
36 |
Correct |
77 ms |
2632 KB |
Output is correct |
37 |
Correct |
65 ms |
2508 KB |
Output is correct |
38 |
Correct |
102 ms |
2668 KB |
Output is correct |
39 |
Correct |
81 ms |
2640 KB |
Output is correct |
40 |
Correct |
87 ms |
2652 KB |
Output is correct |
41 |
Correct |
130 ms |
2700 KB |
Output is correct |
42 |
Correct |
130 ms |
2900 KB |
Output is correct |
43 |
Correct |
108 ms |
2896 KB |
Output is correct |
44 |
Correct |
158 ms |
3056 KB |
Output is correct |
45 |
Correct |
132 ms |
2948 KB |
Output is correct |
46 |
Correct |
148 ms |
2948 KB |
Output is correct |
47 |
Correct |
185 ms |
3004 KB |
Output is correct |
48 |
Correct |
144 ms |
3012 KB |
Output is correct |
49 |
Correct |
161 ms |
3004 KB |
Output is correct |
50 |
Correct |
235 ms |
3056 KB |
Output is correct |
51 |
Correct |
176 ms |
3020 KB |
Output is correct |
52 |
Correct |
178 ms |
3020 KB |
Output is correct |
53 |
Correct |
275 ms |
3112 KB |
Output is correct |
54 |
Correct |
218 ms |
3100 KB |
Output is correct |
55 |
Correct |
203 ms |
3084 KB |
Output is correct |
56 |
Correct |
310 ms |
3148 KB |
Output is correct |
57 |
Correct |
303 ms |
3132 KB |
Output is correct |
58 |
Correct |
232 ms |
3136 KB |
Output is correct |
59 |
Correct |
371 ms |
3196 KB |
Output is correct |
60 |
Correct |
273 ms |
3148 KB |
Output is correct |
61 |
Correct |
283 ms |
3180 KB |
Output is correct |
62 |
Correct |
418 ms |
3264 KB |
Output is correct |
63 |
Correct |
409 ms |
3204 KB |
Output is correct |
64 |
Correct |
554 ms |
3128 KB |
Output is correct |
65 |
Correct |
471 ms |
3320 KB |
Output is correct |
66 |
Correct |
460 ms |
3204 KB |
Output is correct |
67 |
Correct |
44 ms |
3148 KB |
Output is correct |
68 |
Correct |
331 ms |
3328 KB |
Output is correct |
69 |
Correct |
295 ms |
3396 KB |
Output is correct |
70 |
Correct |
331 ms |
3276 KB |
Output is correct |
71 |
Correct |
320 ms |
3276 KB |
Output is correct |
72 |
Correct |
319 ms |
3400 KB |
Output is correct |
73 |
Correct |
254 ms |
4476 KB |
Output is correct |
74 |
Correct |
374 ms |
4460 KB |
Output is correct |
75 |
Correct |
397 ms |
4472 KB |
Output is correct |
76 |
Correct |
417 ms |
4480 KB |
Output is correct |
77 |
Correct |
393 ms |
4480 KB |
Output is correct |
78 |
Correct |
50 ms |
4352 KB |
Output is correct |
79 |
Correct |
375 ms |
4136 KB |
Output is correct |
80 |
Correct |
370 ms |
4376 KB |
Output is correct |
81 |
Correct |
394 ms |
4332 KB |
Output is correct |
82 |
Correct |
373 ms |
4280 KB |
Output is correct |
83 |
Correct |
414 ms |
4264 KB |
Output is correct |
84 |
Correct |
437 ms |
4092 KB |
Output is correct |
85 |
Correct |
413 ms |
4284 KB |
Output is correct |
86 |
Correct |
421 ms |
4260 KB |
Output is correct |
87 |
Correct |
405 ms |
4272 KB |
Output is correct |
88 |
Correct |
403 ms |
3984 KB |
Output is correct |
89 |
Correct |
441 ms |
4068 KB |
Output is correct |
90 |
Correct |
458 ms |
4004 KB |
Output is correct |
91 |
Correct |
442 ms |
4080 KB |
Output is correct |
92 |
Correct |
403 ms |
3984 KB |
Output is correct |