55572023-07-25 15:21:26marcipan5000Labirintuscpp17Time limit exceeded 82/1002.076s155268 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) || (w[x][y]==0)) {
        return;
    }
    h[x][y]=tim;
    fill(x+1,y);
    fill(x-1,y);
    fill(x,y+1);
    fill(x,y-1);
    fill(x+1,y+1);
    fill(x+1,y-1);
    fill(x-1,y+1);
    fill(x-1,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-1;i++) {
        prop[h[i][c-1]]=1;
    }
    return;
}

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

bool dfs(int p) {
    volt[p]=db;
    for (int i:t[p]) {
        if (volt[i]!=db) {
            if (dfs(i)) {
                return(1);
            }
        }
    }
    return(prop[p]);
}

void kiir() {
    for (int i=0;i<r;i++) {
        for (int j=0;j<c;j++) {
            cout << prop[h[i][j]] << " ";
        }
        cout << endl;
    }
    cout << endl;
    cout << "ans: " << ans << endl;
    cout << "db: " << db << endl;
    for (int i=0;i<r;i++) {
        for (int j=0;j<c;j++) {
            cout << w[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

bool can_escape(int N, std::vector<int> U, std::vector<int> V) {
    ans=0;
    d.clear();
    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]);
        }
        if ((U[i] < r-1) && (V[i] < c-1) && (h[U[i]][V[i]]!=h[U[i]+1][V[i]+1]) && (w[U[i]+1][V[i]+1]==1)) {
            t[h[U[i]][V[i]]].push_back(h[U[i]+1][V[i]+1]);
            t[h[U[i]+1][V[i]+1]].push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]+1][V[i]+1]);
        }
        if ((U[i] < r-1) && (V[i] > 0) && (h[U[i]][V[i]]!=h[U[i]+1][V[i]-1]) && (w[U[i]+1][V[i]-1]==1)) {
            t[h[U[i]][V[i]]].push_back(h[U[i]+1][V[i]-1]);
            t[h[U[i]+1][V[i]-1]].push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]+1][V[i]-1]);
        }
        if ((U[i] > 0) && (V[i] < c-1) && (h[U[i]][V[i]]!=h[U[i]-1][V[i]+1]) && (w[U[i]-1][V[i]+1]==1)) {
            t[h[U[i]][V[i]]].push_back(h[U[i]-1][V[i]+1]);
            t[h[U[i]-1][V[i]+1]].push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]-1][V[i]+1]);
        }
        if ((U[i] > 0) && (V[i] > 0) && (h[U[i]][V[i]]!=h[U[i]-1][V[i]-1]) && (w[U[i]-1][V[i]-1]==1)) {
            t[h[U[i]][V[i]]].push_back(h[U[i]-1][V[i]-1]);
            t[h[U[i]-1][V[i]-1]].push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]][V[i]]);
            d.push_back(h[U[i]-1][V[i]-1]);
        }
        
        w[U[i]][V[i]]=1;
    }
    //kiir();
    for (int i=1;i<r;i++) {
        if (volt[h[i][0]]!=db) {
            if (dfs(h[i][0])) {
                ans=1;
                break;
            }
        }
    }
    if (ans==0) {
        for (int i=0;i<c-1;i++) {
            if (dfs(h[r-1][i])) {
                ans=1;
                break;
            }
        }
    }
    for (int i=0;i<N;i++) {
        w[U[i]][V[i]]=0;
    }
    for (int i:d) {
        t[i].clear();
    }
    db++;
    //kiir();
    return(!ans);
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted24ms48884 KiB
2Accepted740ms88640 KiB
subtask215/15
3Accepted52ms54848 KiB
4Accepted24ms53572 KiB
5Accepted143ms56332 KiB
6Accepted145ms58184 KiB
7Accepted129ms62472 KiB
8Accepted115ms64736 KiB
subtask30/18
9Accepted377ms66872 KiB
10Accepted246ms69644 KiB
11Time limit exceeded2.072s72720 KiB
12Accepted642ms131616 KiB
13Accepted1.639s135468 KiB
14Time limit exceeded2.076s78928 KiB
subtask428/28
15Accepted615ms131464 KiB
16Accepted550ms137008 KiB
17Accepted301ms132144 KiB
18Accepted254ms139108 KiB
19Accepted296ms116404 KiB
20Accepted158ms130960 KiB
21Accepted170ms134644 KiB
22Accepted282ms116668 KiB
23Accepted282ms116816 KiB
subtask539/39
24Accepted57ms80412 KiB
25Accepted41ms80356 KiB
26Accepted146ms81744 KiB
27Accepted141ms80664 KiB
28Accepted140ms81084 KiB
29Accepted144ms80660 KiB
30Accepted1.516s141772 KiB
31Accepted575ms144500 KiB
32Accepted560ms143588 KiB
33Accepted705ms148696 KiB
34Accepted1.465s130268 KiB
35Accepted1.363s132220 KiB
36Accepted540ms145020 KiB
37Accepted711ms146568 KiB
38Accepted898ms155268 KiB
39Accepted661ms144068 KiB