#include<bits/stdc++.h>
using namespace std;
#define in insert
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#define Endl "\n"
#define ENDL "\n"
#define fi first
#define se second
#define be begin()
#define en end()
#define pb push_back
typedef long long ll;
typedef long double ld;
const int MOD=1e9+7;
const int MAX=2e5+1;/*
ll binpow(ll a, ll b) {
ld res = 1;
while (b > 0) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
unsigned long long power(unsigned long long x,
ll y, ll p)
{
unsigned long long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
unsigned long long modInverse(unsigned long long n,
ll p)
{
return power(n, p - 2, p);
}
*/
ll n , k ;
ll dx[]={0 , 0 , 1, -1};
ll dy[]={1 , -1 , 0, 0};
char grid[805][805];
ll vis[805][805];
ll levelm[805][805];
ll levelh[805][805];
vector<pair<ll,ll>>hive;
bool valid(ll x , ll y)
{
return x>=0 && y>=0 && x<n && y<n && grid[x][y]!='H' && grid[x][y]!='T';
}
bool valid1(ll x , ll y)
{
return x>=0 && y>=0 && x<n && y<n && grid[x][y]!='T';
}
pair<ll,ll>m , d;
bool check(ll u)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
levelm[i][j]=1e18;
}
}
memset(vis , 0 ,sizeof vis );
queue< pair<ll,ll> >q;
q.push({m.fi,m.se});
vis[m.fi][m.se]=1;
levelm[m.fi][m.se]=u;
while(!q.empty())
{
pair<ll,ll>p=q.front();
q.pop();
for(int i=0;i<4;i++)
{
ll nx = p.fi + dx[i];
ll ny = p.se + dy[i];
if(!valid(nx,ny))continue;
levelm[nx][ny]=min(levelm[p.fi][p.se]+1 ,levelm[nx][ny]);
ll qe = (levelm[nx][ny])/k +((levelm[nx][ny])%k!=0) ;
if(levelh[nx][ny] <= qe)continue;
if(vis[nx][ny])continue;
vis[nx][ny] = 1;
q.push({nx , ny});
}
}
if(levelm[d.fi][d.se] != 1e18)
{
return true;
}
return false;
}
void bfs(vector<pair<ll,ll>>h)
{
queue<pair<ll,ll> >q;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
levelh[i][j]=1e18;
}
}
for(int i=0;i<h.size();i++)
{
levelh[h[i].fi][h[i].se]=0;
vis[h[i].fi][h[i].se]=1;
q.push(h[i]);
}
while(!q.empty())
{
pair<ll,ll>p=q.front();
q.pop();
for(int i=0;i<4;i++)
{
ll nx = p.fi + dx[i];
ll ny = p.se + dy[i];
if(!valid1(nx,ny))continue;
levelh[nx][ny] = min(levelh[p.fi][p.se]+1 ,levelh[nx][ny]);
if(vis[nx][ny])continue;
vis[nx][ny] = 1;
q.push({nx , ny});
}
}
}
int main()
{
fastio
//freopen("gravity.in","r",stdin);
//freopen("gravity.out","w",stdout);
cin>>n>>k;
for(int i=0;i<n;i++)
{
string s;
cin>>s;
for(int j=0;j<s.size();j++)
{
grid[i][j]=s[j];
if(s[j]=='H')hive.pb({i,j});
else if(s[j]=='M')m={i,j};
else if(s[j]=='D')d={i,j};
}
}
bfs(hive);
memset(vis , 0 ,sizeof vis);
ll l=0;
ll r=1e18;
ll ans=-1;
while(l<=r)
{
ll mid=(l+r)/2;
if(check(mid))
{
ans=mid;
l=mid+1;
}
else r=mid-1;
}
cout<<ans<<endl;
return 0;
}
/*
5 6
0 4 0
0 3 1
0 1 1
0 2 1
1 2 1
3 4 0
*/
Compilation message
mecho.cpp: In function 'void bfs(std::vector<std::pair<long long int, long long int> >)':
mecho.cpp:125:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | for(int i=0;i<h.size();i++)
| ~^~~~~~~~~
mecho.cpp: In function 'int main()':
mecho.cpp:167:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
167 | for(int j=0;j<s.size();j++)
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
5448 KB |
Output is correct |
2 |
Correct |
19 ms |
5332 KB |
Output is correct |
3 |
Correct |
16 ms |
5332 KB |
Output is correct |
4 |
Correct |
12 ms |
5332 KB |
Output is correct |
5 |
Incorrect |
11 ms |
5460 KB |
Output isn't correct |
6 |
Correct |
11 ms |
5460 KB |
Output is correct |
7 |
Incorrect |
388 ms |
16616 KB |
Output isn't correct |
8 |
Correct |
17 ms |
5460 KB |
Output is correct |
9 |
Incorrect |
16 ms |
5532 KB |
Output isn't correct |
10 |
Incorrect |
10 ms |
5460 KB |
Output isn't correct |
11 |
Incorrect |
13 ms |
5540 KB |
Output isn't correct |
12 |
Incorrect |
13 ms |
5996 KB |
Output isn't correct |
13 |
Incorrect |
12 ms |
5924 KB |
Output isn't correct |
14 |
Correct |
12 ms |
6016 KB |
Output is correct |
15 |
Correct |
12 ms |
5588 KB |
Output is correct |
16 |
Incorrect |
16 ms |
5588 KB |
Output isn't correct |
17 |
Correct |
23 ms |
5588 KB |
Output is correct |
18 |
Incorrect |
17 ms |
5588 KB |
Output isn't correct |
19 |
Correct |
16 ms |
5704 KB |
Output is correct |
20 |
Incorrect |
13 ms |
5704 KB |
Output isn't correct |
21 |
Correct |
12 ms |
5804 KB |
Output is correct |
22 |
Incorrect |
16 ms |
5800 KB |
Output isn't correct |
23 |
Correct |
12 ms |
5904 KB |
Output is correct |
24 |
Incorrect |
12 ms |
5904 KB |
Output isn't correct |
25 |
Correct |
16 ms |
5972 KB |
Output is correct |
26 |
Incorrect |
13 ms |
5996 KB |
Output isn't correct |
27 |
Correct |
14 ms |
6044 KB |
Output is correct |
28 |
Incorrect |
17 ms |
5972 KB |
Output isn't correct |
29 |
Correct |
17 ms |
6108 KB |
Output is correct |
30 |
Incorrect |
26 ms |
6116 KB |
Output isn't correct |
31 |
Correct |
18 ms |
6156 KB |
Output is correct |
32 |
Incorrect |
15 ms |
6164 KB |
Output isn't correct |
33 |
Correct |
33 ms |
10196 KB |
Output is correct |
34 |
Incorrect |
36 ms |
10216 KB |
Output isn't correct |
35 |
Incorrect |
118 ms |
10204 KB |
Output isn't correct |
36 |
Correct |
54 ms |
10820 KB |
Output is correct |
37 |
Incorrect |
56 ms |
10912 KB |
Output isn't correct |
38 |
Incorrect |
165 ms |
10832 KB |
Output isn't correct |
39 |
Correct |
42 ms |
11604 KB |
Output is correct |
40 |
Incorrect |
56 ms |
11640 KB |
Output isn't correct |
41 |
Incorrect |
196 ms |
11636 KB |
Output isn't correct |
42 |
Correct |
54 ms |
12244 KB |
Output is correct |
43 |
Incorrect |
49 ms |
12344 KB |
Output isn't correct |
44 |
Incorrect |
192 ms |
12348 KB |
Output isn't correct |
45 |
Correct |
67 ms |
13012 KB |
Output is correct |
46 |
Incorrect |
73 ms |
13028 KB |
Output isn't correct |
47 |
Incorrect |
229 ms |
13040 KB |
Output isn't correct |
48 |
Correct |
84 ms |
13652 KB |
Output is correct |
49 |
Incorrect |
91 ms |
13704 KB |
Output isn't correct |
50 |
Incorrect |
351 ms |
13724 KB |
Output isn't correct |
51 |
Correct |
100 ms |
14540 KB |
Output is correct |
52 |
Incorrect |
89 ms |
14520 KB |
Output isn't correct |
53 |
Incorrect |
364 ms |
14548 KB |
Output isn't correct |
54 |
Correct |
95 ms |
15256 KB |
Output is correct |
55 |
Incorrect |
94 ms |
15256 KB |
Output isn't correct |
56 |
Incorrect |
434 ms |
15280 KB |
Output isn't correct |
57 |
Correct |
90 ms |
16004 KB |
Output is correct |
58 |
Incorrect |
95 ms |
16008 KB |
Output isn't correct |
59 |
Incorrect |
454 ms |
16044 KB |
Output isn't correct |
60 |
Correct |
99 ms |
16748 KB |
Output is correct |
61 |
Incorrect |
90 ms |
16720 KB |
Output isn't correct |
62 |
Incorrect |
520 ms |
16768 KB |
Output isn't correct |
63 |
Incorrect |
203 ms |
16716 KB |
Output isn't correct |
64 |
Incorrect |
428 ms |
16764 KB |
Output isn't correct |
65 |
Incorrect |
297 ms |
16764 KB |
Output isn't correct |
66 |
Incorrect |
240 ms |
16784 KB |
Output isn't correct |
67 |
Correct |
200 ms |
16820 KB |
Output is correct |
68 |
Incorrect |
151 ms |
16756 KB |
Output isn't correct |
69 |
Incorrect |
129 ms |
16824 KB |
Output isn't correct |
70 |
Incorrect |
109 ms |
16824 KB |
Output isn't correct |
71 |
Incorrect |
119 ms |
16876 KB |
Output isn't correct |
72 |
Incorrect |
107 ms |
16748 KB |
Output isn't correct |
73 |
Incorrect |
150 ms |
17436 KB |
Output isn't correct |
74 |
Incorrect |
235 ms |
17316 KB |
Output isn't correct |
75 |
Incorrect |
216 ms |
17384 KB |
Output isn't correct |
76 |
Incorrect |
184 ms |
17320 KB |
Output isn't correct |
77 |
Incorrect |
195 ms |
17308 KB |
Output isn't correct |
78 |
Correct |
264 ms |
17316 KB |
Output is correct |
79 |
Incorrect |
199 ms |
17228 KB |
Output isn't correct |
80 |
Incorrect |
180 ms |
17280 KB |
Output isn't correct |
81 |
Incorrect |
225 ms |
17224 KB |
Output isn't correct |
82 |
Incorrect |
184 ms |
17248 KB |
Output isn't correct |
83 |
Incorrect |
263 ms |
17164 KB |
Output isn't correct |
84 |
Incorrect |
311 ms |
17184 KB |
Output isn't correct |
85 |
Incorrect |
239 ms |
17228 KB |
Output isn't correct |
86 |
Incorrect |
244 ms |
17148 KB |
Output isn't correct |
87 |
Incorrect |
278 ms |
17100 KB |
Output isn't correct |
88 |
Incorrect |
267 ms |
17108 KB |
Output isn't correct |
89 |
Incorrect |
363 ms |
17100 KB |
Output isn't correct |
90 |
Incorrect |
315 ms |
16972 KB |
Output isn't correct |
91 |
Incorrect |
318 ms |
17104 KB |
Output isn't correct |
92 |
Incorrect |
296 ms |
17104 KB |
Output isn't correct |