#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long llo;
#define endl '\n'
int n,s;
queue<pair<int,int>> ss;
int dist[801][801];
int it[801][801];
vector<int> x={1,-1,0,0};
vector<int> y={0,0,1,-1};
int dd[801][801];
pair<int,int> aa;
pair<int,int> bb;
bool check(int mid){
queue<pair<int,int>> tt;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
dd[i][j]=-1;
}
}
dd[aa.a][aa.b]=0;
tt.push({aa.a,aa.b});
while(tt.size()){
pair<int,int> no=tt.front();
tt.pop();
//if(dd[no.a][no.b])
for(int i=0;i<4;i++){
int xx=no.a+x[i];
int yy=no.b+y[i];
if(xx<0 or xx>=n or yy<0 or yy>=n){
continue;
}
if(it[xx][yy]==0){
continue;
}
if(dd[xx][yy]==-1){
if(dist[xx][yy]!=-1){
if(dist[xx][yy]<=mid+(dd[no.a][no.b]+1)/s){
continue;
}
}
dd[xx][yy]=dd[no.a][no.b]+1;
//cout<<xx<<":"<<yy<<":"<<dd[xx][yy]<<endl;
tt.push({xx,yy});
}
}
}
/*for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<dd[i][j]<<",";
}
cout<<endl;
}
cout<<mid<<":"<<dd[bb.a][bb.b]<<endl;*/
return dd[bb.a][bb.b]!=-1;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>s;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
dist[i][j]=-1;
char s;
cin>>s;
it[i][j]=1;
if(s=='T'){
it[i][j]=0;
}
else if(s=='G'){
}
else if(s=='M'){
aa={i,j};
}
else if(s=='D'){
it[i][j]=0;
bb={i,j};
}
else if(s=='H'){
ss.push({i,j});
dist[i][j]=0;
}
}
}
while(ss.size()){
pair<int,int> no=ss.front();
ss.pop();
for(int i=0;i<4;i++){
int xx=no.a+x[i];
int yy=no.b+y[i];
if(xx<0 or xx>=n or yy<0 or yy>=n){
continue;
}
if(it[xx][yy]==0){
continue;
}
if(dist[xx][yy]==-1){
dist[xx][yy]=dist[no.a][no.b]+1;
ss.push({xx,yy});
}
}
}
int low=0;
int high=dist[aa.a][aa.b]-1;
it[bb.a][bb.b]=1;
if(check(low)==false){
cout<<-1<<endl;
}
else{
while(low<high-1){
int mid=(low+high)/2;
if(check(mid)){
low=mid;
}
else{
high=mid;
}
}
int ans=low;
if(check(high)){
ans=max(ans,high);
}
cout<<ans<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
145 ms |
8128 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
896 KB |
Output is correct |
13 |
Correct |
1 ms |
784 KB |
Output is correct |
14 |
Correct |
1 ms |
896 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
640 KB |
Output is correct |
20 |
Correct |
1 ms |
640 KB |
Output is correct |
21 |
Correct |
1 ms |
768 KB |
Output is correct |
22 |
Correct |
1 ms |
768 KB |
Output is correct |
23 |
Correct |
1 ms |
768 KB |
Output is correct |
24 |
Correct |
1 ms |
768 KB |
Output is correct |
25 |
Correct |
2 ms |
896 KB |
Output is correct |
26 |
Correct |
1 ms |
896 KB |
Output is correct |
27 |
Correct |
2 ms |
896 KB |
Output is correct |
28 |
Correct |
1 ms |
1024 KB |
Output is correct |
29 |
Correct |
1 ms |
1024 KB |
Output is correct |
30 |
Correct |
2 ms |
1024 KB |
Output is correct |
31 |
Correct |
1 ms |
1024 KB |
Output is correct |
32 |
Correct |
2 ms |
1024 KB |
Output is correct |
33 |
Correct |
9 ms |
3712 KB |
Output is correct |
34 |
Correct |
11 ms |
3712 KB |
Output is correct |
35 |
Correct |
47 ms |
3712 KB |
Output is correct |
36 |
Correct |
12 ms |
4224 KB |
Output is correct |
37 |
Correct |
18 ms |
4224 KB |
Output is correct |
38 |
Correct |
55 ms |
4224 KB |
Output is correct |
39 |
Correct |
17 ms |
4736 KB |
Output is correct |
40 |
Correct |
20 ms |
4736 KB |
Output is correct |
41 |
Correct |
60 ms |
4736 KB |
Output is correct |
42 |
Correct |
18 ms |
5120 KB |
Output is correct |
43 |
Correct |
23 ms |
5120 KB |
Output is correct |
44 |
Correct |
79 ms |
5120 KB |
Output is correct |
45 |
Correct |
21 ms |
5632 KB |
Output is correct |
46 |
Correct |
27 ms |
5632 KB |
Output is correct |
47 |
Correct |
86 ms |
5632 KB |
Output is correct |
48 |
Correct |
29 ms |
6144 KB |
Output is correct |
49 |
Correct |
28 ms |
6144 KB |
Output is correct |
50 |
Correct |
126 ms |
6140 KB |
Output is correct |
51 |
Correct |
30 ms |
6528 KB |
Output is correct |
52 |
Correct |
36 ms |
6528 KB |
Output is correct |
53 |
Correct |
153 ms |
6648 KB |
Output is correct |
54 |
Correct |
33 ms |
7040 KB |
Output is correct |
55 |
Correct |
38 ms |
7032 KB |
Output is correct |
56 |
Correct |
170 ms |
7032 KB |
Output is correct |
57 |
Correct |
40 ms |
7552 KB |
Output is correct |
58 |
Correct |
66 ms |
7544 KB |
Output is correct |
59 |
Correct |
199 ms |
7544 KB |
Output is correct |
60 |
Correct |
41 ms |
7964 KB |
Output is correct |
61 |
Correct |
58 ms |
7968 KB |
Output is correct |
62 |
Correct |
208 ms |
8056 KB |
Output is correct |
63 |
Correct |
163 ms |
7928 KB |
Output is correct |
64 |
Correct |
276 ms |
8056 KB |
Output is correct |
65 |
Correct |
258 ms |
8056 KB |
Output is correct |
66 |
Correct |
195 ms |
8056 KB |
Output is correct |
67 |
Correct |
54 ms |
8184 KB |
Output is correct |
68 |
Correct |
92 ms |
8184 KB |
Output is correct |
69 |
Correct |
64 ms |
8184 KB |
Output is correct |
70 |
Correct |
63 ms |
8192 KB |
Output is correct |
71 |
Correct |
65 ms |
8184 KB |
Output is correct |
72 |
Correct |
61 ms |
8184 KB |
Output is correct |
73 |
Correct |
63 ms |
8440 KB |
Output is correct |
74 |
Correct |
131 ms |
8448 KB |
Output is correct |
75 |
Correct |
104 ms |
8440 KB |
Output is correct |
76 |
Correct |
118 ms |
8312 KB |
Output is correct |
77 |
Correct |
111 ms |
8312 KB |
Output is correct |
78 |
Correct |
45 ms |
8188 KB |
Output is correct |
79 |
Correct |
112 ms |
8184 KB |
Output is correct |
80 |
Correct |
105 ms |
8184 KB |
Output is correct |
81 |
Correct |
112 ms |
8184 KB |
Output is correct |
82 |
Correct |
99 ms |
8184 KB |
Output is correct |
83 |
Correct |
118 ms |
8184 KB |
Output is correct |
84 |
Correct |
125 ms |
8184 KB |
Output is correct |
85 |
Correct |
139 ms |
8184 KB |
Output is correct |
86 |
Correct |
140 ms |
8184 KB |
Output is correct |
87 |
Correct |
124 ms |
8180 KB |
Output is correct |
88 |
Correct |
195 ms |
8184 KB |
Output is correct |
89 |
Correct |
142 ms |
8184 KB |
Output is correct |
90 |
Correct |
138 ms |
8184 KB |
Output is correct |
91 |
Correct |
148 ms |
8184 KB |
Output is correct |
92 |
Correct |
146 ms |
8184 KB |
Output is correct |