/*input
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
TGHHGGT
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 100101;
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;
template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
template<typename T>
void print(vector<T> vec, string name = "") {
cout << name;
for (auto u : vec)
cout << u << ' ';
cout << '\n';
}
using vc = vector<char>;
int N, S;
vector<vc> sk;
vii hiveDis;
int dx[] = {0, 1, 0, -1};
int dy[] = { -1, 0, 1, 0};
pi dxy[] = {
{dx[0], dy[0]},
{dx[1], dy[1]},
{dx[2], dy[2]},
{dx[3], dy[3]}
};
pi mecho;
pi home;
vpi hives;
bool valid(pi a) {
return a.x >= 0 and a.y >= 0 and a.x < N and a.y < N;
}
bool gali(int time) {
vii dis(N, vi(N, MOD));
dis[mecho.x][mecho.y] = 0;
queue<pi> q;
q.push(mecho);
while (q.size()) {
pi c = q.front(); q.pop();
for (int i = 0; i < 4; ++i)
{
pi n = c + dxy[i];
if (valid(n) and sk[n.x][n.y] != 'T') {
if (sk[n.x][n.y] == 'D')
return true;
int newDis = dis[c.x][c.y] + 1;
if (time + newDis / S < hiveDis[n.x][n.y] and newDis < dis[n.x][n.y]) {
dis[n.x][n.y] = newDis;
q.push(n);
}
}
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N >> S;
sk = vector<vc>(N, vc(N));
hiveDis = vii(N, vi(N, MOD));
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
cin >> sk[i][j];
if (sk[i][j] == 'M') mecho = {i, j};
else if (sk[i][j] == 'D') home = {i, j};
else if (sk[i][j] == 'H') hives.emplace_back(i, j);
}
}
queue<pi> q;
for (auto u : hives) {
hiveDis[u.x][u.y] = 0;
q.push(u);
}
while (q.size()) {
pi c = q.front(); q.pop();
for (int i = 0; i < 4; ++i)
{
pi nc = c + dxy[i];
if (valid(nc) and sk[nc.x][nc.y] != 'T' and nc != home) {
if (hiveDis[c.x][c.y] + 1 < hiveDis[nc.x][nc.y]) {
hiveDis[nc.x][nc.y] = hiveDis[c.x][c.y] + 1;
q.push(nc);
}
}
}
}
int l = 0;
int h = 5 * N;
int m;
while (l < h) {
m = (l + h + 1) / 2;
if (gali(m))
l = m;
else
h = m - 1;
}
printf("%d\n", l);
}
/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
119 ms |
6808 KB |
Output is correct |
8 |
Incorrect |
6 ms |
384 KB |
Output isn't correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
13 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
14 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
15 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
18 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
19 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
20 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
21 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
22 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
23 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
24 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
25 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
26 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
27 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
28 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
29 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
30 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
31 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
32 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
33 |
Incorrect |
10 ms |
1616 KB |
Output isn't correct |
34 |
Incorrect |
10 ms |
1608 KB |
Output isn't correct |
35 |
Correct |
30 ms |
1616 KB |
Output is correct |
36 |
Incorrect |
11 ms |
1920 KB |
Output isn't correct |
37 |
Incorrect |
11 ms |
2048 KB |
Output isn't correct |
38 |
Correct |
39 ms |
2048 KB |
Output is correct |
39 |
Incorrect |
13 ms |
2456 KB |
Output isn't correct |
40 |
Incorrect |
15 ms |
2416 KB |
Output isn't correct |
41 |
Correct |
45 ms |
2460 KB |
Output is correct |
42 |
Incorrect |
16 ms |
2928 KB |
Output isn't correct |
43 |
Incorrect |
16 ms |
2928 KB |
Output isn't correct |
44 |
Correct |
56 ms |
2920 KB |
Output is correct |
45 |
Incorrect |
22 ms |
3408 KB |
Output isn't correct |
46 |
Incorrect |
18 ms |
3408 KB |
Output isn't correct |
47 |
Correct |
66 ms |
3428 KB |
Output is correct |
48 |
Incorrect |
21 ms |
3984 KB |
Output isn't correct |
49 |
Incorrect |
21 ms |
4000 KB |
Output isn't correct |
50 |
Correct |
81 ms |
4008 KB |
Output is correct |
51 |
Incorrect |
23 ms |
4656 KB |
Output isn't correct |
52 |
Incorrect |
31 ms |
4868 KB |
Output isn't correct |
53 |
Correct |
99 ms |
4644 KB |
Output is correct |
54 |
Incorrect |
26 ms |
5312 KB |
Output isn't correct |
55 |
Incorrect |
27 ms |
5328 KB |
Output isn't correct |
56 |
Correct |
123 ms |
5288 KB |
Output is correct |
57 |
Incorrect |
31 ms |
6000 KB |
Output isn't correct |
58 |
Incorrect |
32 ms |
6036 KB |
Output isn't correct |
59 |
Correct |
134 ms |
5984 KB |
Output is correct |
60 |
Incorrect |
34 ms |
6780 KB |
Output isn't correct |
61 |
Incorrect |
35 ms |
6800 KB |
Output isn't correct |
62 |
Correct |
160 ms |
6756 KB |
Output is correct |
63 |
Correct |
114 ms |
6804 KB |
Output is correct |
64 |
Correct |
208 ms |
6756 KB |
Output is correct |
65 |
Correct |
220 ms |
6768 KB |
Output is correct |
66 |
Correct |
169 ms |
6800 KB |
Output is correct |
67 |
Incorrect |
130 ms |
6784 KB |
Output isn't correct |
68 |
Correct |
66 ms |
6880 KB |
Output is correct |
69 |
Correct |
68 ms |
6788 KB |
Output is correct |
70 |
Correct |
70 ms |
6960 KB |
Output is correct |
71 |
Correct |
50 ms |
6788 KB |
Output is correct |
72 |
Incorrect |
48 ms |
6788 KB |
Output isn't correct |
73 |
Incorrect |
61 ms |
6856 KB |
Output isn't correct |
74 |
Correct |
89 ms |
6824 KB |
Output is correct |
75 |
Correct |
94 ms |
6808 KB |
Output is correct |
76 |
Correct |
100 ms |
6800 KB |
Output is correct |
77 |
Correct |
91 ms |
6800 KB |
Output is correct |
78 |
Incorrect |
98 ms |
6776 KB |
Output isn't correct |
79 |
Correct |
75 ms |
6808 KB |
Output is correct |
80 |
Correct |
89 ms |
6776 KB |
Output is correct |
81 |
Correct |
113 ms |
6808 KB |
Output is correct |
82 |
Correct |
99 ms |
6792 KB |
Output is correct |
83 |
Correct |
156 ms |
6800 KB |
Output is correct |
84 |
Correct |
111 ms |
6792 KB |
Output is correct |
85 |
Correct |
142 ms |
7000 KB |
Output is correct |
86 |
Correct |
121 ms |
6800 KB |
Output is correct |
87 |
Correct |
122 ms |
6792 KB |
Output is correct |
88 |
Correct |
122 ms |
6796 KB |
Output is correct |
89 |
Correct |
152 ms |
6976 KB |
Output is correct |
90 |
Correct |
148 ms |
6828 KB |
Output is correct |
91 |
Correct |
143 ms |
6772 KB |
Output is correct |
92 |
Correct |
128 ms |
6824 KB |
Output is correct |