55562023-07-25 14:31:48marcipan5000Labirintuscpp17Wrong answer 0/1002.084s523884 KiB
#include "labirintus.h"
#include <bits/stdc++.h>

using namespace std;

int r,c;
vector<vector<int> > h;
vector<vector<int> > w;
int tim=1;
bool prop[1000001];
vector<int> t[1000001]; 
vector<int> d;

void fill(int x,int y) {
    if ((!((x>=0) && (x<r) && (y>=0) && (y<c))) || (h[x][y]!=0)) {
        return;
    }
    h[x][y]=tim;
    fill(x+1,y);
    fill(x-1,y);
    fill(x,y+1);
    fill(x,y-1);
}

void init_labyrinth(int R, int C, std::vector<std::vector<int>> L) {
    h=L; w=L;
    r=R; c=C;
    for (int i=0;i<r;i++) {
        for (int j=0;j<c;j++) {
            h[i][j]=0;
        }
    }
    for (int i=0;i<r;i++) {
        for (int j=0;j<c;j++) {
            if ((L[i][j]==1) && (h[i][j]==0)) {
                fill(i,j);
                tim++;
            }
            if (L[i][j]==0) {
                h[i][j]=tim;
                tim++;
            }
        }
    }
    for (int i=1;i<c;i++) {
        prop[h[0][i]]=1;
    }
    for (int i=0;i<r;i++) {
        prop[h[i][c-1]]=1;
    }
    return;
}

int db=1;
int volt[1000001];
bool ans=0;

void dfs(int p) {
    volt[p]=db;
    for (int i:t[p]) {
        if (volt[i]!=db) {
            dfs(volt[i]);
        }
    }
    ans=ans||prop[p];
}

bool can_escape(int N, std::vector<int> U, std::vector<int> V) {
    ans=0;
    for (int i=0;i<N;i++) {
        if ((U[i] < r-1) && (h[U[i]][V[i]]!=h[U[i]+1][V[i]]) && (w[U[i]+1][V[i]]==1)) {
            t[h[U[i]][V[i]]].push_back(h[U[i]+1][V[i]]);
            t[h[U[i]+1][V[i]]].push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]+1][V[i]]);
        }
        if ((V[i] < c-1) && (h[U[i]][V[i]]!=h[U[i]][V[i]+1]) && (w[U[i]][V[i]+1]==1)) {
            t[h[U[i]][V[i]]].push_back(h[U[i]][V[i]+1]);
            t[h[U[i]][V[i]+1]].push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]][V[i]+1]);
        }
        if ((U[i] > 0) && (h[U[i]][V[i]]!=h[U[i]-1][V[i]]) && (w[U[i]-1][V[i]]==1)) {
            t[h[U[i]][V[i]]].push_back(h[U[i]-1][V[i]]);
            t[h[U[i]-1][V[i]]].push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]-1][V[i]]);
        }
        if ((V[i] > 0) && (h[U[i]][V[i]]!=h[U[i]][V[i]-1]) && (w[U[i]][V[i]-1]==1)) {
            t[h[U[i]][V[i]]].push_back(h[U[i]][V[i]-1]);
            t[h[U[i]][V[i]-1]].push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]][V[i]-1]);
        }
        w[U[i]][V[i]]=1;
    }
    for (int i=1;i<r;i++) {
        if (volt[h[i][0]]!=db) {
            dfs(h[i][0]);
        }
    }
    for (int i=0;i<c;i++) {
        if (volt[h[r-1][i]]!=db) {
            dfs(h[r-1][i]);
        }
    }
    for (int i=0;i<N;i++) {
        w[U[i]][V[i]]=0;
    }
    for (int i:d) {
        t[i].clear();
    }
    db++;
    return(ans);
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Wrong answer25ms48684 KiB
2Time limit exceeded2.065s90916 KiB
subtask20/15
3Time limit exceeded2.079s170872 KiB
4Wrong answer30ms55900 KiB
5Wrong answer214ms62592 KiB
6Wrong answer444ms64988 KiB
7Wrong answer87ms63888 KiB
8Wrong answer89ms66100 KiB
subtask30/18
9Runtime error293ms523884 KiB
10Runtime error239ms523620 KiB
11Time limit exceeded2.075s92136 KiB
12Time limit exceeded2.078s95604 KiB
13Time limit exceeded2.072s95976 KiB
14Time limit exceeded2.084s96892 KiB
subtask40/28
15Wrong answer337ms190476 KiB
16Wrong answer337ms196100 KiB
17Wrong answer340ms201236 KiB
18Wrong answer224ms202648 KiB
19Time limit exceeded2.063s126120 KiB
20Wrong answer181ms209044 KiB
21Wrong answer180ms210312 KiB
22Time limit exceeded2.079s133540 KiB
23Time limit exceeded2.082s138308 KiB
subtask50/39
24Wrong answer1.756s93388 KiB
25Wrong answer412ms92288 KiB
26Wrong answer208ms102744 KiB
27Wrong answer444ms105024 KiB
28Wrong answer289ms108004 KiB
29Wrong answer439ms110768 KiB
30Time limit exceeded2.066s145108 KiB
31Time limit exceeded2.048s149056 KiB
32Time limit exceeded2.072s149692 KiB
33Time limit exceeded2.072s152424 KiB
34Time limit exceeded2.072s152528 KiB
35Time limit exceeded2.059s154004 KiB
36Wrong answer321ms247600 KiB
37Wrong answer323ms252580 KiB
38Wrong answer310ms257304 KiB
39Wrong answer338ms261460 KiB