#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <unordered_set>
#include <map>
#include <string>
#include <tuple>
#include <numeric>
#include <climits>
#include <bitset>
#include <iomanip>
#include <random>
#include <ctime>
using namespace std;
typedef long long ll;
const ll INF = 1e14;
const ll MOD = 1e9 + 7;
const ll MAXN = 2e5 + 1;
ll fact[MAXN];
void precompute()
{
fact[0] = 1;
for (ll i = 1; i < MAXN; i++) {
fact[i] = (i * fact[i - 1]) % MOD;
}
}
ll powmod(ll a, ll b){
a %= MOD;
if (a == 0) return 0;
ll product = 1;
while(b > 0){
if (b&1){ // you can also use b % 2 == 1
product *= a;
product %= MOD;
--b;
}
a *= a;
a %= MOD;
b /= 2; // you can also use b >> 1
}
return product;
}
ll max(ll a, ll b) {
if (a > b) {
return a;
} else {
return b;
}
}
ll min(ll a, ll b) {
if (a < b) {
return a;
} else {
return b;
}
}
//calculates the inverse of a number mod MOD
ll inv(ll a) {
return powmod(a, MOD - 2);
}
struct State{
ll cr, cc, cd;
};
ll dr[4] = {0, 1, 0, -1};
ll dc[4] = {1, 0, -1, 0};
void solve(ll ca) {
ll n, s;
cin >> n >> s;
char grid[n][n];
pair<ll, ll> sc;
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'M') {
sc = {i, j};
}
}
}
ll dist[n][n];
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < n; j++) {
dist[i][j] = INF;
}
}
queue<State> q;
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < n; j++) {
if (grid[i][j] == 'H') {
q.push({i, j, 0});
}
}
}
while (!q.empty()) {
State cur = q.front();
q.pop();
if (dist[cur.cr][cur.cc] <= cur.cd) {
continue;
}
dist[cur.cr][cur.cc] = cur.cd;
for (ll k = 0; k < 4; k++) {
ll nr = cur.cr + dr[k];
ll nc = cur.cc + dc[k];
if (nr >= 0 && nr < n && nc >= 0 && nc < n && grid[nr][nc] != 'T' && grid[nr][nc] != 'D' && grid[nr][nc] != 'H') {
q.push({nr, nc, cur.cd+1});
}
}
}
/*
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < n; j++) {
if (dist[i][j] == INF) {
cout << -1 << " ";
continue;
}
cout << dist[i][j] << " ";
}
cout << endl;
}*/
ll lo = 0; ll hi = INF;
lo--;
while (lo < hi) {
ll mid = lo + (hi - lo + 1)/2;
//mid = 2;
bool works = false;
bool visited[n][n];
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < n; j++) {
visited[i][j] = false;
}
}
queue<State> q2;
q2.push({sc.first, sc.second, s*mid});
while (!q2.empty()) {
State cur = q2.front();
q2.pop();
/*if (cur.cr == 3 && cur.cc == 6) {
cout << "I AM HERE" << endl;
}*/
if (grid[cur.cr][cur.cc] == 'D') {
works = true;
break;
}
/*if (cur.cr == 3 && cur.cc == 6) {
cout << "I AM HERE" << endl;
}*/
ll nummin = cur.cd/s;
if (nummin >= dist[cur.cr][cur.cc]) {
continue;
}
if (visited[cur.cr][cur.cc]) {
continue;
}
//cout << cur.cr << " " << cur.cc << endl;
visited[cur.cr][cur.cc] = true;
for (ll k = 0; k < 4; k++) {
ll nr = cur.cr + dr[k];
ll nc = cur.cc + dc[k];
if (nr >= 0 && nr < n && nc >= 0 && nc < n && grid[nr][nc] != 'T') {
q2.push({nr, nc, cur.cd+1});
}
}
}
if (works) {
lo = mid;
} else {
hi = mid - 1;
}
/*cout << works << endl;
return;*/
}
cout << lo << endl;
}
int main() {
mt19937 rng(0);
// Fast IO
ios::sync_with_stdio(false);
cin.tie(nullptr);
/*
freopen("gravity.in", "r", stdin);
freopen("gravity.out", "w", stdout);
*/
ll t = 1;
//cin >> t;
ll co = 0;
while (t--) {
solve(co);
co++;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
208 ms |
8392 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
324 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
328 KB |
Output is correct |
31 |
Correct |
1 ms |
328 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
5 ms |
1620 KB |
Output is correct |
34 |
Correct |
6 ms |
1620 KB |
Output is correct |
35 |
Correct |
38 ms |
1620 KB |
Output is correct |
36 |
Correct |
7 ms |
2004 KB |
Output is correct |
37 |
Correct |
7 ms |
2004 KB |
Output is correct |
38 |
Correct |
57 ms |
2112 KB |
Output is correct |
39 |
Correct |
7 ms |
2388 KB |
Output is correct |
40 |
Correct |
8 ms |
2380 KB |
Output is correct |
41 |
Correct |
64 ms |
2516 KB |
Output is correct |
42 |
Correct |
9 ms |
2896 KB |
Output is correct |
43 |
Correct |
10 ms |
3020 KB |
Output is correct |
44 |
Correct |
80 ms |
3028 KB |
Output is correct |
45 |
Correct |
11 ms |
3540 KB |
Output is correct |
46 |
Correct |
11 ms |
3540 KB |
Output is correct |
47 |
Correct |
107 ms |
3612 KB |
Output is correct |
48 |
Correct |
12 ms |
4180 KB |
Output is correct |
49 |
Correct |
14 ms |
4104 KB |
Output is correct |
50 |
Correct |
116 ms |
4156 KB |
Output is correct |
51 |
Correct |
15 ms |
4840 KB |
Output is correct |
52 |
Correct |
16 ms |
4864 KB |
Output is correct |
53 |
Correct |
134 ms |
4872 KB |
Output is correct |
54 |
Correct |
18 ms |
5588 KB |
Output is correct |
55 |
Correct |
19 ms |
5496 KB |
Output is correct |
56 |
Correct |
170 ms |
5684 KB |
Output is correct |
57 |
Correct |
20 ms |
6348 KB |
Output is correct |
58 |
Correct |
20 ms |
6356 KB |
Output is correct |
59 |
Correct |
207 ms |
6524 KB |
Output is correct |
60 |
Correct |
22 ms |
7072 KB |
Output is correct |
61 |
Correct |
25 ms |
7088 KB |
Output is correct |
62 |
Correct |
240 ms |
7388 KB |
Output is correct |
63 |
Correct |
122 ms |
7216 KB |
Output is correct |
64 |
Correct |
220 ms |
7116 KB |
Output is correct |
65 |
Correct |
237 ms |
7224 KB |
Output is correct |
66 |
Correct |
170 ms |
7116 KB |
Output is correct |
67 |
Correct |
143 ms |
7112 KB |
Output is correct |
68 |
Correct |
59 ms |
7372 KB |
Output is correct |
69 |
Correct |
54 ms |
7264 KB |
Output is correct |
70 |
Correct |
52 ms |
7500 KB |
Output is correct |
71 |
Correct |
46 ms |
7376 KB |
Output is correct |
72 |
Correct |
39 ms |
7372 KB |
Output is correct |
73 |
Correct |
75 ms |
10372 KB |
Output is correct |
74 |
Correct |
130 ms |
10440 KB |
Output is correct |
75 |
Correct |
147 ms |
10384 KB |
Output is correct |
76 |
Correct |
155 ms |
10144 KB |
Output is correct |
77 |
Correct |
149 ms |
10444 KB |
Output is correct |
78 |
Correct |
168 ms |
9844 KB |
Output is correct |
79 |
Correct |
151 ms |
10316 KB |
Output is correct |
80 |
Correct |
129 ms |
10060 KB |
Output is correct |
81 |
Correct |
150 ms |
10040 KB |
Output is correct |
82 |
Correct |
143 ms |
10272 KB |
Output is correct |
83 |
Correct |
172 ms |
9672 KB |
Output is correct |
84 |
Correct |
149 ms |
9356 KB |
Output is correct |
85 |
Correct |
152 ms |
9056 KB |
Output is correct |
86 |
Correct |
170 ms |
9064 KB |
Output is correct |
87 |
Correct |
155 ms |
9476 KB |
Output is correct |
88 |
Correct |
170 ms |
9012 KB |
Output is correct |
89 |
Correct |
190 ms |
9112 KB |
Output is correct |
90 |
Correct |
179 ms |
8996 KB |
Output is correct |
91 |
Correct |
169 ms |
8676 KB |
Output is correct |
92 |
Correct |
168 ms |
8992 KB |
Output is correct |