#include<bits/stdc++.h>
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization("unroll-loops")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("fast-math")
// #pragma GCC optimize("no-stack-protector")
// #define ll __int128
#define ll long long
// #define ll int
#define f(i,a,b) for(int i=a;i<b;i++)
#define mod 1000000007
// #define mod 998244353
#define mp make_pair
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define ff first
#define ss second
#define rf(i,a,b) for(int i=a;i>=b;i--)
#define sc(a) scanf("%lld",&a)
#define pf printf
#define sz(a) (int)(a.size())
#define psf push_front
#define ppf pop_front
#define ppb pop_back
#define pb push_back
#define pq priority_queue
#define all(s) s.begin(),s.end()
#define sp(a) setprecision(a)
#define rz resize
#define ld long double
#define inf (ll)1e18
#define ub upper_bound
#define lb lower_bound
#define bs binary_search
#define eb emplace_back
const double pi = acos(-1);
ll binpow(ll a, ll b){ll res=1;while(b!=0){if(b&1)res*=a;a*=a;b>>=1;}return res;}
// ll binpow(ll a, ll b, ll md){ll res=1;a%=mod;while(b!=0){if(b&1)res*=a,res%=md;a*=a,a%=md;b>>=1;}return res%md;}
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("xortransform.in","r",stdin);
// freopen("xortransform.out","w",stdout);
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// #endif
int z=1;
// cin>>z;
f(i,1,z+1)
{
//cout<<"Case #"<<i<<": ";
ll n,d,sx,sy,fx,fy;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
cin>>n>>d;
vector<string> s(n);
f(i,0,n)
cin>>s[i];
ll l=0,r=n*n+1;
queue<vector<ll> > q;
vector<vector<ll> > dis(n,vector<ll> (n,inf));
f(i,0,n)
{
f(j,0,n)
{
if(s[i][j]=='H')
q.push({i,j}),dis[i][j]=0;
else if(s[i][j]=='M')
sx=i,sy=j;
else if(s[i][j]=='D')
fx=i,fy=j;
}
}
while(!q.empty())
{
auto temp=q.front();
q.pop();
ll x=temp[0],y=temp[1];
f(i,0,4)
{
ll xx=x+dx[i],yy=y+dy[i];
if(xx>=0 && yy>=0 && xx<n && yy<n && s[xx][yy]!='T' && s[xx][yy]!='D' && dis[xx][yy]>d+dis[x][y])
dis[xx][yy]=d+dis[x][y],q.push({xx,yy});
}
}
while(l<=r)
{
ll mid=(l+r)/2;
queue<vector<ll> > q;
vector<vector<ll> > dis2(n,vector<ll> (n,inf));
if(dis[sx][sy]>mid*d)
q.push({sx,sy});
dis2[sx][sy]=mid*d;
while(!q.empty())
{
auto temp=q.front();
q.pop();
ll x=temp[0],y=temp[1];
if(x==fx && y==fy)
break;
f(i,0,4)
{
ll xx=x+dx[i],yy=y+dy[i];
if(xx>=0 && yy>=0 && xx<n && yy<n && s[xx][yy]!='T' && dis2[xx][yy]>1+dis2[x][y])
{
if((xx==fx && yy==fy) || (1+dis2[x][y]<dis[xx][yy]))
dis2[xx][yy]=1+dis2[x][y],q.push({xx,yy});
}
}
}
if(dis2[fx][fy]<inf)
l=mid+1;
else
r=mid-1;
}
cout<<r<<"\n";
}
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:116:27: warning: 'fy' may be used uninitialized in this function [-Wmaybe-uninitialized]
116 | if(dis2[fx][fy]<inf)
| ^
mecho.cpp:116:23: warning: 'fx' may be used uninitialized in this function [-Wmaybe-uninitialized]
116 | if(dis2[fx][fy]<inf)
| ^
mecho.cpp:96:26: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
96 | if(dis[sx][sy]>mid*d)
| ^
mecho.cpp:96:22: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
96 | if(dis[sx][sy]>mid*d)
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
325 ms |
11316 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
12 ms |
2440 KB |
Output is correct |
34 |
Correct |
14 ms |
2444 KB |
Output is correct |
35 |
Correct |
76 ms |
2412 KB |
Output is correct |
36 |
Correct |
18 ms |
3112 KB |
Output is correct |
37 |
Correct |
19 ms |
3072 KB |
Output is correct |
38 |
Correct |
97 ms |
3064 KB |
Output is correct |
39 |
Correct |
22 ms |
3776 KB |
Output is correct |
40 |
Correct |
23 ms |
3740 KB |
Output is correct |
41 |
Correct |
123 ms |
3744 KB |
Output is correct |
42 |
Correct |
27 ms |
4608 KB |
Output is correct |
43 |
Correct |
36 ms |
4580 KB |
Output is correct |
44 |
Correct |
173 ms |
4580 KB |
Output is correct |
45 |
Correct |
32 ms |
5476 KB |
Output is correct |
46 |
Correct |
32 ms |
5492 KB |
Output is correct |
47 |
Correct |
189 ms |
5452 KB |
Output is correct |
48 |
Correct |
39 ms |
6492 KB |
Output is correct |
49 |
Correct |
45 ms |
6688 KB |
Output is correct |
50 |
Correct |
236 ms |
6540 KB |
Output is correct |
51 |
Correct |
47 ms |
7520 KB |
Output is correct |
52 |
Correct |
49 ms |
7528 KB |
Output is correct |
53 |
Correct |
298 ms |
7508 KB |
Output is correct |
54 |
Correct |
55 ms |
8696 KB |
Output is correct |
55 |
Correct |
62 ms |
8628 KB |
Output is correct |
56 |
Correct |
365 ms |
8624 KB |
Output is correct |
57 |
Correct |
60 ms |
9888 KB |
Output is correct |
58 |
Correct |
67 ms |
9924 KB |
Output is correct |
59 |
Correct |
447 ms |
9868 KB |
Output is correct |
60 |
Correct |
72 ms |
11224 KB |
Output is correct |
61 |
Correct |
77 ms |
11148 KB |
Output is correct |
62 |
Correct |
471 ms |
11248 KB |
Output is correct |
63 |
Correct |
320 ms |
11156 KB |
Output is correct |
64 |
Correct |
575 ms |
11144 KB |
Output is correct |
65 |
Correct |
423 ms |
11192 KB |
Output is correct |
66 |
Correct |
338 ms |
11124 KB |
Output is correct |
67 |
Correct |
366 ms |
11172 KB |
Output is correct |
68 |
Correct |
151 ms |
11248 KB |
Output is correct |
69 |
Correct |
157 ms |
11208 KB |
Output is correct |
70 |
Correct |
120 ms |
11236 KB |
Output is correct |
71 |
Correct |
138 ms |
11272 KB |
Output is correct |
72 |
Correct |
100 ms |
11232 KB |
Output is correct |
73 |
Correct |
144 ms |
11272 KB |
Output is correct |
74 |
Correct |
216 ms |
11180 KB |
Output is correct |
75 |
Correct |
272 ms |
11252 KB |
Output is correct |
76 |
Correct |
246 ms |
11304 KB |
Output is correct |
77 |
Correct |
231 ms |
11220 KB |
Output is correct |
78 |
Correct |
322 ms |
11200 KB |
Output is correct |
79 |
Correct |
185 ms |
11252 KB |
Output is correct |
80 |
Correct |
240 ms |
11148 KB |
Output is correct |
81 |
Correct |
258 ms |
11216 KB |
Output is correct |
82 |
Correct |
224 ms |
11156 KB |
Output is correct |
83 |
Correct |
292 ms |
11224 KB |
Output is correct |
84 |
Correct |
253 ms |
11132 KB |
Output is correct |
85 |
Correct |
283 ms |
11248 KB |
Output is correct |
86 |
Correct |
276 ms |
11228 KB |
Output is correct |
87 |
Correct |
336 ms |
11188 KB |
Output is correct |
88 |
Correct |
292 ms |
11196 KB |
Output is correct |
89 |
Correct |
318 ms |
11180 KB |
Output is correct |
90 |
Correct |
315 ms |
11184 KB |
Output is correct |
91 |
Correct |
303 ms |
11328 KB |
Output is correct |
92 |
Correct |
370 ms |
11224 KB |
Output is correct |