#include <bits/stdc++.h>
using namespace std;
using tint = long long;
using ld = long double;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
using pi = pair<int,int>;
using pl = pair<tint,tint>;
using vi = vector<int>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vvi = vector<vi>;
using vl = vector<tint>;
using vb = vector<bool>;
#define pb push_back
#define pf push_front
#define rsz resize
#define all(x) begin(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)(x).size()
#define ins insert
#define f first
#define s second
#define mp make_pair
#define DBG(x) cerr << #x << " = " << x << endl;
const int MOD = 1e9+9; //change this
const tint mod = 998244353;
const int MX = 805;
const tint INF = 1e18;
const int inf = 2e9;
const ld PI = acos(ld(-1));
const ld eps = 1e-5;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
template<class T> void remDup(vector<T> &v){
sort(all(v)); v.erase(unique(all(v)),end(v));
}
template<class T> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template<class T> bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
bool valid(int x, int y, int n, int m){
return (0<=x && x<n && 0<=y && y<m);
}
int cdiv(int a, int b) { return a/b+((a^b)>0&&a%b); } //redondea p arriba
int fdiv(int a, int b) { return a/b-((a^b)<0&&a%b); } //redondea p abajo
void NACHO(string name = ""){
ios_base::sync_with_stdio(0); cin.tie(0);
if(sz(name)){
freopen((name+".in").c_str(), "r", stdin);
freopen((name+".out").c_str(), "w", stdout);
}
}
string a[MX];
int x, y, sx, sy;
int dist[MX][MX];
int distM[MX][MX];
bool vis[MX][MX];
int n, s;
void bfs(){
queue<pi> q;
F0R(i, n){
F0R(j, n){
dist[i][j] = inf;
if(a[i][j] == 'H'){
dist[i][j] = 0;
vis[i][j] = 1;
q.push(mp(i, j));
}
}
}
while(sz(q)){
auto u = q.front(); q.pop();
F0R(k, 4){
int xx = u.f+dx[k], yy = u.s+dy[k];
if(valid(xx, yy, n, n) && !vis[xx][yy] && (a[xx][yy] == 'G' || a[xx][yy] == 'M')){
dist[xx][yy] = dist[u.f][u.s]+1;
vis[xx][yy] = 1;
q.push(mp(xx, yy));
}
}
}
}
bool bee[MX][MX];
bool can(int m){
F0R(i, n) F0R(j, n) vis[i][j] = 0;
vis[x][y] = 1;
queue<pi> q;
q.push(mp(x, y));
if(dist[x][y]-m <= 0) return 0;
while(sz(q)){
auto u = q.front(); q.pop();
F0R(k, 4){
int xx = u.f+dx[k], yy = u.s+dy[k];
if(valid(xx, yy, n, n) && !vis[xx][yy] && (a[xx][yy] == 'G' || a[xx][yy] == 'D') && (distM[u.f][u.s]+1)/s < dist[xx][yy]-m){
distM[xx][yy] = distM[u.f][u.s]+1;
vis[xx][yy] = 1;
q.push(mp(xx, yy));
}
}
}
return vis[sx][sy];
}
int main(){
NACHO();
cin >> n >> s;
F0R(i, n){
cin >> a[i];
F0R(j, n){
if(a[i][j] == 'M') x = i, y = j;
if(a[i][j] == 'D') sx = i, sy = j;
}
}
bfs();
int low = 0, high = 2*MX+5;
if(!can(0)){
cout << -1 << "\n";
return 0;
}
while(high-low > 1){
int mid = low+(high-low)/2;
if(can(mid)) low = mid;
else high = mid;
}
cout << low << "\n";
}
/*
3 1
MGD
GGG
HGG
*/
/*
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
TGHHGGT
*/
/*
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
THHHHHT
*/
Compilation message
mecho.cpp: In function 'void NACHO(std::string)':
mecho.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
69 | freopen((name+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
70 | freopen((name+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
115 ms |
6892 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
876 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
748 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
620 KB |
Output is correct |
21 |
Correct |
1 ms |
620 KB |
Output is correct |
22 |
Correct |
1 ms |
620 KB |
Output is correct |
23 |
Correct |
1 ms |
620 KB |
Output is correct |
24 |
Correct |
1 ms |
748 KB |
Output is correct |
25 |
Incorrect |
1 ms |
748 KB |
Output isn't correct |
26 |
Correct |
1 ms |
876 KB |
Output is correct |
27 |
Incorrect |
2 ms |
748 KB |
Output isn't correct |
28 |
Correct |
2 ms |
876 KB |
Output is correct |
29 |
Incorrect |
2 ms |
748 KB |
Output isn't correct |
30 |
Correct |
2 ms |
876 KB |
Output is correct |
31 |
Incorrect |
2 ms |
876 KB |
Output isn't correct |
32 |
Correct |
3 ms |
876 KB |
Output is correct |
33 |
Incorrect |
20 ms |
2668 KB |
Output isn't correct |
34 |
Incorrect |
24 ms |
3180 KB |
Output isn't correct |
35 |
Correct |
28 ms |
3052 KB |
Output is correct |
36 |
Incorrect |
20 ms |
2924 KB |
Output isn't correct |
37 |
Incorrect |
31 ms |
3564 KB |
Output isn't correct |
38 |
Correct |
38 ms |
3564 KB |
Output is correct |
39 |
Incorrect |
25 ms |
3308 KB |
Output isn't correct |
40 |
Incorrect |
39 ms |
3948 KB |
Output isn't correct |
41 |
Correct |
49 ms |
3948 KB |
Output is correct |
42 |
Incorrect |
31 ms |
3820 KB |
Output isn't correct |
43 |
Incorrect |
49 ms |
4460 KB |
Output isn't correct |
44 |
Correct |
65 ms |
4460 KB |
Output is correct |
45 |
Incorrect |
37 ms |
3948 KB |
Output isn't correct |
46 |
Incorrect |
56 ms |
4844 KB |
Output isn't correct |
47 |
Correct |
80 ms |
4844 KB |
Output is correct |
48 |
Incorrect |
42 ms |
4332 KB |
Output isn't correct |
49 |
Incorrect |
68 ms |
5228 KB |
Output isn't correct |
50 |
Correct |
106 ms |
5356 KB |
Output is correct |
51 |
Incorrect |
49 ms |
4716 KB |
Output isn't correct |
52 |
Incorrect |
80 ms |
5740 KB |
Output isn't correct |
53 |
Correct |
146 ms |
5740 KB |
Output is correct |
54 |
Incorrect |
56 ms |
4972 KB |
Output isn't correct |
55 |
Incorrect |
93 ms |
6124 KB |
Output isn't correct |
56 |
Correct |
132 ms |
6124 KB |
Output is correct |
57 |
Incorrect |
70 ms |
5356 KB |
Output isn't correct |
58 |
Incorrect |
107 ms |
6608 KB |
Output isn't correct |
59 |
Correct |
161 ms |
6612 KB |
Output is correct |
60 |
Incorrect |
73 ms |
5740 KB |
Output isn't correct |
61 |
Incorrect |
122 ms |
7020 KB |
Output isn't correct |
62 |
Correct |
172 ms |
7020 KB |
Output is correct |
63 |
Correct |
120 ms |
7020 KB |
Output is correct |
64 |
Correct |
176 ms |
7040 KB |
Output is correct |
65 |
Correct |
204 ms |
7020 KB |
Output is correct |
66 |
Correct |
169 ms |
7040 KB |
Output is correct |
67 |
Correct |
37 ms |
7020 KB |
Output is correct |
68 |
Correct |
55 ms |
7020 KB |
Output is correct |
69 |
Correct |
49 ms |
7020 KB |
Output is correct |
70 |
Correct |
44 ms |
7020 KB |
Output is correct |
71 |
Correct |
43 ms |
7020 KB |
Output is correct |
72 |
Correct |
41 ms |
7020 KB |
Output is correct |
73 |
Correct |
50 ms |
7276 KB |
Output is correct |
74 |
Correct |
95 ms |
7276 KB |
Output is correct |
75 |
Correct |
90 ms |
7276 KB |
Output is correct |
76 |
Correct |
97 ms |
7424 KB |
Output is correct |
77 |
Correct |
103 ms |
7312 KB |
Output is correct |
78 |
Correct |
34 ms |
5868 KB |
Output is correct |
79 |
Correct |
92 ms |
7276 KB |
Output is correct |
80 |
Correct |
101 ms |
5868 KB |
Output is correct |
81 |
Correct |
89 ms |
5996 KB |
Output is correct |
82 |
Correct |
77 ms |
5996 KB |
Output is correct |
83 |
Correct |
112 ms |
5996 KB |
Output is correct |
84 |
Correct |
112 ms |
7148 KB |
Output is correct |
85 |
Correct |
106 ms |
6124 KB |
Output is correct |
86 |
Correct |
105 ms |
6124 KB |
Output is correct |
87 |
Correct |
93 ms |
6124 KB |
Output is correct |
88 |
Correct |
102 ms |
6124 KB |
Output is correct |
89 |
Correct |
128 ms |
7148 KB |
Output is correct |
90 |
Correct |
112 ms |
6892 KB |
Output is correct |
91 |
Correct |
117 ms |
7148 KB |
Output is correct |
92 |
Correct |
114 ms |
6124 KB |
Output is correct |