#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 = 824;
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;
for(ll l=0;l<n;l++) {
for(ll i=0;i<n;i++) {
cin>>grid[l][i]; d[l][i]=inf;
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]==inf&&grid[x][y]=='G') {
d[x][y]=d[a][b]+m; q.push_back(mp(x, y));
}
}
}
}
ll lo=-1, hi=n*n*2; d[hx][hy]=n*n*m;
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 |
Correct |
5 ms |
5580 KB |
Output is correct |
2 |
Correct |
5 ms |
5580 KB |
Output is correct |
3 |
Correct |
4 ms |
5580 KB |
Output is correct |
4 |
Correct |
5 ms |
5580 KB |
Output is correct |
5 |
Correct |
5 ms |
5580 KB |
Output is correct |
6 |
Correct |
5 ms |
5572 KB |
Output is correct |
7 |
Correct |
145 ms |
12280 KB |
Output is correct |
8 |
Correct |
5 ms |
5708 KB |
Output is correct |
9 |
Correct |
5 ms |
5708 KB |
Output is correct |
10 |
Correct |
5 ms |
5708 KB |
Output is correct |
11 |
Correct |
5 ms |
5692 KB |
Output is correct |
12 |
Incorrect |
7 ms |
5952 KB |
Output isn't correct |
13 |
Incorrect |
6 ms |
5912 KB |
Output isn't correct |
14 |
Correct |
6 ms |
5952 KB |
Output is correct |
15 |
Correct |
5 ms |
5708 KB |
Output is correct |
16 |
Correct |
5 ms |
5708 KB |
Output is correct |
17 |
Correct |
5 ms |
5708 KB |
Output is correct |
18 |
Correct |
6 ms |
5708 KB |
Output is correct |
19 |
Correct |
7 ms |
5788 KB |
Output is correct |
20 |
Correct |
6 ms |
5796 KB |
Output is correct |
21 |
Correct |
6 ms |
5836 KB |
Output is correct |
22 |
Correct |
6 ms |
5836 KB |
Output is correct |
23 |
Correct |
6 ms |
5828 KB |
Output is correct |
24 |
Correct |
6 ms |
5908 KB |
Output is correct |
25 |
Correct |
6 ms |
5964 KB |
Output is correct |
26 |
Correct |
6 ms |
5964 KB |
Output is correct |
27 |
Correct |
6 ms |
5964 KB |
Output is correct |
28 |
Correct |
6 ms |
5964 KB |
Output is correct |
29 |
Correct |
7 ms |
5968 KB |
Output is correct |
30 |
Correct |
7 ms |
6012 KB |
Output is correct |
31 |
Correct |
8 ms |
5964 KB |
Output is correct |
32 |
Correct |
7 ms |
5964 KB |
Output is correct |
33 |
Correct |
11 ms |
8216 KB |
Output is correct |
34 |
Correct |
12 ms |
8300 KB |
Output is correct |
35 |
Correct |
48 ms |
8300 KB |
Output is correct |
36 |
Correct |
13 ms |
8652 KB |
Output is correct |
37 |
Correct |
13 ms |
8700 KB |
Output is correct |
38 |
Correct |
51 ms |
8708 KB |
Output is correct |
39 |
Correct |
14 ms |
9044 KB |
Output is correct |
40 |
Correct |
14 ms |
9008 KB |
Output is correct |
41 |
Correct |
64 ms |
9036 KB |
Output is correct |
42 |
Correct |
16 ms |
9420 KB |
Output is correct |
43 |
Correct |
16 ms |
9464 KB |
Output is correct |
44 |
Correct |
82 ms |
9600 KB |
Output is correct |
45 |
Correct |
19 ms |
9868 KB |
Output is correct |
46 |
Correct |
21 ms |
9936 KB |
Output is correct |
47 |
Correct |
92 ms |
9940 KB |
Output is correct |
48 |
Correct |
19 ms |
10240 KB |
Output is correct |
49 |
Correct |
20 ms |
10292 KB |
Output is correct |
50 |
Correct |
112 ms |
10348 KB |
Output is correct |
51 |
Correct |
27 ms |
10692 KB |
Output is correct |
52 |
Correct |
22 ms |
10700 KB |
Output is correct |
53 |
Correct |
150 ms |
10780 KB |
Output is correct |
54 |
Correct |
23 ms |
11068 KB |
Output is correct |
55 |
Correct |
24 ms |
11136 KB |
Output is correct |
56 |
Correct |
175 ms |
11200 KB |
Output is correct |
57 |
Correct |
27 ms |
11576 KB |
Output is correct |
58 |
Correct |
25 ms |
11596 KB |
Output is correct |
59 |
Correct |
181 ms |
11736 KB |
Output is correct |
60 |
Correct |
29 ms |
12056 KB |
Output is correct |
61 |
Correct |
27 ms |
12012 KB |
Output is correct |
62 |
Correct |
210 ms |
12008 KB |
Output is correct |
63 |
Correct |
139 ms |
11976 KB |
Output is correct |
64 |
Correct |
249 ms |
12060 KB |
Output is correct |
65 |
Correct |
210 ms |
12136 KB |
Output is correct |
66 |
Correct |
163 ms |
12012 KB |
Output is correct |
67 |
Correct |
145 ms |
12060 KB |
Output is correct |
68 |
Correct |
63 ms |
12028 KB |
Output is correct |
69 |
Correct |
73 ms |
12068 KB |
Output is correct |
70 |
Correct |
48 ms |
12108 KB |
Output is correct |
71 |
Correct |
57 ms |
12036 KB |
Output is correct |
72 |
Incorrect |
51 ms |
12076 KB |
Output isn't correct |
73 |
Incorrect |
114 ms |
12692 KB |
Output isn't correct |
74 |
Correct |
91 ms |
12508 KB |
Output is correct |
75 |
Correct |
113 ms |
12560 KB |
Output is correct |
76 |
Correct |
97 ms |
12612 KB |
Output is correct |
77 |
Correct |
101 ms |
12588 KB |
Output is correct |
78 |
Correct |
132 ms |
12568 KB |
Output is correct |
79 |
Correct |
74 ms |
12552 KB |
Output is correct |
80 |
Correct |
94 ms |
12500 KB |
Output is correct |
81 |
Correct |
109 ms |
12556 KB |
Output is correct |
82 |
Correct |
93 ms |
12484 KB |
Output is correct |
83 |
Correct |
134 ms |
12476 KB |
Output is correct |
84 |
Correct |
110 ms |
12436 KB |
Output is correct |
85 |
Correct |
143 ms |
12484 KB |
Output is correct |
86 |
Correct |
129 ms |
12480 KB |
Output is correct |
87 |
Correct |
119 ms |
12424 KB |
Output is correct |
88 |
Correct |
130 ms |
12316 KB |
Output is correct |
89 |
Correct |
135 ms |
12380 KB |
Output is correct |
90 |
Correct |
142 ms |
12380 KB |
Output is correct |
91 |
Correct |
134 ms |
12356 KB |
Output is correct |
92 |
Correct |
145 ms |
12384 KB |
Output is correct |