#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <set>
#include <iterator>
#include <stack>
#include <map>
#include <math.h>
#include <bitset>
#include <deque>
#include <string>
#include <tuple>
#include <queue>
#include <numeric>
#include <unordered_set>
#include <unordered_map>
#define pi 3.141592653589793238
#define ll long long
#define ld long double
#define rep(i, a, b) for (long long i = a; i <= b; i++)
#define mod 998244353ll
#define INF 1000000000000000000
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define all(x) (x).begin (), (x).end ()
#define sz(x) (ll) (x).size ()
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define rank rnk
#define log lg
#define fast \
ios_base::sync_with_stdio (false); \
cin.tie (NULL); \
cout.tie (NULL)
using namespace std;
vector <pair<int,int>> dir={{0,1},{0,-1},{1,0},{-1,0}};
signed main ()
{
fast;
// freopen("atlarge.in","r",stdin);
// freopen("atlarge.out","w",stdout);
int n,s;
cin>>n>>s;
string g[n];
rep(i,0,n-1)
cin>>g[i];
int lo=0;
int hi=n*n;
int best=-1;
while(lo<=hi)
{
int mid=(lo+hi)/2;
queue <pair<int,int>> q;
int disth[n][n];
rep(i,0,n-1)
rep(j,0,n-1)
disth[i][j]=mod;
rep(i,0,n-1)
rep(j,0,n-1)
if(g[i][j]=='H')
disth[i][j]=0, q.push({i,j});
while(!q.empty())
{
auto v=q.front();
q.pop();
if(disth[v.ff][v.ss]==mid)
continue;
for(auto mv:dir)
{
pair <int,int> u={v.ff+mv.ff,v.ss+mv.ss};
if(u.ff>=0 && u.ff<n && u.ss>=0 && u.ss<n)
if(g[u.ff][u.ss]=='G' || g[u.ff][u.ss]=='M')
if(disth[u.ff][u.ss]>disth[v.ff][v.ss]+1)
{
disth[u.ff][u.ss]=disth[v.ff][v.ss]+1;
q.push(u);
}
}
}
rep(i,0,n-1)
rep(j,0,n-1)
if(disth[i][j]!=mod)
{
disth[i][j]=0;
q.push({i,j});
}
while(!q.empty())
{
auto v=q.front();
q.pop();
for(auto mv:dir)
{
pair <int,int> u={v.ff+mv.ff,v.ss+mv.ss};
if(u.ff>=0 && u.ff<n && u.ss>=0 && u.ss<n)
if(g[u.ff][u.ss]=='G' || g[u.ff][u.ss]=='M')
if(disth[u.ff][u.ss]>disth[v.ff][v.ss]+1)
{
disth[u.ff][u.ss]=disth[v.ff][v.ss]+1;
q.push(u);
}
}
}
// rep(i,0,n-1)
// {
// rep(j,0,n-1)
// {
// if(disth[i][j]==mod)
// cout<<"-";
// else
// cout<<disth[i][j];
// }
// cout<<endl;
// }
// cout<<endl;
int distm[n][n];
rep(i,0,n-1)
rep(j,0,n-1)
distm[i][j]=mod;
pair <int,int> start;
rep(i,0,n-1)
rep(j,0,n-1)
if(g[i][j]=='M')
start={i,j};
distm[start.ff][start.ss]=0;
if(disth[start.ff][start.ss]!=0)
q.push(start);
while(!q.empty())
{
auto v=q.front();
q.pop();
for(auto mv:dir)
{
pair <int,int> u={v.ff+mv.ff,v.ss+mv.ss};
if(u.ff>=0 && u.ff<n && u.ss>=0 && u.ss<n)
if(g[u.ff][u.ss]=='G' || g[u.ff][u.ss]=='M' || g[u.ff][u.ss]=='D')
if(distm[u.ff][u.ss]>distm[v.ff][v.ss]+1)
{
if((distm[v.ff][v.ss]+1)/s<disth[u.ff][u.ss])
{
distm[u.ff][u.ss]=distm[v.ff][v.ss]+1;
q.push(u);
}
}
}
}
// rep(i,0,n-1)
// {
// rep(j,0,n-1)
// {
// if(distm[i][j]==mod)
// cout<<"-";
// else
// cout<<distm[i][j];
// }
// cout<<endl;
// }
// cout<<endl;
// bool cango[n][n], visited[n][n];
// rep(i,0,n-1)
// rep(j,0,n-1)
// {
// if(distm[i][j]/s<disth[i][j] && g[i][j]!='T')
// cango[i][j]=true;
// else
// cango[i][j]=false;
// visited[i][j]=false;
// }
// if(cango[start.ff][start.ss])
// q.push(start), visited[start.ff][start.ss]=true;
// rep(i,0,n-1)
// {
// rep(j,0,n-1)
// {
// if(cango[i][j])
// cout<<"+";
// else
// cout<<"-";
// }
// cout<<endl;
// }
// cout<<endl;
// while(!q.empty())
// {
// auto v=q.front();
// q.pop();
// for(auto mv:dir)
// {
// pair <int,int> u={v.ff+mv.ff,v.ss+mv.ss};
// if(u.ff>=0 && u.ff<n && u.ss>=0 && u.ss<n)
// if(cango[u.ff][u.ss])
// if(!visited[u.ff][u.ss])
// {
// visited[u.ff][u.ss]=true;
// q.push(u);
// }
// }
// }
bool ok=false;
rep(i,0,n-1)
rep(j,0,n-1)
if(g[i][j]=='D')
if(distm[i][j]!=mod)
ok=true;
if(ok)
{
best=mid;
lo=mid+1;
}
else
{
hi=mid-1;
}
}
cout<<best;
return 0;
}
/*
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
THHHHHT
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
0 ms |
312 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
312 KB |
Output is correct |
7 |
Correct |
554 ms |
11860 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 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 |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
308 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
284 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
312 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
332 KB |
Output is correct |
31 |
Correct |
2 ms |
316 KB |
Output is correct |
32 |
Correct |
2 ms |
332 KB |
Output is correct |
33 |
Correct |
49 ms |
1996 KB |
Output is correct |
34 |
Correct |
45 ms |
2036 KB |
Output is correct |
35 |
Correct |
66 ms |
2264 KB |
Output is correct |
36 |
Correct |
60 ms |
2664 KB |
Output is correct |
37 |
Correct |
62 ms |
2536 KB |
Output is correct |
38 |
Correct |
86 ms |
2852 KB |
Output is correct |
39 |
Correct |
76 ms |
3132 KB |
Output is correct |
40 |
Correct |
79 ms |
3132 KB |
Output is correct |
41 |
Correct |
122 ms |
3520 KB |
Output is correct |
42 |
Correct |
102 ms |
3788 KB |
Output is correct |
43 |
Correct |
95 ms |
3808 KB |
Output is correct |
44 |
Correct |
142 ms |
4320 KB |
Output is correct |
45 |
Correct |
126 ms |
4424 KB |
Output is correct |
46 |
Correct |
118 ms |
4624 KB |
Output is correct |
47 |
Correct |
171 ms |
5160 KB |
Output is correct |
48 |
Correct |
144 ms |
5420 KB |
Output is correct |
49 |
Correct |
149 ms |
5316 KB |
Output is correct |
50 |
Correct |
204 ms |
6084 KB |
Output is correct |
51 |
Correct |
174 ms |
6192 KB |
Output is correct |
52 |
Correct |
171 ms |
6176 KB |
Output is correct |
53 |
Correct |
254 ms |
7076 KB |
Output is correct |
54 |
Correct |
209 ms |
7124 KB |
Output is correct |
55 |
Correct |
203 ms |
7376 KB |
Output is correct |
56 |
Correct |
299 ms |
8136 KB |
Output is correct |
57 |
Correct |
230 ms |
8176 KB |
Output is correct |
58 |
Correct |
226 ms |
8260 KB |
Output is correct |
59 |
Correct |
334 ms |
9304 KB |
Output is correct |
60 |
Correct |
281 ms |
9284 KB |
Output is correct |
61 |
Correct |
260 ms |
9332 KB |
Output is correct |
62 |
Correct |
393 ms |
10636 KB |
Output is correct |
63 |
Correct |
503 ms |
9760 KB |
Output is correct |
64 |
Correct |
604 ms |
9804 KB |
Output is correct |
65 |
Correct |
544 ms |
9732 KB |
Output is correct |
66 |
Correct |
534 ms |
9904 KB |
Output is correct |
67 |
Correct |
517 ms |
9720 KB |
Output is correct |
68 |
Correct |
557 ms |
9872 KB |
Output is correct |
69 |
Correct |
580 ms |
10064 KB |
Output is correct |
70 |
Correct |
536 ms |
9752 KB |
Output is correct |
71 |
Correct |
568 ms |
9748 KB |
Output is correct |
72 |
Correct |
609 ms |
9752 KB |
Output is correct |
73 |
Correct |
538 ms |
11836 KB |
Output is correct |
74 |
Correct |
627 ms |
11824 KB |
Output is correct |
75 |
Correct |
624 ms |
11832 KB |
Output is correct |
76 |
Correct |
627 ms |
11824 KB |
Output is correct |
77 |
Correct |
630 ms |
12012 KB |
Output is correct |
78 |
Correct |
555 ms |
11856 KB |
Output is correct |
79 |
Correct |
560 ms |
11828 KB |
Output is correct |
80 |
Correct |
575 ms |
11920 KB |
Output is correct |
81 |
Correct |
550 ms |
11888 KB |
Output is correct |
82 |
Correct |
566 ms |
11832 KB |
Output is correct |
83 |
Correct |
535 ms |
11884 KB |
Output is correct |
84 |
Correct |
583 ms |
11840 KB |
Output is correct |
85 |
Correct |
608 ms |
11836 KB |
Output is correct |
86 |
Correct |
564 ms |
11912 KB |
Output is correct |
87 |
Correct |
600 ms |
12008 KB |
Output is correct |
88 |
Correct |
560 ms |
11800 KB |
Output is correct |
89 |
Correct |
567 ms |
11840 KB |
Output is correct |
90 |
Correct |
573 ms |
11912 KB |
Output is correct |
91 |
Correct |
594 ms |
12020 KB |
Output is correct |
92 |
Correct |
572 ms |
11832 KB |
Output is correct |