#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'&&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 |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
0 ms |
256 KB |
Output isn't correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Incorrect |
0 ms |
256 KB |
Output isn't correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
7 |
Incorrect |
170 ms |
6988 KB |
Output isn't correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 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 |
1648 KB |
Output is correct |
34 |
Correct |
16 ms |
1648 KB |
Output is correct |
35 |
Correct |
37 ms |
1684 KB |
Output is correct |
36 |
Correct |
18 ms |
2012 KB |
Output is correct |
37 |
Correct |
21 ms |
2016 KB |
Output is correct |
38 |
Correct |
47 ms |
2020 KB |
Output is correct |
39 |
Correct |
23 ms |
2400 KB |
Output is correct |
40 |
Correct |
29 ms |
2580 KB |
Output is correct |
41 |
Correct |
65 ms |
2424 KB |
Output is correct |
42 |
Correct |
31 ms |
3128 KB |
Output is correct |
43 |
Correct |
33 ms |
3096 KB |
Output is correct |
44 |
Correct |
84 ms |
3100 KB |
Output is correct |
45 |
Correct |
37 ms |
3680 KB |
Output is correct |
46 |
Correct |
39 ms |
3608 KB |
Output is correct |
47 |
Correct |
101 ms |
3664 KB |
Output is correct |
48 |
Correct |
44 ms |
4184 KB |
Output is correct |
49 |
Correct |
50 ms |
4196 KB |
Output is correct |
50 |
Correct |
115 ms |
4248 KB |
Output is correct |
51 |
Correct |
53 ms |
4820 KB |
Output is correct |
52 |
Correct |
57 ms |
5072 KB |
Output is correct |
53 |
Correct |
147 ms |
4796 KB |
Output is correct |
54 |
Correct |
61 ms |
5432 KB |
Output is correct |
55 |
Correct |
66 ms |
5496 KB |
Output is correct |
56 |
Correct |
170 ms |
5456 KB |
Output is correct |
57 |
Correct |
70 ms |
6164 KB |
Output is correct |
58 |
Correct |
76 ms |
6260 KB |
Output is correct |
59 |
Correct |
194 ms |
6128 KB |
Output is correct |
60 |
Correct |
81 ms |
7000 KB |
Output is correct |
61 |
Correct |
89 ms |
6940 KB |
Output is correct |
62 |
Correct |
218 ms |
7040 KB |
Output is correct |
63 |
Correct |
192 ms |
6912 KB |
Output is correct |
64 |
Correct |
310 ms |
6880 KB |
Output is correct |
65 |
Correct |
248 ms |
7004 KB |
Output is correct |
66 |
Correct |
214 ms |
7008 KB |
Output is correct |
67 |
Correct |
216 ms |
6996 KB |
Output is correct |
68 |
Correct |
116 ms |
6904 KB |
Output is correct |
69 |
Correct |
118 ms |
6976 KB |
Output is correct |
70 |
Correct |
99 ms |
6940 KB |
Output is correct |
71 |
Correct |
110 ms |
7040 KB |
Output is correct |
72 |
Correct |
92 ms |
6916 KB |
Output is correct |
73 |
Correct |
91 ms |
6912 KB |
Output is correct |
74 |
Correct |
126 ms |
6988 KB |
Output is correct |
75 |
Correct |
148 ms |
6912 KB |
Output is correct |
76 |
Correct |
133 ms |
6912 KB |
Output is correct |
77 |
Correct |
136 ms |
6912 KB |
Output is correct |
78 |
Correct |
162 ms |
6980 KB |
Output is correct |
79 |
Correct |
123 ms |
6928 KB |
Output is correct |
80 |
Correct |
138 ms |
6988 KB |
Output is correct |
81 |
Correct |
143 ms |
6896 KB |
Output is correct |
82 |
Correct |
137 ms |
6912 KB |
Output is correct |
83 |
Correct |
153 ms |
6912 KB |
Output is correct |
84 |
Correct |
144 ms |
6908 KB |
Output is correct |
85 |
Correct |
155 ms |
6920 KB |
Output is correct |
86 |
Correct |
146 ms |
7036 KB |
Output is correct |
87 |
Correct |
150 ms |
7024 KB |
Output is correct |
88 |
Correct |
157 ms |
6900 KB |
Output is correct |
89 |
Correct |
159 ms |
7024 KB |
Output is correct |
90 |
Correct |
165 ms |
6960 KB |
Output is correct |
91 |
Correct |
164 ms |
6912 KB |
Output is correct |
92 |
Correct |
170 ms |
6912 KB |
Output is correct |