55572023-07-25 15:21:26marcipan5000Labirintuscpp17Időlimit túllépés 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);
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva24ms48884 KiB
2Elfogadva740ms88640 KiB
subtask215/15
3Elfogadva52ms54848 KiB
4Elfogadva24ms53572 KiB
5Elfogadva143ms56332 KiB
6Elfogadva145ms58184 KiB
7Elfogadva129ms62472 KiB
8Elfogadva115ms64736 KiB
subtask30/18
9Elfogadva377ms66872 KiB
10Elfogadva246ms69644 KiB
11Időlimit túllépés2.072s72720 KiB
12Elfogadva642ms131616 KiB
13Elfogadva1.639s135468 KiB
14Időlimit túllépés2.076s78928 KiB
subtask428/28
15Elfogadva615ms131464 KiB
16Elfogadva550ms137008 KiB
17Elfogadva301ms132144 KiB
18Elfogadva254ms139108 KiB
19Elfogadva296ms116404 KiB
20Elfogadva158ms130960 KiB
21Elfogadva170ms134644 KiB
22Elfogadva282ms116668 KiB
23Elfogadva282ms116816 KiB
subtask539/39
24Elfogadva57ms80412 KiB
25Elfogadva41ms80356 KiB
26Elfogadva146ms81744 KiB
27Elfogadva141ms80664 KiB
28Elfogadva140ms81084 KiB
29Elfogadva144ms80660 KiB
30Elfogadva1.516s141772 KiB
31Elfogadva575ms144500 KiB
32Elfogadva560ms143588 KiB
33Elfogadva705ms148696 KiB
34Elfogadva1.465s130268 KiB
35Elfogadva1.363s132220 KiB
36Elfogadva540ms145020 KiB
37Elfogadva711ms146568 KiB
38Elfogadva898ms155268 KiB
39Elfogadva661ms144068 KiB