#include<bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int dx[] = {1, 0, 0, -1};
const int dy[] = {0, 1, -1, 0};
const int MAXN = 8e2 + 10;
const int inf = 1e9 + 7;
int n, s;
int xe, ye;
int grid[MAXN][MAXN];
int loangbee[MAXN][MAXN];
int loangpl[MAXN][MAXN];
bool exceed(int x, int y) {
return (x > n || x < 1 || y > n || y < 1);
}
void bfsbee() {
queue<pair<int, int>> q;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (grid[i][j] == 4) {
q.push({i, j});
loangbee[i][j] = 0;
}
}
}
while (!q.empty()) {
pii temp = q.front();
q.pop();
int x = temp.fi;
int y = temp.se;
for (int i = 0; i < 4; i++) {
int u = x + dx[i];
int v = y + dy[i];
if (!exceed(u, v)) {
if (loangbee[u][v] == inf && grid[u][v] == 1) {
loangbee[u][v] = loangbee[x][y] + 1;
q.push({u, v});
}
}
}
}
}
int bfspl(int original_value) {
queue<pair<int, int>> q;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (grid[i][j] == 2) {
q.push({i, j});
loangpl[i][j] = 0;
}
else {
loangpl[i][j] = inf;
}
}
}
while (!q.empty()) {
pii temp = q.front();
q.pop();
int x = temp.fi;
int y = temp.se;
for (int i = 0; i < 4; i++) {
int u = x + dx[i];
int v = y + dy[i];
if (!exceed(u, v)) {
if (loangpl[u][v] == inf && grid[u][v] == 1) {
if ((loangpl[x][y] + 1) / s < loangbee[u][v] - original_value) {
q.push({u, v});
loangpl[u][v] = loangpl[x][y] + 1;
};
}
}
}
}
for (int i = 0; i < 4; i++) {
int u = xe + dx[i];
int v = ye + dy[i];
if (exceed(u, v)) continue;
if (loangpl[u][v] != inf) return 1;
}
return 0;
}
int main() {
cin >> n >> s;
for (int i = 1; i <= n; i++) {
string str;
cin >> str;
for (int j = 1; j <= n; j++) {
char c = str[j-1];
loangpl[i][j] = inf;
loangbee[i][j] = inf;
if (c == 'T') {
grid[i][j] = 0;
}
if (c == 'G') grid[i][j] = 1;
if (c == 'M') grid[i][j] = 2;
if (c == 'D') {
grid[i][j] = 1;
xe = i;
ye = j;
}
if (c == 'H') grid[i][j] = 4;
}
}
bfsbee();
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) {
// cout << loangbee[i][j] << " ";
// }
// cout << endl;
// }
// cout << "map nguoi" << endl;
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) {
// cout << loangpl[i][j] << " ";
// }
// cout << endl;
//
int l = 0, r = n * n;
while (l <= r) {
int mid = (l + r) / 2;
if (bfspl(mid) == 1) {
l = mid + 1;
}
else r = mid - 1;
}
cout << l - 1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
66 ms |
8044 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
0 ms |
860 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
908 KB |
Output isn't correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
0 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
856 KB |
Output is correct |
26 |
Correct |
1 ms |
872 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
1 ms |
1116 KB |
Output is correct |
32 |
Correct |
1 ms |
1116 KB |
Output is correct |
33 |
Correct |
7 ms |
3676 KB |
Output is correct |
34 |
Correct |
7 ms |
3676 KB |
Output is correct |
35 |
Correct |
17 ms |
3672 KB |
Output is correct |
36 |
Correct |
8 ms |
4188 KB |
Output is correct |
37 |
Correct |
8 ms |
4256 KB |
Output is correct |
38 |
Correct |
21 ms |
4188 KB |
Output is correct |
39 |
Correct |
9 ms |
4604 KB |
Output is correct |
40 |
Correct |
10 ms |
4732 KB |
Output is correct |
41 |
Correct |
29 ms |
4696 KB |
Output is correct |
42 |
Correct |
12 ms |
5212 KB |
Output is correct |
43 |
Correct |
11 ms |
5212 KB |
Output is correct |
44 |
Correct |
41 ms |
5156 KB |
Output is correct |
45 |
Correct |
14 ms |
5468 KB |
Output is correct |
46 |
Correct |
17 ms |
5680 KB |
Output is correct |
47 |
Correct |
38 ms |
5464 KB |
Output is correct |
48 |
Correct |
16 ms |
5980 KB |
Output is correct |
49 |
Correct |
17 ms |
6164 KB |
Output is correct |
50 |
Correct |
45 ms |
6144 KB |
Output is correct |
51 |
Correct |
18 ms |
6488 KB |
Output is correct |
52 |
Correct |
19 ms |
6492 KB |
Output is correct |
53 |
Correct |
57 ms |
6492 KB |
Output is correct |
54 |
Correct |
24 ms |
7000 KB |
Output is correct |
55 |
Correct |
22 ms |
7004 KB |
Output is correct |
56 |
Correct |
83 ms |
7100 KB |
Output is correct |
57 |
Correct |
24 ms |
7516 KB |
Output is correct |
58 |
Correct |
31 ms |
7516 KB |
Output is correct |
59 |
Correct |
75 ms |
7576 KB |
Output is correct |
60 |
Correct |
27 ms |
8024 KB |
Output is correct |
61 |
Correct |
28 ms |
8028 KB |
Output is correct |
62 |
Correct |
93 ms |
8028 KB |
Output is correct |
63 |
Correct |
83 ms |
8032 KB |
Output is correct |
64 |
Correct |
148 ms |
8040 KB |
Output is correct |
65 |
Correct |
114 ms |
8060 KB |
Output is correct |
66 |
Correct |
97 ms |
8028 KB |
Output is correct |
67 |
Correct |
91 ms |
8024 KB |
Output is correct |
68 |
Correct |
47 ms |
8028 KB |
Output is correct |
69 |
Correct |
48 ms |
8024 KB |
Output is correct |
70 |
Correct |
39 ms |
8028 KB |
Output is correct |
71 |
Correct |
47 ms |
7976 KB |
Output is correct |
72 |
Incorrect |
41 ms |
8024 KB |
Output isn't correct |
73 |
Incorrect |
56 ms |
8280 KB |
Output isn't correct |
74 |
Correct |
47 ms |
8280 KB |
Output is correct |
75 |
Correct |
55 ms |
8280 KB |
Output is correct |
76 |
Correct |
50 ms |
8324 KB |
Output is correct |
77 |
Correct |
61 ms |
8324 KB |
Output is correct |
78 |
Correct |
59 ms |
8284 KB |
Output is correct |
79 |
Correct |
52 ms |
8284 KB |
Output is correct |
80 |
Correct |
59 ms |
8280 KB |
Output is correct |
81 |
Correct |
54 ms |
8284 KB |
Output is correct |
82 |
Correct |
49 ms |
8280 KB |
Output is correct |
83 |
Correct |
58 ms |
8024 KB |
Output is correct |
84 |
Correct |
53 ms |
8248 KB |
Output is correct |
85 |
Correct |
60 ms |
8028 KB |
Output is correct |
86 |
Correct |
61 ms |
8024 KB |
Output is correct |
87 |
Correct |
57 ms |
8092 KB |
Output is correct |
88 |
Correct |
59 ms |
8028 KB |
Output is correct |
89 |
Correct |
62 ms |
8200 KB |
Output is correct |
90 |
Correct |
79 ms |
8200 KB |
Output is correct |
91 |
Correct |
73 ms |
8028 KB |
Output is correct |
92 |
Correct |
64 ms |
8200 KB |
Output is correct |