/*
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 (!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-1)/s) >= dist[curr.ss.ff][curr.ss.ss]) {
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 |
0 ms |
288 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 |
838 ms |
35064 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 |
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 |
4 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 |
3 ms |
768 KB |
Output is correct |
26 |
Correct |
3 ms |
768 KB |
Output is correct |
27 |
Correct |
3 ms |
824 KB |
Output is correct |
28 |
Correct |
3 ms |
896 KB |
Output is correct |
29 |
Correct |
2 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 |
58 ms |
7800 KB |
Output is correct |
34 |
Correct |
68 ms |
7800 KB |
Output is correct |
35 |
Incorrect |
130 ms |
7928 KB |
Output isn't correct |
36 |
Correct |
87 ms |
9932 KB |
Output is correct |
37 |
Correct |
89 ms |
9848 KB |
Output is correct |
38 |
Incorrect |
170 ms |
9848 KB |
Output isn't correct |
39 |
Correct |
94 ms |
12152 KB |
Output is correct |
40 |
Correct |
116 ms |
12216 KB |
Output is correct |
41 |
Incorrect |
218 ms |
12152 KB |
Output isn't correct |
42 |
Correct |
121 ms |
14712 KB |
Output is correct |
43 |
Correct |
142 ms |
14712 KB |
Output is correct |
44 |
Incorrect |
287 ms |
14840 KB |
Output isn't correct |
45 |
Correct |
149 ms |
17400 KB |
Output is correct |
46 |
Correct |
173 ms |
17400 KB |
Output is correct |
47 |
Incorrect |
353 ms |
17404 KB |
Output isn't correct |
48 |
Correct |
183 ms |
20344 KB |
Output is correct |
49 |
Correct |
209 ms |
20344 KB |
Output is correct |
50 |
Incorrect |
703 ms |
20388 KB |
Output isn't correct |
51 |
Correct |
217 ms |
23580 KB |
Output is correct |
52 |
Correct |
258 ms |
23696 KB |
Output is correct |
53 |
Incorrect |
514 ms |
23672 KB |
Output isn't correct |
54 |
Correct |
254 ms |
27192 KB |
Output is correct |
55 |
Correct |
453 ms |
27096 KB |
Output is correct |
56 |
Incorrect |
586 ms |
27128 KB |
Output isn't correct |
57 |
Correct |
284 ms |
30840 KB |
Output is correct |
58 |
Correct |
486 ms |
30828 KB |
Output is correct |
59 |
Execution timed out |
1095 ms |
30840 KB |
Time limit exceeded |
60 |
Correct |
388 ms |
34808 KB |
Output is correct |
61 |
Correct |
410 ms |
34824 KB |
Output is correct |
62 |
Incorrect |
851 ms |
34992 KB |
Output isn't correct |
63 |
Incorrect |
886 ms |
34424 KB |
Output isn't correct |
64 |
Correct |
832 ms |
34608 KB |
Output is correct |
65 |
Incorrect |
860 ms |
34436 KB |
Output isn't correct |
66 |
Incorrect |
854 ms |
34424 KB |
Output isn't correct |
67 |
Incorrect |
811 ms |
34424 KB |
Output isn't correct |
68 |
Incorrect |
860 ms |
34484 KB |
Output isn't correct |
69 |
Incorrect |
852 ms |
34608 KB |
Output isn't correct |
70 |
Incorrect |
853 ms |
34424 KB |
Output isn't correct |
71 |
Incorrect |
797 ms |
34496 KB |
Output isn't correct |
72 |
Incorrect |
739 ms |
34680 KB |
Output isn't correct |
73 |
Incorrect |
810 ms |
34936 KB |
Output isn't correct |
74 |
Correct |
841 ms |
35000 KB |
Output is correct |
75 |
Incorrect |
893 ms |
34808 KB |
Output isn't correct |
76 |
Incorrect |
837 ms |
34944 KB |
Output isn't correct |
77 |
Incorrect |
842 ms |
34936 KB |
Output isn't correct |
78 |
Incorrect |
757 ms |
34936 KB |
Output isn't correct |
79 |
Correct |
752 ms |
34808 KB |
Output is correct |
80 |
Incorrect |
733 ms |
34788 KB |
Output isn't correct |
81 |
Incorrect |
760 ms |
34832 KB |
Output isn't correct |
82 |
Incorrect |
751 ms |
34936 KB |
Output isn't correct |
83 |
Incorrect |
741 ms |
34904 KB |
Output isn't correct |
84 |
Correct |
784 ms |
35068 KB |
Output is correct |
85 |
Incorrect |
775 ms |
34916 KB |
Output isn't correct |
86 |
Incorrect |
765 ms |
34776 KB |
Output isn't correct |
87 |
Incorrect |
776 ms |
34936 KB |
Output isn't correct |
88 |
Incorrect |
804 ms |
34808 KB |
Output isn't correct |
89 |
Correct |
794 ms |
35064 KB |
Output is correct |
90 |
Incorrect |
804 ms |
34812 KB |
Output isn't correct |
91 |
Incorrect |
801 ms |
34756 KB |
Output isn't correct |
92 |
Incorrect |
805 ms |
35040 KB |
Output isn't correct |