#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
// -------------------------------------------------- Main Code --------------------------------------------------
const int N = 800;
int n, s;
int arr[N][N], beesDis[N][N], bearDis[N][N];
queue<pii> bees;
pii st, en;
int di[4] = {0, 0, 1, -1}, dj[4] = {1, -1, 0, 0};
bool isVal(int i, int j) {
return (i >= 0 && j >= 0 && i < n && j < n && arr[i][j]);
}
void fillBees() {
while (!bees.empty()) {
auto f = bees.front(); bees.pop();
for (int i = 0; i < 4; i++) {
int ni = f.f+di[i], nj = f.s+dj[i];
if (isVal(ni, nj) && beesDis[ni][nj] == -1) {
beesDis[ni][nj] = beesDis[f.f][f.s]+1;
bees.push({ni, nj});
}
}
}
}
bool isPoss(int i, int j, int dis, int initTime) {
return ((dis/s)+initTime < beesDis[i][j]);
}
bool isFiss(int initTime) {
if (beesDis[st.f][st.s] <= initTime) return false;
memset(bearDis, -1, sizeof bearDis);
queue<pii> bear; bear.push({st.f, st.s});
bearDis[st.f][st.s] = 0;
while (!bear.empty()) {
auto f = bear.front(); bear.pop();
for (int i = 0; i < 4; i++) {
int ni = f.f+di[i], nj = f.s+dj[i];
if (isVal(ni, nj) && bearDis[ni][nj] == -1 && isPoss(ni, nj, bearDis[f.f][f.s]+1, initTime)) {
bearDis[ni][nj] = bearDis[f.f][f.s]+1;
bear.push({ni, nj});
}
}
}
for (int i = 0; i < 4; i++) {
int ni = en.f+di[i], nj = en.s+dj[i];
if (isVal(ni, nj) && bearDis[ni][nj] != -1) return true;
}
return false;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> s;
memset(beesDis, -1, sizeof beesDis);
memset(arr, 0, sizeof arr);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
char x; cin >> x;
if (x == 'M') st = {i, j};
if (x == 'D') en = {i, j};
if (x == 'H') {
bees.push({i, j});
beesDis[i][j] = 0;
}
if (x == 'G' || x == 'M') arr[i][j] = 1;
}
}
fillBees();
int i = 0, j = n*n, ans = inf;
while (i <= j) {
int mid = (i+j)/2;
if (isFiss(mid)) ans = mid, i = mid+1;
else j = mid-1;
}
cout << (ans==inf?-1:ans) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7764 KB |
Output is correct |
2 |
Correct |
4 ms |
7764 KB |
Output is correct |
3 |
Correct |
3 ms |
7764 KB |
Output is correct |
4 |
Correct |
4 ms |
7764 KB |
Output is correct |
5 |
Correct |
3 ms |
7764 KB |
Output is correct |
6 |
Correct |
4 ms |
7764 KB |
Output is correct |
7 |
Correct |
74 ms |
7892 KB |
Output is correct |
8 |
Correct |
3 ms |
7764 KB |
Output is correct |
9 |
Correct |
4 ms |
7728 KB |
Output is correct |
10 |
Correct |
4 ms |
7764 KB |
Output is correct |
11 |
Correct |
3 ms |
7764 KB |
Output is correct |
12 |
Correct |
5 ms |
7764 KB |
Output is correct |
13 |
Correct |
5 ms |
7764 KB |
Output is correct |
14 |
Correct |
4 ms |
7764 KB |
Output is correct |
15 |
Correct |
4 ms |
7764 KB |
Output is correct |
16 |
Correct |
4 ms |
7764 KB |
Output is correct |
17 |
Correct |
3 ms |
7764 KB |
Output is correct |
18 |
Correct |
4 ms |
7764 KB |
Output is correct |
19 |
Correct |
4 ms |
7764 KB |
Output is correct |
20 |
Correct |
3 ms |
7764 KB |
Output is correct |
21 |
Correct |
4 ms |
7764 KB |
Output is correct |
22 |
Correct |
4 ms |
7764 KB |
Output is correct |
23 |
Correct |
4 ms |
7764 KB |
Output is correct |
24 |
Correct |
4 ms |
7764 KB |
Output is correct |
25 |
Correct |
4 ms |
7764 KB |
Output is correct |
26 |
Correct |
4 ms |
7764 KB |
Output is correct |
27 |
Correct |
4 ms |
7764 KB |
Output is correct |
28 |
Correct |
4 ms |
7764 KB |
Output is correct |
29 |
Correct |
4 ms |
7764 KB |
Output is correct |
30 |
Correct |
4 ms |
7764 KB |
Output is correct |
31 |
Correct |
4 ms |
7764 KB |
Output is correct |
32 |
Correct |
5 ms |
7764 KB |
Output is correct |
33 |
Correct |
8 ms |
7764 KB |
Output is correct |
34 |
Correct |
8 ms |
7764 KB |
Output is correct |
35 |
Correct |
23 ms |
7840 KB |
Output is correct |
36 |
Correct |
8 ms |
7764 KB |
Output is correct |
37 |
Correct |
9 ms |
7768 KB |
Output is correct |
38 |
Correct |
27 ms |
7804 KB |
Output is correct |
39 |
Correct |
10 ms |
7828 KB |
Output is correct |
40 |
Correct |
11 ms |
7764 KB |
Output is correct |
41 |
Correct |
38 ms |
7776 KB |
Output is correct |
42 |
Correct |
11 ms |
7764 KB |
Output is correct |
43 |
Correct |
12 ms |
7768 KB |
Output is correct |
44 |
Correct |
49 ms |
7828 KB |
Output is correct |
45 |
Correct |
14 ms |
7764 KB |
Output is correct |
46 |
Correct |
13 ms |
7764 KB |
Output is correct |
47 |
Correct |
54 ms |
7820 KB |
Output is correct |
48 |
Correct |
14 ms |
7768 KB |
Output is correct |
49 |
Correct |
14 ms |
7764 KB |
Output is correct |
50 |
Correct |
59 ms |
7820 KB |
Output is correct |
51 |
Correct |
15 ms |
7764 KB |
Output is correct |
52 |
Correct |
22 ms |
7764 KB |
Output is correct |
53 |
Correct |
81 ms |
7764 KB |
Output is correct |
54 |
Correct |
22 ms |
7832 KB |
Output is correct |
55 |
Correct |
20 ms |
7764 KB |
Output is correct |
56 |
Correct |
98 ms |
7820 KB |
Output is correct |
57 |
Correct |
19 ms |
7840 KB |
Output is correct |
58 |
Correct |
22 ms |
7764 KB |
Output is correct |
59 |
Correct |
114 ms |
7824 KB |
Output is correct |
60 |
Correct |
22 ms |
7764 KB |
Output is correct |
61 |
Correct |
24 ms |
7812 KB |
Output is correct |
62 |
Correct |
120 ms |
7884 KB |
Output is correct |
63 |
Correct |
107 ms |
7748 KB |
Output is correct |
64 |
Correct |
193 ms |
7752 KB |
Output is correct |
65 |
Correct |
155 ms |
7752 KB |
Output is correct |
66 |
Correct |
116 ms |
7764 KB |
Output is correct |
67 |
Correct |
113 ms |
7824 KB |
Output is correct |
68 |
Correct |
54 ms |
7776 KB |
Output is correct |
69 |
Correct |
61 ms |
7848 KB |
Output is correct |
70 |
Correct |
38 ms |
7756 KB |
Output is correct |
71 |
Correct |
43 ms |
7768 KB |
Output is correct |
72 |
Correct |
33 ms |
7784 KB |
Output is correct |
73 |
Correct |
34 ms |
8044 KB |
Output is correct |
74 |
Correct |
54 ms |
8032 KB |
Output is correct |
75 |
Correct |
75 ms |
8140 KB |
Output is correct |
76 |
Correct |
57 ms |
8104 KB |
Output is correct |
77 |
Correct |
57 ms |
8036 KB |
Output is correct |
78 |
Correct |
67 ms |
8024 KB |
Output is correct |
79 |
Correct |
46 ms |
8020 KB |
Output is correct |
80 |
Correct |
62 ms |
8016 KB |
Output is correct |
81 |
Correct |
73 ms |
8028 KB |
Output is correct |
82 |
Correct |
54 ms |
8032 KB |
Output is correct |
83 |
Correct |
69 ms |
8012 KB |
Output is correct |
84 |
Correct |
63 ms |
8032 KB |
Output is correct |
85 |
Correct |
89 ms |
8096 KB |
Output is correct |
86 |
Correct |
67 ms |
8032 KB |
Output is correct |
87 |
Correct |
72 ms |
8028 KB |
Output is correct |
88 |
Correct |
73 ms |
7980 KB |
Output is correct |
89 |
Correct |
79 ms |
7892 KB |
Output is correct |
90 |
Correct |
77 ms |
7888 KB |
Output is correct |
91 |
Correct |
74 ms |
7980 KB |
Output is correct |
92 |
Correct |
76 ms |
7952 KB |
Output is correct |