/*
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') {
seen.insert(curr);
continue;
}
// visitamos a todos los vecinos
if (!seen.count({curr.ff+1, curr.ss}) && Check(curr.ff+1, curr.ss)) to.push({curr.ff+1, curr.ss}), dist[curr.ff+1][curr.ss] = min(dist[curr.ff+1][curr.ss], dist[curr.ff][curr.ss]+1);
if (!seen.count({curr.ff-1, curr.ss}) && Check(curr.ff-1, curr.ss)) to.push({curr.ff-1, curr.ss}), dist[curr.ff-1][curr.ss] = min(dist[curr.ff-1][curr.ss], dist[curr.ff][curr.ss]+1);
if (!seen.count({curr.ff, curr.ss+1}) && Check(curr.ff, curr.ss+1)) to.push({curr.ff, curr.ss+1}), dist[curr.ff][curr.ss+1] = min(dist[curr.ff][curr.ss+1], dist[curr.ff][curr.ss]+1);
if (!seen.count({curr.ff, curr.ss-1}) && Check(curr.ff, curr.ss-1)) to.push({curr.ff, curr.ss-1}), dist[curr.ff][curr.ss-1] = min(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 |
1 ms |
384 KB |
Output is correct |
4 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
7 |
Incorrect |
848 ms |
34808 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
11 |
Incorrect |
1 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 |
2 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 |
2 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
25 |
Correct |
3 ms |
768 KB |
Output is correct |
26 |
Correct |
2 ms |
768 KB |
Output is correct |
27 |
Correct |
4 ms |
896 KB |
Output is correct |
28 |
Correct |
4 ms |
824 KB |
Output is correct |
29 |
Correct |
3 ms |
896 KB |
Output is correct |
30 |
Correct |
3 ms |
896 KB |
Output is correct |
31 |
Correct |
3 ms |
896 KB |
Output is correct |
32 |
Correct |
4 ms |
896 KB |
Output is correct |
33 |
Correct |
65 ms |
7800 KB |
Output is correct |
34 |
Correct |
68 ms |
7800 KB |
Output is correct |
35 |
Incorrect |
136 ms |
7800 KB |
Output isn't correct |
36 |
Correct |
96 ms |
9848 KB |
Output is correct |
37 |
Correct |
98 ms |
9848 KB |
Output is correct |
38 |
Incorrect |
182 ms |
9848 KB |
Output isn't correct |
39 |
Correct |
102 ms |
12152 KB |
Output is correct |
40 |
Correct |
125 ms |
12152 KB |
Output is correct |
41 |
Incorrect |
228 ms |
12320 KB |
Output isn't correct |
42 |
Correct |
136 ms |
14712 KB |
Output is correct |
43 |
Correct |
147 ms |
14656 KB |
Output is correct |
44 |
Incorrect |
293 ms |
14676 KB |
Output isn't correct |
45 |
Correct |
157 ms |
17400 KB |
Output is correct |
46 |
Correct |
178 ms |
17400 KB |
Output is correct |
47 |
Incorrect |
358 ms |
17400 KB |
Output isn't correct |
48 |
Correct |
198 ms |
20472 KB |
Output is correct |
49 |
Correct |
213 ms |
20344 KB |
Output is correct |
50 |
Incorrect |
430 ms |
20472 KB |
Output isn't correct |
51 |
Correct |
228 ms |
23672 KB |
Output is correct |
52 |
Correct |
263 ms |
23708 KB |
Output is correct |
53 |
Incorrect |
543 ms |
23704 KB |
Output isn't correct |
54 |
Correct |
261 ms |
27128 KB |
Output is correct |
55 |
Correct |
292 ms |
27256 KB |
Output is correct |
56 |
Incorrect |
617 ms |
27256 KB |
Output isn't correct |
57 |
Correct |
315 ms |
30840 KB |
Output is correct |
58 |
Correct |
338 ms |
30968 KB |
Output is correct |
59 |
Incorrect |
735 ms |
30840 KB |
Output isn't correct |
60 |
Correct |
398 ms |
34864 KB |
Output is correct |
61 |
Correct |
399 ms |
34808 KB |
Output is correct |
62 |
Incorrect |
823 ms |
34936 KB |
Output isn't correct |
63 |
Incorrect |
838 ms |
34540 KB |
Output isn't correct |
64 |
Correct |
846 ms |
34424 KB |
Output is correct |
65 |
Incorrect |
844 ms |
34428 KB |
Output isn't correct |
66 |
Incorrect |
836 ms |
34424 KB |
Output isn't correct |
67 |
Incorrect |
802 ms |
34552 KB |
Output isn't correct |
68 |
Incorrect |
873 ms |
34552 KB |
Output isn't correct |
69 |
Incorrect |
807 ms |
34552 KB |
Output isn't correct |
70 |
Incorrect |
796 ms |
34552 KB |
Output isn't correct |
71 |
Incorrect |
778 ms |
34536 KB |
Output isn't correct |
72 |
Incorrect |
727 ms |
34552 KB |
Output isn't correct |
73 |
Incorrect |
822 ms |
34792 KB |
Output isn't correct |
74 |
Correct |
859 ms |
34812 KB |
Output is correct |
75 |
Incorrect |
872 ms |
34816 KB |
Output isn't correct |
76 |
Incorrect |
840 ms |
34936 KB |
Output isn't correct |
77 |
Incorrect |
854 ms |
34772 KB |
Output isn't correct |
78 |
Incorrect |
770 ms |
34808 KB |
Output isn't correct |
79 |
Correct |
760 ms |
34808 KB |
Output is correct |
80 |
Incorrect |
780 ms |
34896 KB |
Output isn't correct |
81 |
Incorrect |
774 ms |
34864 KB |
Output isn't correct |
82 |
Incorrect |
740 ms |
34808 KB |
Output isn't correct |
83 |
Incorrect |
763 ms |
34808 KB |
Output isn't correct |
84 |
Correct |
791 ms |
34808 KB |
Output is correct |
85 |
Incorrect |
780 ms |
34808 KB |
Output isn't correct |
86 |
Incorrect |
795 ms |
34940 KB |
Output isn't correct |
87 |
Incorrect |
796 ms |
35064 KB |
Output isn't correct |
88 |
Incorrect |
802 ms |
34808 KB |
Output isn't correct |
89 |
Correct |
805 ms |
34808 KB |
Output is correct |
90 |
Incorrect |
816 ms |
34808 KB |
Output isn't correct |
91 |
Incorrect |
848 ms |
34768 KB |
Output isn't correct |
92 |
Incorrect |
805 ms |
35000 KB |
Output isn't correct |