#include <algorithm>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P,int> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
int n,m;
vvi b;
vs a;
bool f(int t){
queue<P> q;
vvi c(n,vi(n,inf));
int gx,gy;
for(int i=0;i<n;i++) for(int j=0;j<n;j++){
if(a[i][j]=='M'){
q.push({i,j});
c[i][j]=0;
if(t>=b[i][j]) return 0;
}
if(a[i][j]=='D') gx=i,gy=j;
}
while(!q.empty()){
P p=q.front();q.pop();
int i=p.first,j=p.second;
for(int k=0;k<4;k++){
int x=i+dx[k],y=j+dy[k];
if(x>=0&&x<n&&y>=0&&y<n&&a[x][y]!='T'&&c[x][y]==inf&&t+(c[i][j]+1)/m<b[x][y]){
q.push({x,y});
c[x][y]=c[i][j]+1;
}
}
}
return c[gx][gy]<inf;
}
int main(){
cin>>n>>m;
a=vs(n);
b=vvi(n,vi(n,inf));
for(auto &i:a) cin>>i;
queue<P> q;
for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(a[i][j]=='H'){
b[i][j]=0;
q.push({i,j});
}
while(!q.empty()){
P p=q.front();q.pop();
int i=p.first,j=p.second;
for(int k=0;k<4;k++){
int x=i+dx[k],y=j+dy[k];
if(x>=0&&x<n&&y>=0&&y<n&&a[x][y]!='T'&&a[x][y]!='D'&&b[x][y]==inf){
q.push({x,y});
b[x][y]=b[i][j]+1;
}
}
}
int l=-1,r=n*n;
while(r-l>1){
int m=(l+r)/2;
if(f(m)) l=m;
else r=m;
}
cout<<l<<endl;
}
Compilation message
mecho.cpp: In function 'bool f(int)':
mecho.cpp:65:17: warning: 'gy' may be used uninitialized in this function [-Wmaybe-uninitialized]
65 | return c[gx][gy]<inf;
| ^
mecho.cpp:65:13: warning: 'gx' may be used uninitialized in this function [-Wmaybe-uninitialized]
65 | return c[gx][gy]<inf;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
182 ms |
6280 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
14 ms |
1528 KB |
Output is correct |
34 |
Correct |
15 ms |
1528 KB |
Output is correct |
35 |
Correct |
39 ms |
1528 KB |
Output is correct |
36 |
Correct |
18 ms |
1892 KB |
Output is correct |
37 |
Correct |
24 ms |
1888 KB |
Output is correct |
38 |
Correct |
47 ms |
1888 KB |
Output is correct |
39 |
Correct |
24 ms |
2196 KB |
Output is correct |
40 |
Correct |
28 ms |
2196 KB |
Output is correct |
41 |
Correct |
65 ms |
2200 KB |
Output is correct |
42 |
Correct |
30 ms |
2840 KB |
Output is correct |
43 |
Correct |
32 ms |
2848 KB |
Output is correct |
44 |
Correct |
84 ms |
2896 KB |
Output is correct |
45 |
Correct |
40 ms |
3308 KB |
Output is correct |
46 |
Correct |
39 ms |
3288 KB |
Output is correct |
47 |
Correct |
97 ms |
3352 KB |
Output is correct |
48 |
Correct |
44 ms |
3856 KB |
Output is correct |
49 |
Correct |
47 ms |
3876 KB |
Output is correct |
50 |
Correct |
118 ms |
3836 KB |
Output is correct |
51 |
Correct |
51 ms |
4408 KB |
Output is correct |
52 |
Correct |
56 ms |
4400 KB |
Output is correct |
53 |
Correct |
144 ms |
4388 KB |
Output is correct |
54 |
Correct |
61 ms |
4980 KB |
Output is correct |
55 |
Correct |
67 ms |
4976 KB |
Output is correct |
56 |
Correct |
170 ms |
4948 KB |
Output is correct |
57 |
Correct |
72 ms |
5588 KB |
Output is correct |
58 |
Correct |
75 ms |
5576 KB |
Output is correct |
59 |
Correct |
192 ms |
5612 KB |
Output is correct |
60 |
Correct |
82 ms |
6272 KB |
Output is correct |
61 |
Correct |
90 ms |
6272 KB |
Output is correct |
62 |
Correct |
222 ms |
6360 KB |
Output is correct |
63 |
Correct |
192 ms |
6388 KB |
Output is correct |
64 |
Correct |
312 ms |
6268 KB |
Output is correct |
65 |
Correct |
248 ms |
6232 KB |
Output is correct |
66 |
Correct |
214 ms |
6248 KB |
Output is correct |
67 |
Correct |
206 ms |
6244 KB |
Output is correct |
68 |
Correct |
116 ms |
6264 KB |
Output is correct |
69 |
Correct |
116 ms |
6236 KB |
Output is correct |
70 |
Correct |
101 ms |
6324 KB |
Output is correct |
71 |
Correct |
110 ms |
6276 KB |
Output is correct |
72 |
Correct |
93 ms |
6324 KB |
Output is correct |
73 |
Correct |
88 ms |
6268 KB |
Output is correct |
74 |
Correct |
126 ms |
6256 KB |
Output is correct |
75 |
Correct |
148 ms |
6280 KB |
Output is correct |
76 |
Correct |
134 ms |
6304 KB |
Output is correct |
77 |
Correct |
133 ms |
6272 KB |
Output is correct |
78 |
Correct |
156 ms |
6272 KB |
Output is correct |
79 |
Correct |
122 ms |
6300 KB |
Output is correct |
80 |
Correct |
132 ms |
6256 KB |
Output is correct |
81 |
Correct |
148 ms |
6284 KB |
Output is correct |
82 |
Correct |
135 ms |
6308 KB |
Output is correct |
83 |
Correct |
156 ms |
6256 KB |
Output is correct |
84 |
Correct |
142 ms |
6348 KB |
Output is correct |
85 |
Correct |
152 ms |
6280 KB |
Output is correct |
86 |
Correct |
150 ms |
6256 KB |
Output is correct |
87 |
Correct |
149 ms |
6280 KB |
Output is correct |
88 |
Correct |
159 ms |
6356 KB |
Output is correct |
89 |
Correct |
157 ms |
6272 KB |
Output is correct |
90 |
Correct |
160 ms |
6260 KB |
Output is correct |
91 |
Correct |
158 ms |
6260 KB |
Output is correct |
92 |
Correct |
171 ms |
6276 KB |
Output is correct |