5395 | 2023-05-09 14:12:07 | mraron | Labirintus | cpp17 | Elfogadva 100/100 | 439ms | 43344 KiB |
#include "labirintus.h"
#include <algorithm>
using namespace std;
int M, R, C;
vector<vector<int>> labyrinth;
vector<int> sz, par;
vector<pair<int,int>> hist;
int find_par(int x) {
return x==par[x] ? x : find_par(par[x]);
}
void unite(int r1, int c1, int r2, int c2, bool save=false) {
int x = r1*C+c1, y;
if (r2<0 || r2>=R) y = (r2==R ? M-2 : M-1);
else if (c2<0 || c2>=C) y = (c2==C ? M-1 : M-2);
else y = r2*C+c2;
x = find_par(x), y = find_par(y);
if (x==y) return;
if (sz[x] < sz[y]) swap(x,y);
sz[x] += sz[y];
par[y] = x;
if (save) hist.push_back({x, y});
}
void init_labyrinth(int _R, int _C, std::vector<std::vector<int>> L) {
R = _R, C = _C;
labyrinth = L;
M = R*C+2;
sz.assign(M, 1), par.resize(M);
for (int i=0; i<M; ++i) par[i] = i;
for (int i=0; i<R; ++i) {
for (int j=0; j<C; ++j) {
if (labyrinth[i][j] == 0) continue;
for (int x=i-1; x<=i+1; ++x)
for (int y=j-1; y<=j+1; ++y)
if (x<0 || x>=R || y<0 || y>=C || labyrinth[x][y] == 1)
unite(i,j, x,y);
}
}
}
bool can_escape(int N, std::vector<int> U, std::vector<int> V) {
hist.clear();
for (int k=0; k<N; ++k) {
int i = U[k], j = V[k];
for (int x=i-1; x<=i+1; ++x)
for (int y=j-1; y<=j+1; ++y)
if (x<0 || x>=R || y<0 || y>=C || labyrinth[x][y] == 1)
unite(i,j, x,y, true);
labyrinth[i][j] = 1;
}
bool ans = (find_par(M-2) != find_par(M-1));
reverse(hist.begin(), hist.end());
for (auto [x,y] : hist) {
par[y] = y;
sz[x] -= sz[y];
}
for (int k=0; k<N; ++k) {
labyrinth[U[k]][V[k]] = 0;
}
return ans;
}
Részfeladat | Összpont | Teszt | Verdikt | Idő | Memória | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Elfogadva | 3ms | 1328 KiB | ||||
2 | Elfogadva | 178ms | 40500 KiB | ||||
subtask2 | 15/15 | ||||||
3 | Elfogadva | 32ms | 3180 KiB | ||||
4 | Elfogadva | 8ms | 3396 KiB | ||||
5 | Elfogadva | 115ms | 2892 KiB | ||||
6 | Elfogadva | 115ms | 2608 KiB | ||||
7 | Elfogadva | 81ms | 3800 KiB | ||||
8 | Elfogadva | 82ms | 4164 KiB | ||||
subtask3 | 18/18 | ||||||
9 | Elfogadva | 209ms | 3684 KiB | ||||
10 | Elfogadva | 182ms | 3084 KiB | ||||
11 | Elfogadva | 416ms | 41684 KiB | ||||
12 | Elfogadva | 402ms | 42024 KiB | ||||
13 | Elfogadva | 398ms | 41976 KiB | ||||
14 | Elfogadva | 439ms | 42076 KiB | ||||
subtask4 | 28/28 | ||||||
15 | Elfogadva | 257ms | 42412 KiB | ||||
16 | Elfogadva | 259ms | 42632 KiB | ||||
17 | Elfogadva | 263ms | 42576 KiB | ||||
18 | Elfogadva | 150ms | 42704 KiB | ||||
19 | Elfogadva | 252ms | 42636 KiB | ||||
20 | Elfogadva | 120ms | 42592 KiB | ||||
21 | Elfogadva | 122ms | 42920 KiB | ||||
22 | Elfogadva | 250ms | 42604 KiB | ||||
23 | Elfogadva | 246ms | 42864 KiB | ||||
subtask5 | 39/39 | ||||||
24 | Elfogadva | 35ms | 3960 KiB | ||||
25 | Elfogadva | 17ms | 3868 KiB | ||||
26 | Elfogadva | 115ms | 4420 KiB | ||||
27 | Elfogadva | 115ms | 3960 KiB | ||||
28 | Elfogadva | 115ms | 4184 KiB | ||||
29 | Elfogadva | 115ms | 3968 KiB | ||||
30 | Elfogadva | 432ms | 42984 KiB | ||||
31 | Elfogadva | 402ms | 43036 KiB | ||||
32 | Elfogadva | 275ms | 43032 KiB | ||||
33 | Elfogadva | 405ms | 43236 KiB | ||||
34 | Elfogadva | 229ms | 43316 KiB | ||||
35 | Elfogadva | 228ms | 43276 KiB | ||||
36 | Elfogadva | 310ms | 43180 KiB | ||||
37 | Elfogadva | 287ms | 43180 KiB | ||||
38 | Elfogadva | 294ms | 43244 KiB | ||||
39 | Elfogadva | 289ms | 43344 KiB |