#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define in insert
#define all(x) x.begin(),x.end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define int long long
typedef long long ll;
typedef vector<int> vi;
typedef set<int> si;
typedef multiset<int> msi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
template<typename T, typename U>
ostream & operator << (ostream &out, const pair<T, U> &c) {
out << c.first << ' ' << c.second;
return out;
}
template<typename T>
ostream & operator << (ostream &out, vector<T> &v) {
const int sz = v.size();
for (int i = 0; i < sz; i++) {
if (i) out << ' ';
out << v[i];
}
return out;
}
template<typename T>
istream & operator >> (istream &in, vector<T> &v) {
for (T &x : v) in >> x;
return in;
}
template<typename T>
void mxx(T &a, T b){if(b > a) a = b;}
template<typename T>
void mnn(T &a, T b){if(b < a) a = b;}
const int mxn = 810, inf = 1e9 + 10;
int g[mxn][mxn], n, s, bee[mxn][mxn], mn[mxn][mxn];
pii en, st;
vector<pii> h;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool ch(int m) {
queue<pair<int, int>> q;
if(bee[st.ff][st.ss] * s < m) return 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
mn[i][j] = inf;
mn[st.ff][st.ss] = m;
q.push(st);
while(q.size()) {
int x = q.front().ff;
int y = q.front().ss;
q.pop();
for(int i = 0; i < 4; i++) {
int fx = x + dx[i];
int fy = y + dy[i];
if(max(fx, fy) > n || min(fx, fy) < 1 || mn[fx][fy] != inf || g[fx][fy] || mn[x][y] + 1 > bee[x][y] * s) continue;
mn[fx][fy] = mn[x][y] + 1;
q.push({fx, fy});
}
}
return mn[en.ff][en.ss] != inf;
}
void go() {
cin >> n >> s;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
char ch;
cin >> ch;
if(ch == 'M') st = {i, j};
else if(ch == 'D') en = {i, j};
else if(ch == 'H') h.pb({i, j});
else if(ch == 'T') g[i][j] = 1;
}
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
bee[i][j] = inf;
queue<pair<int, int>> q;
for(auto it : h)
q.push(it), bee[it.ff][it.ss] = 0;
while(q.size()) {
int x = q.front().ff;
int y = q.front().ss;
q.pop();
for(int i = 0; i < 4; i++) {
int fx = x + dx[i];
int fy = y + dy[i];
if(max(fx, fy) > n || min(fx, fy) < 1 || bee[fx][fy] != inf || g[fx][fy] || en == make_pair(fx, fy)) continue;
bee[fx][fy] = bee[x][y] + 1;
q.push({fx, fy});
}
}
int l = -1, r = (int)1e9;
while(r - l > 1) {
int m = (r + l) / 2;
if(ch(m)) l = m;
else r = m;
}
if(l != -1) l /= s;
cout << l << '\n';
}
signed main() {
// #ifndef ONO
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
// #endif
fast;
int t = 1;
// cin >> t;
while(t--) {
go();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
125 ms |
12016 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
852 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
1108 KB |
Output is correct |
27 |
Correct |
1 ms |
980 KB |
Output is correct |
28 |
Correct |
1 ms |
1108 KB |
Output is correct |
29 |
Correct |
1 ms |
1108 KB |
Output is correct |
30 |
Correct |
1 ms |
1236 KB |
Output is correct |
31 |
Correct |
1 ms |
1108 KB |
Output is correct |
32 |
Correct |
1 ms |
1364 KB |
Output is correct |
33 |
Correct |
7 ms |
5844 KB |
Output is correct |
34 |
Correct |
8 ms |
6612 KB |
Output is correct |
35 |
Correct |
38 ms |
6356 KB |
Output is correct |
36 |
Correct |
8 ms |
6740 KB |
Output is correct |
37 |
Correct |
8 ms |
7636 KB |
Output is correct |
38 |
Correct |
51 ms |
7316 KB |
Output is correct |
39 |
Correct |
13 ms |
7636 KB |
Output is correct |
40 |
Correct |
11 ms |
8652 KB |
Output is correct |
41 |
Correct |
65 ms |
8148 KB |
Output is correct |
42 |
Correct |
10 ms |
8560 KB |
Output is correct |
43 |
Correct |
16 ms |
9632 KB |
Output is correct |
44 |
Correct |
95 ms |
9092 KB |
Output is correct |
45 |
Correct |
13 ms |
9616 KB |
Output is correct |
46 |
Correct |
16 ms |
10704 KB |
Output is correct |
47 |
Correct |
110 ms |
9980 KB |
Output is correct |
48 |
Correct |
16 ms |
10452 KB |
Output is correct |
49 |
Correct |
22 ms |
11648 KB |
Output is correct |
50 |
Correct |
145 ms |
10880 KB |
Output is correct |
51 |
Correct |
18 ms |
11540 KB |
Output is correct |
52 |
Correct |
18 ms |
12672 KB |
Output is correct |
53 |
Correct |
164 ms |
11776 KB |
Output is correct |
54 |
Correct |
22 ms |
12500 KB |
Output is correct |
55 |
Correct |
25 ms |
13568 KB |
Output is correct |
56 |
Correct |
205 ms |
12676 KB |
Output is correct |
57 |
Correct |
22 ms |
13532 KB |
Output is correct |
58 |
Correct |
26 ms |
14568 KB |
Output is correct |
59 |
Correct |
246 ms |
13676 KB |
Output is correct |
60 |
Correct |
28 ms |
14596 KB |
Output is correct |
61 |
Correct |
25 ms |
15492 KB |
Output is correct |
62 |
Correct |
305 ms |
14452 KB |
Output is correct |
63 |
Correct |
132 ms |
15444 KB |
Output is correct |
64 |
Correct |
316 ms |
15576 KB |
Output is correct |
65 |
Correct |
193 ms |
15564 KB |
Output is correct |
66 |
Correct |
151 ms |
15564 KB |
Output is correct |
67 |
Correct |
126 ms |
15572 KB |
Output is correct |
68 |
Correct |
81 ms |
15620 KB |
Output is correct |
69 |
Correct |
71 ms |
15620 KB |
Output is correct |
70 |
Correct |
55 ms |
15544 KB |
Output is correct |
71 |
Correct |
54 ms |
15620 KB |
Output is correct |
72 |
Correct |
52 ms |
15544 KB |
Output is correct |
73 |
Correct |
52 ms |
15948 KB |
Output is correct |
74 |
Correct |
96 ms |
15896 KB |
Output is correct |
75 |
Correct |
84 ms |
16012 KB |
Output is correct |
76 |
Correct |
80 ms |
15944 KB |
Output is correct |
77 |
Correct |
88 ms |
16032 KB |
Output is correct |
78 |
Correct |
100 ms |
15804 KB |
Output is correct |
79 |
Correct |
91 ms |
15800 KB |
Output is correct |
80 |
Correct |
78 ms |
15888 KB |
Output is correct |
81 |
Correct |
90 ms |
15820 KB |
Output is correct |
82 |
Correct |
95 ms |
15984 KB |
Output is correct |
83 |
Correct |
112 ms |
15920 KB |
Output is correct |
84 |
Correct |
124 ms |
15956 KB |
Output is correct |
85 |
Correct |
105 ms |
15820 KB |
Output is correct |
86 |
Correct |
105 ms |
15948 KB |
Output is correct |
87 |
Correct |
114 ms |
15940 KB |
Output is correct |
88 |
Correct |
116 ms |
15728 KB |
Output is correct |
89 |
Correct |
158 ms |
15724 KB |
Output is correct |
90 |
Correct |
121 ms |
15656 KB |
Output is correct |
91 |
Correct |
127 ms |
15724 KB |
Output is correct |
92 |
Correct |
151 ms |
15720 KB |
Output is correct |