/*
ID: c4ts0up
LANG: C++
TASK: Mecho
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define ff first
#define ss second
const int NMAX = 805;
const int INF = 1e9;
int n, s;
pair <int,int> home, mecho;
char grid[NMAX][NMAX];
int dist[NMAX][NMAX];
// hace el check a una coordenada
bool Check(int row, int col) {
return (0 <= row && row < n && 0 <= col && col < n);
}
// BFS para encontrar el tiempo requerido para que las abejas lleguen a un lugar
void EncontrarDistancias() {
queue <pair <int,int> > to;
set <pair <int,int> > seen;
for (int i=0; i<n; i++) for (int j=0; j<n; j++) {
if (grid[i][j] == 'H') to.push({i,j}), dist[i][j] = 0;
else dist[i][j] = INF;
}
while (!to.empty()) {
pair <int,int> curr = to.front();
to.pop();
// ya lo visitamos
if (seen.count(curr)) continue;
if (grid[curr.ff][curr.ss] == 'T' || grid[curr.ff][curr.ss] == 'D') {
seen.insert(curr);
continue;
}
// visitamos a todos los vecinos
if (Check(curr.ff+1, curr.ss) && dist[curr.ff+1][curr.ss] > dist[curr.ff][curr.ss]+1) to.push({curr.ff+1, curr.ss}), dist[curr.ff+1][curr.ss] = dist[curr.ff][curr.ss]+1;
if (Check(curr.ff-1, curr.ss) && dist[curr.ff-1][curr.ss] > dist[curr.ff][curr.ss]+1) to.push({curr.ff-1, curr.ss}), dist[curr.ff-1][curr.ss] = dist[curr.ff][curr.ss]+1;
if (Check(curr.ff, curr.ss+1) && dist[curr.ff][curr.ss+1] > dist[curr.ff][curr.ss]+1) to.push({curr.ff, curr.ss+1}), dist[curr.ff][curr.ss+1] = dist[curr.ff][curr.ss]+1;
if (Check(curr.ff, curr.ss-1) && dist[curr.ff][curr.ss-1] > dist[curr.ff][curr.ss]+1) to.push({curr.ff, curr.ss-1}), dist[curr.ff][curr.ss-1] = dist[curr.ff][curr.ss]+1;
seen.insert(curr);
}
}
bool BFS(int minutos) {
bool seen[NMAX][NMAX];
queue <pair <int, pair <int,int> > > to;
to.push({0, mecho});
for (int i=0; i<=n; i++) for (int j=0; j<=n; j++) seen[i][j] = false;
while (!to.empty()) {
pair <int, pair <int,int> > curr = to.front();
to.pop();
int r = curr.ss.ff;
int c = curr.ss.ss;
int pasos = curr.ff;
// ya lo visitamos, no tenemos que volver
if (seen[r][c]) continue;
// no podemos atravesarlo
if (grid[r][c] == 'T') {
seen[r][c] = true;
continue;
}
else if (grid[r][c] == 'D') return true;
// ya está infestado de abejas, o sea, bloqueado
if (minutos + (curr.ff/s) >= dist[r][c]) {
seen[r][c] = true;
continue;
}
// visitamos a todos los vecinos
if (!seen[r+1][c] && Check(r+1, c)) to.push({pasos, {r+1,c}});
if (!seen[r-1][c] && Check(r-1, c)) to.push({pasos, {r-1,c}});
if (!seen[r][c+1] && Check(r, c+1)) to.push({pasos, {r,c+1}});
if (!seen[r][c-1] && Check(r, c-1)) to.push({pasos, {r,c-1}});
seen[r][c] = true;
}
return false;
}
int main() {
/*//
freopen("", "r", stdin);
freopen("", "w", stdout);
//*/
cin >> n >> s;
for (int i=0; i<n; i++) {
string aux;
cin >> aux;
for (int j=0; j<n; j++) {
grid[i][j] = aux[j];
if (grid[i][j] == 'M') mecho = {i,j};
else if (grid[i][j] == 'D') home = {i,j};
}
}
EncontrarDistancias();
if (!BFS(0)) {
cout << -1 << endl;
exit(0);
}
int lb = 0, ub = 1e6, mid;
while (lb <= ub) {
mid = (lb+ub)/2;
if (BFS(mid)) lb = mid+1;
else ub = mid-1;
}
//cerr << "lb = " << lb << "; ub = " << ub << "; mid = " << mid << endl;
cout << ub-1 << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
7 |
Incorrect |
483 ms |
34168 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
11 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
12 |
Incorrect |
2 ms |
768 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
640 KB |
Output isn't correct |
14 |
Incorrect |
3 ms |
768 KB |
Output isn't correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
640 KB |
Output is correct |
23 |
Correct |
1 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
25 |
Correct |
2 ms |
768 KB |
Output is correct |
26 |
Correct |
2 ms |
768 KB |
Output is correct |
27 |
Correct |
2 ms |
768 KB |
Output is correct |
28 |
Correct |
2 ms |
768 KB |
Output is correct |
29 |
Correct |
2 ms |
896 KB |
Output is correct |
30 |
Correct |
2 ms |
896 KB |
Output is correct |
31 |
Correct |
2 ms |
896 KB |
Output is correct |
32 |
Correct |
3 ms |
896 KB |
Output is correct |
33 |
Correct |
43 ms |
7672 KB |
Output is correct |
34 |
Correct |
49 ms |
7672 KB |
Output is correct |
35 |
Incorrect |
95 ms |
7672 KB |
Output isn't correct |
36 |
Correct |
61 ms |
9720 KB |
Output is correct |
37 |
Correct |
69 ms |
9724 KB |
Output is correct |
38 |
Incorrect |
123 ms |
9720 KB |
Output isn't correct |
39 |
Correct |
72 ms |
11896 KB |
Output is correct |
40 |
Correct |
84 ms |
11960 KB |
Output is correct |
41 |
Incorrect |
157 ms |
11896 KB |
Output isn't correct |
42 |
Correct |
90 ms |
14456 KB |
Output is correct |
43 |
Correct |
106 ms |
14512 KB |
Output is correct |
44 |
Incorrect |
210 ms |
14456 KB |
Output isn't correct |
45 |
Correct |
106 ms |
17144 KB |
Output is correct |
46 |
Correct |
130 ms |
17144 KB |
Output is correct |
47 |
Incorrect |
265 ms |
17252 KB |
Output isn't correct |
48 |
Correct |
131 ms |
20088 KB |
Output is correct |
49 |
Correct |
147 ms |
20088 KB |
Output is correct |
50 |
Incorrect |
324 ms |
20072 KB |
Output isn't correct |
51 |
Correct |
151 ms |
23160 KB |
Output is correct |
52 |
Correct |
174 ms |
23160 KB |
Output is correct |
53 |
Incorrect |
391 ms |
23256 KB |
Output isn't correct |
54 |
Correct |
175 ms |
26616 KB |
Output is correct |
55 |
Correct |
202 ms |
26616 KB |
Output is correct |
56 |
Incorrect |
437 ms |
26748 KB |
Output isn't correct |
57 |
Correct |
216 ms |
30328 KB |
Output is correct |
58 |
Correct |
248 ms |
30200 KB |
Output is correct |
59 |
Incorrect |
529 ms |
30328 KB |
Output isn't correct |
60 |
Correct |
274 ms |
34176 KB |
Output is correct |
61 |
Correct |
271 ms |
34020 KB |
Output is correct |
62 |
Incorrect |
588 ms |
34424 KB |
Output isn't correct |
63 |
Incorrect |
612 ms |
33816 KB |
Output isn't correct |
64 |
Correct |
614 ms |
33784 KB |
Output is correct |
65 |
Incorrect |
614 ms |
33912 KB |
Output isn't correct |
66 |
Incorrect |
590 ms |
33912 KB |
Output isn't correct |
67 |
Incorrect |
579 ms |
33896 KB |
Output isn't correct |
68 |
Incorrect |
543 ms |
33884 KB |
Output isn't correct |
69 |
Incorrect |
545 ms |
33912 KB |
Output isn't correct |
70 |
Incorrect |
654 ms |
33916 KB |
Output isn't correct |
71 |
Incorrect |
626 ms |
33784 KB |
Output isn't correct |
72 |
Incorrect |
557 ms |
33916 KB |
Output isn't correct |
73 |
Incorrect |
469 ms |
34224 KB |
Output isn't correct |
74 |
Correct |
496 ms |
34224 KB |
Output is correct |
75 |
Incorrect |
489 ms |
34168 KB |
Output isn't correct |
76 |
Incorrect |
532 ms |
34296 KB |
Output isn't correct |
77 |
Incorrect |
491 ms |
34168 KB |
Output isn't correct |
78 |
Incorrect |
452 ms |
34296 KB |
Output isn't correct |
79 |
Correct |
415 ms |
34168 KB |
Output is correct |
80 |
Incorrect |
415 ms |
34328 KB |
Output isn't correct |
81 |
Incorrect |
455 ms |
34296 KB |
Output isn't correct |
82 |
Incorrect |
422 ms |
34168 KB |
Output isn't correct |
83 |
Incorrect |
513 ms |
34296 KB |
Output isn't correct |
84 |
Correct |
449 ms |
34168 KB |
Output is correct |
85 |
Incorrect |
444 ms |
34168 KB |
Output isn't correct |
86 |
Incorrect |
448 ms |
34168 KB |
Output isn't correct |
87 |
Incorrect |
452 ms |
34168 KB |
Output isn't correct |
88 |
Incorrect |
494 ms |
34208 KB |
Output isn't correct |
89 |
Correct |
463 ms |
34296 KB |
Output is correct |
90 |
Incorrect |
461 ms |
34272 KB |
Output isn't correct |
91 |
Incorrect |
458 ms |
34296 KB |
Output isn't correct |
92 |
Incorrect |
475 ms |
34168 KB |
Output isn't correct |