#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<ii>;
using vpl = vector<pl>;
#define mp make_pair
#define pb push_back
#define pp pop_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define sz(v) ((int)v.size())
#define all(x) (x).begin() , (x).end()
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ld dist(ld x, ld y, ld a, ld b) { return sqrt((x-a)*(x-a) + (y-b)*(y-b)); }
ll gcd(ll a , ll b){ return b ? gcd(b , a % b) : a ;}
ll lcm(ll a , ll b){ return (a * b) / gcd(a , b);}
ll fact(ll n) { return n > 1?(n * fact(n-1)):1;}
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void IO(string s = "") { if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } }
const int N = 2024;
const int inf = 1e9;
const int MOD = 1e9+7;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
const int dr[8] = {1,1,0,-1,-1,-1, 0, 1}, dc[8] = {0,1,1, 1, 0,-1,-1,-1}; // S,SE,E,NE,N,NW,W,SW
const long long INF = 1e18;
const double EPS = 1e-9;
const double PI = 3.14159265358979323846;
/** |||||||||||||||||||||||||||||||| SOLUTION |||||||||||||||||||||||||||||||| **/
ll n, m; char grid[N][N]; ll d[N][N];
ll vis[N][N]; ll sx, sy, hx, hy;
bool bfs(ll v)
{
if(v*m>=d[sx][sy]) return false;
memset(vis, 0, sizeof(vis)); vis[sx][sy]=1;
deque<pair<ll, pl>>q; q.pb(mp(v*m, mp(sx, sy)));
while(!q.empty()) {
ll w=q.front().ff, a=q.front().ss.ff, b=q.front().ss.ss; q.pop_front();
if(grid[a][b]=='D') return true;
for(ll l=0;l<4;l++) {
ll x=a+nx[l], y=b+ny[l];
if(x>=0&&x<n&&y>=0&&y<n) {
if(grid[x][y]=='T'||w+1>=d[x][y]||vis[x][y]) continue;
q.pb(mp(w+1, mp(x, y))); vis[x][y]=1;
}
}
}
return false;
}
void solve()
{
/// JUST KEEP GOING
cin>>n>>m; deque<pl>q; memset(d, -1, sizeof d);
for(ll l=0;l<n;l++) {
for(ll i=0;i<n;i++) {
cin>>grid[l][i];
if(grid[l][i]=='H') { d[l][i]=0; q.pb(mp(l, i)); }
else if(grid[l][i]=='M') { sx=l; sy=i; grid[l][i]=='G'; }
else if(grid[l][i]=='D') { hx=l; hy=i; }
}
}
while(!q.empty()) {
ll a=q.front().ff, b=q.front().ss; q.pop_front();
for(ll l=0;l<4;l++) {
ll x=a+nx[l], y=b+ny[l];
if(x>=0&&x<n&&y>=0&&y<n) {
if(d[x][y]==-1&&grid[x][y]=='G') {
d[x][y]=d[a][b]+m; q.push_back(mp(x, y));
}
}
}
}
ll lo=-1, hi=1e9; d[hx][hy]=1e9;
while(hi-lo>1) {
ll mid=(lo+hi)/2;
if(bfs(mid)) { lo=mid; }
else hi=mid;
}
cout<<lo<<'\n';
}
int32_t main()
{
FAST; /// IO("lasers");
int t; t=1;
while(t--) {
solve();
}
return 0;
}
/*** Stuff you should look for :
* int overflow, array bounds.
* special cases (n=1?).
* Observe The Constraints.
* do smth instead of nothing and stay organized.
* Reformulate The Problem Into Something More Theoretical.
* WRITE STUFF DOWN.
* DON'T GET STUCK ON ONE APPROACH.
* Don't Spend Too Much Time On The Problem: Move On!
* If You Don't Fight You Cannot Win.
***/
Compilation message
mecho.cpp: In function 'void solve()':
mecho.cpp:74:62: warning: statement has no effect [-Wunused-value]
74 | else if(grid[l][i]=='M') { sx=l; sy=i; grid[l][i]=='G'; }
| ~~~~~~~~~~^~~~~
mecho.cpp: In function 'void setIn(std::string)':
mecho.cpp:30:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | void setIn(string s) { freopen(s.c_str(),"r",stdin); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
mecho.cpp: In function 'void setOut(std::string)':
mecho.cpp:31:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | void setOut(string s) { freopen(s.c_str(),"w",stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
32408 KB |
Output isn't correct |
2 |
Incorrect |
17 ms |
32368 KB |
Output isn't correct |
3 |
Incorrect |
17 ms |
32332 KB |
Output isn't correct |
4 |
Incorrect |
16 ms |
32324 KB |
Output isn't correct |
5 |
Incorrect |
17 ms |
32312 KB |
Output isn't correct |
6 |
Incorrect |
16 ms |
32392 KB |
Output isn't correct |
7 |
Incorrect |
46 ms |
34116 KB |
Output isn't correct |
8 |
Correct |
16 ms |
32332 KB |
Output is correct |
9 |
Incorrect |
17 ms |
32332 KB |
Output isn't correct |
10 |
Incorrect |
17 ms |
32392 KB |
Output isn't correct |
11 |
Incorrect |
16 ms |
32348 KB |
Output isn't correct |
12 |
Incorrect |
18 ms |
32512 KB |
Output isn't correct |
13 |
Incorrect |
17 ms |
32476 KB |
Output isn't correct |
14 |
Correct |
17 ms |
32416 KB |
Output is correct |
15 |
Incorrect |
16 ms |
32412 KB |
Output isn't correct |
16 |
Incorrect |
17 ms |
32332 KB |
Output isn't correct |
17 |
Incorrect |
17 ms |
32380 KB |
Output isn't correct |
18 |
Incorrect |
17 ms |
32388 KB |
Output isn't correct |
19 |
Incorrect |
17 ms |
32452 KB |
Output isn't correct |
20 |
Incorrect |
17 ms |
32424 KB |
Output isn't correct |
21 |
Incorrect |
17 ms |
32460 KB |
Output isn't correct |
22 |
Incorrect |
16 ms |
32396 KB |
Output isn't correct |
23 |
Incorrect |
17 ms |
32460 KB |
Output isn't correct |
24 |
Incorrect |
17 ms |
32420 KB |
Output isn't correct |
25 |
Incorrect |
18 ms |
32448 KB |
Output isn't correct |
26 |
Incorrect |
16 ms |
32404 KB |
Output isn't correct |
27 |
Incorrect |
17 ms |
32532 KB |
Output isn't correct |
28 |
Incorrect |
17 ms |
32476 KB |
Output isn't correct |
29 |
Incorrect |
17 ms |
32476 KB |
Output isn't correct |
30 |
Incorrect |
17 ms |
32432 KB |
Output isn't correct |
31 |
Incorrect |
18 ms |
32444 KB |
Output isn't correct |
32 |
Incorrect |
17 ms |
32460 KB |
Output isn't correct |
33 |
Incorrect |
20 ms |
33076 KB |
Output isn't correct |
34 |
Incorrect |
21 ms |
32964 KB |
Output isn't correct |
35 |
Incorrect |
22 ms |
32992 KB |
Output isn't correct |
36 |
Incorrect |
23 ms |
33108 KB |
Output isn't correct |
37 |
Incorrect |
20 ms |
33100 KB |
Output isn't correct |
38 |
Incorrect |
27 ms |
33196 KB |
Output isn't correct |
39 |
Incorrect |
23 ms |
33184 KB |
Output isn't correct |
40 |
Incorrect |
22 ms |
33220 KB |
Output isn't correct |
41 |
Incorrect |
26 ms |
33284 KB |
Output isn't correct |
42 |
Incorrect |
23 ms |
33356 KB |
Output isn't correct |
43 |
Incorrect |
24 ms |
33292 KB |
Output isn't correct |
44 |
Incorrect |
29 ms |
33356 KB |
Output isn't correct |
45 |
Incorrect |
26 ms |
33388 KB |
Output isn't correct |
46 |
Incorrect |
24 ms |
33404 KB |
Output isn't correct |
47 |
Incorrect |
30 ms |
33456 KB |
Output isn't correct |
48 |
Incorrect |
28 ms |
33464 KB |
Output isn't correct |
49 |
Incorrect |
26 ms |
33532 KB |
Output isn't correct |
50 |
Incorrect |
32 ms |
33584 KB |
Output isn't correct |
51 |
Incorrect |
27 ms |
33604 KB |
Output isn't correct |
52 |
Incorrect |
29 ms |
33640 KB |
Output isn't correct |
53 |
Incorrect |
35 ms |
33680 KB |
Output isn't correct |
54 |
Incorrect |
30 ms |
33740 KB |
Output isn't correct |
55 |
Incorrect |
32 ms |
33688 KB |
Output isn't correct |
56 |
Incorrect |
40 ms |
33732 KB |
Output isn't correct |
57 |
Incorrect |
32 ms |
33804 KB |
Output isn't correct |
58 |
Incorrect |
32 ms |
33812 KB |
Output isn't correct |
59 |
Incorrect |
45 ms |
33888 KB |
Output isn't correct |
60 |
Incorrect |
34 ms |
33924 KB |
Output isn't correct |
61 |
Incorrect |
33 ms |
33868 KB |
Output isn't correct |
62 |
Incorrect |
50 ms |
33984 KB |
Output isn't correct |
63 |
Incorrect |
44 ms |
33868 KB |
Output isn't correct |
64 |
Incorrect |
44 ms |
33904 KB |
Output isn't correct |
65 |
Incorrect |
44 ms |
34068 KB |
Output isn't correct |
66 |
Incorrect |
47 ms |
33868 KB |
Output isn't correct |
67 |
Correct |
45 ms |
33936 KB |
Output is correct |
68 |
Incorrect |
48 ms |
33920 KB |
Output isn't correct |
69 |
Incorrect |
49 ms |
33996 KB |
Output isn't correct |
70 |
Incorrect |
53 ms |
33996 KB |
Output isn't correct |
71 |
Incorrect |
49 ms |
33960 KB |
Output isn't correct |
72 |
Incorrect |
49 ms |
34024 KB |
Output isn't correct |
73 |
Incorrect |
56 ms |
34488 KB |
Output isn't correct |
74 |
Incorrect |
57 ms |
34456 KB |
Output isn't correct |
75 |
Incorrect |
54 ms |
34500 KB |
Output isn't correct |
76 |
Incorrect |
55 ms |
34444 KB |
Output isn't correct |
77 |
Incorrect |
58 ms |
34508 KB |
Output isn't correct |
78 |
Correct |
47 ms |
34464 KB |
Output is correct |
79 |
Incorrect |
51 ms |
34456 KB |
Output isn't correct |
80 |
Incorrect |
47 ms |
34396 KB |
Output isn't correct |
81 |
Incorrect |
48 ms |
34380 KB |
Output isn't correct |
82 |
Incorrect |
50 ms |
34460 KB |
Output isn't correct |
83 |
Incorrect |
47 ms |
34388 KB |
Output isn't correct |
84 |
Incorrect |
51 ms |
34276 KB |
Output isn't correct |
85 |
Incorrect |
50 ms |
34388 KB |
Output isn't correct |
86 |
Incorrect |
53 ms |
34320 KB |
Output isn't correct |
87 |
Incorrect |
54 ms |
34356 KB |
Output isn't correct |
88 |
Incorrect |
49 ms |
34284 KB |
Output isn't correct |
89 |
Incorrect |
48 ms |
34252 KB |
Output isn't correct |
90 |
Incorrect |
46 ms |
34252 KB |
Output isn't correct |
91 |
Incorrect |
47 ms |
34284 KB |
Output isn't correct |
92 |
Incorrect |
47 ms |
34296 KB |
Output isn't correct |