#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;
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); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
32332 KB |
Output is correct |
2 |
Correct |
37 ms |
32396 KB |
Output is correct |
3 |
Correct |
32 ms |
32424 KB |
Output is correct |
4 |
Correct |
36 ms |
32428 KB |
Output is correct |
5 |
Correct |
40 ms |
32332 KB |
Output is correct |
6 |
Correct |
44 ms |
32332 KB |
Output is correct |
7 |
Correct |
241 ms |
42440 KB |
Output is correct |
8 |
Correct |
45 ms |
32444 KB |
Output is correct |
9 |
Correct |
49 ms |
32460 KB |
Output is correct |
10 |
Correct |
49 ms |
32460 KB |
Output is correct |
11 |
Correct |
48 ms |
32352 KB |
Output is correct |
12 |
Incorrect |
64 ms |
32756 KB |
Output isn't correct |
13 |
Incorrect |
60 ms |
32692 KB |
Output isn't correct |
14 |
Correct |
63 ms |
32716 KB |
Output is correct |
15 |
Correct |
54 ms |
32500 KB |
Output is correct |
16 |
Correct |
50 ms |
32500 KB |
Output is correct |
17 |
Correct |
53 ms |
32492 KB |
Output is correct |
18 |
Correct |
59 ms |
32452 KB |
Output is correct |
19 |
Correct |
56 ms |
32568 KB |
Output is correct |
20 |
Correct |
52 ms |
32572 KB |
Output is correct |
21 |
Correct |
59 ms |
32588 KB |
Output is correct |
22 |
Correct |
60 ms |
32588 KB |
Output is correct |
23 |
Correct |
64 ms |
32688 KB |
Output is correct |
24 |
Correct |
63 ms |
32708 KB |
Output is correct |
25 |
Correct |
64 ms |
32768 KB |
Output is correct |
26 |
Correct |
64 ms |
32780 KB |
Output is correct |
27 |
Correct |
66 ms |
32716 KB |
Output is correct |
28 |
Correct |
68 ms |
32820 KB |
Output is correct |
29 |
Correct |
67 ms |
32844 KB |
Output is correct |
30 |
Correct |
69 ms |
32844 KB |
Output is correct |
31 |
Correct |
67 ms |
32868 KB |
Output is correct |
32 |
Correct |
69 ms |
32880 KB |
Output is correct |
33 |
Correct |
84 ms |
35404 KB |
Output is correct |
34 |
Correct |
89 ms |
35340 KB |
Output is correct |
35 |
Correct |
121 ms |
35448 KB |
Output is correct |
36 |
Correct |
93 ms |
35916 KB |
Output is correct |
37 |
Correct |
93 ms |
36032 KB |
Output is correct |
38 |
Correct |
126 ms |
35976 KB |
Output is correct |
39 |
Correct |
103 ms |
36648 KB |
Output is correct |
40 |
Correct |
97 ms |
36684 KB |
Output is correct |
41 |
Correct |
150 ms |
36672 KB |
Output is correct |
42 |
Correct |
96 ms |
37324 KB |
Output is correct |
43 |
Correct |
97 ms |
37336 KB |
Output is correct |
44 |
Correct |
175 ms |
37344 KB |
Output is correct |
45 |
Correct |
103 ms |
37960 KB |
Output is correct |
46 |
Correct |
99 ms |
38056 KB |
Output is correct |
47 |
Correct |
176 ms |
38072 KB |
Output is correct |
48 |
Correct |
105 ms |
38760 KB |
Output is correct |
49 |
Correct |
106 ms |
38920 KB |
Output is correct |
50 |
Correct |
213 ms |
38808 KB |
Output is correct |
51 |
Correct |
110 ms |
39576 KB |
Output is correct |
52 |
Correct |
107 ms |
39564 KB |
Output is correct |
53 |
Correct |
224 ms |
39652 KB |
Output is correct |
54 |
Correct |
115 ms |
40392 KB |
Output is correct |
55 |
Correct |
113 ms |
40348 KB |
Output is correct |
56 |
Correct |
252 ms |
40508 KB |
Output is correct |
57 |
Correct |
113 ms |
41404 KB |
Output is correct |
58 |
Correct |
111 ms |
41284 KB |
Output is correct |
59 |
Correct |
279 ms |
41248 KB |
Output is correct |
60 |
Correct |
122 ms |
42180 KB |
Output is correct |
61 |
Correct |
114 ms |
42180 KB |
Output is correct |
62 |
Correct |
312 ms |
42120 KB |
Output is correct |
63 |
Correct |
220 ms |
42240 KB |
Output is correct |
64 |
Correct |
347 ms |
42084 KB |
Output is correct |
65 |
Correct |
279 ms |
42168 KB |
Output is correct |
66 |
Correct |
274 ms |
42176 KB |
Output is correct |
67 |
Correct |
234 ms |
42180 KB |
Output is correct |
68 |
Correct |
154 ms |
42228 KB |
Output is correct |
69 |
Correct |
159 ms |
42224 KB |
Output is correct |
70 |
Correct |
138 ms |
42180 KB |
Output is correct |
71 |
Correct |
154 ms |
42168 KB |
Output is correct |
72 |
Incorrect |
140 ms |
42228 KB |
Output isn't correct |
73 |
Incorrect |
232 ms |
42804 KB |
Output isn't correct |
74 |
Correct |
183 ms |
42640 KB |
Output is correct |
75 |
Correct |
205 ms |
42724 KB |
Output is correct |
76 |
Correct |
186 ms |
42728 KB |
Output is correct |
77 |
Correct |
184 ms |
42700 KB |
Output is correct |
78 |
Correct |
217 ms |
42572 KB |
Output is correct |
79 |
Correct |
167 ms |
42604 KB |
Output is correct |
80 |
Correct |
194 ms |
42564 KB |
Output is correct |
81 |
Correct |
199 ms |
42616 KB |
Output is correct |
82 |
Correct |
189 ms |
42692 KB |
Output is correct |
83 |
Correct |
223 ms |
42672 KB |
Output is correct |
84 |
Correct |
207 ms |
42584 KB |
Output is correct |
85 |
Correct |
249 ms |
42508 KB |
Output is correct |
86 |
Correct |
231 ms |
42564 KB |
Output is correct |
87 |
Correct |
217 ms |
42544 KB |
Output is correct |
88 |
Correct |
228 ms |
42492 KB |
Output is correct |
89 |
Correct |
224 ms |
42576 KB |
Output is correct |
90 |
Correct |
238 ms |
42440 KB |
Output is correct |
91 |
Correct |
233 ms |
42444 KB |
Output is correct |
92 |
Correct |
251 ms |
42492 KB |
Output is correct |