Submission #943133

# Submission time Handle Problem Language Result Execution time Memory
943133 2024-03-11T08:38:35 Z salmon Portals (BOI14_portals) C++14
0 / 100
317 ms 262144 KB
#include <bits/stdc++.h>
using namespace std;

int R;
int C;
int x,y;
int gx,gy;
int sx,sy;
char c;
int lst[1100][1100];
int nextr[1100][1100];
int nextc[1100][1100];
int prevr[1100][1100];
int prevc[1100][1100];

int dw[1100][1100];
int d[1100][1100][2];

queue<vector<int>> q;

vector<pair<int,int>> bah = {{0,-1},{0,1},{1,0},{-1,0}};

bool valid(int i, int j){
    if(i >= 0 && i < R && 0 <= j && j < C) return true;
    return false;
}

int main(){
    scanf(" %d",&R);
    scanf(" %d",&C);

    for(int i = 0; i < R; i++){
        for(int j = 0; j < C; j++){
            scanf(" %c",&c);
            if(c == '#'){
                lst[i][j] = 1;
            }
            else{
                lst[i][j] = 0;
            }

            if(c == 'C'){
                gx = i;
                gy = j;
            }
            if(c == 'S'){
                sx = i;
                sy = j;
            }

            d[i][j][0] = -1;
            d[i][j][1] = -1;
            dw[i][j] = -1;
        }
    }

    for(int i = 0; i < R; i++){
        vector<int> s;

        for(int j = 0; j < C; j++){
            while(!s.empty() && lst[i][j] == 1){
                nextr[i][s.back()] = j;
                s.pop_back();
            }
            s.push_back(j);
        }

        while(!s.empty()){
            nextr[i][s.back()] = C;
            s.pop_back();
        }
    }

    for(int j = 0; j < C; j++){
        vector<int> s;

        for(int i = 0; i < R; i++){
            while(!s.empty() && lst[i][j] == 1){
                nextc[s.back()][j] = i;
                s.pop_back();
            }
            s.push_back(j);
        }

        while(!s.empty()){
            nextc[s.back()][j] = C;
            s.pop_back();
        }
    }


    for(int i = 0; i < R; i++){
        vector<int> s;

        for(int j = C - 1; j >= 0; j--){
            while(!s.empty() && lst[i][j] == 1){
                prevr[i][s.back()] = j;
                s.pop_back();
            }
            s.push_back(j);
        }

        while(!s.empty()){
            prevr[i][s.back()] = -1;
            s.pop_back();
        }
    }

    for(int j = 0; j < C; j++){
        vector<int> s;

        for(int i = R - 1; i >= 0; i--){
            while(!s.empty() && lst[i][j] == 1){
                prevc[s.back()][j] = i;
                s.pop_back();
            }
            s.push_back(j);
        }

        while(!s.empty()){
            prevc[s.back()][j] = -1;
            s.pop_back();
        }
    }

    /*for(int i = 0; i < R; i++){
        for(int j = 0; j < C; j++){
            if(lst[i][j] == -1) q.push({i,j,0});
        }
    }

    while(!q.empty()){
        vector<int> v = q.top();
        q.pop();
        int i = v[0];
        int j = v[1];

        if(dw[i][j] != -1) continue;

        dw[i][j] = v[2];

        for(pair<int,int> ii : bah){
            int ni = ii.first + i;
            int nj = ii.second + j;

            if(valid(ni,nj)){
                q.push({ni,nj,v[2] + 1});
            }
        }
    }*/

    q.push({sx,sy,true,0});
    q.push({sx,sy,false,0});

    while(!q.empty()){
        vector<int> v = q.front();
        q.pop();

        int i = v[0];
        int j = v[1];
        int z = v[2];

        if(d[i][j][z] != -1) continue;

        for(pair<int,int> ii : bah){
            int ni = ii.first + i;
            int nj = ii.second + j;

            if(valid(ni,nj)){
                q.push({ni,nj,z,v[2] + 1});
            }
        }

        if(z){
            if(!valid(i,j+1) || lst[i][j + 1] == 1){
                q.push({i,prevr[i][j] + 1,z,v[2] + 1});
            }
            if(!valid(i,j-1) || lst[i][j - 1] == 1){
                q.push({i,nextr[i][j] - 1,z,v[2] + 1});
            }
            if(!valid(i+1,j) || lst[i+1][j] == 1){
                q.push({prevc[i][j] + 1,j,z,v[2] + 1});
            }
            if(!valid(i-1,j) || lst[i-1][j] == 1){
                q.push({nextc[i][j] - 1,j,z,v[2] + 1});
            }

            q.push({i,prevr[i][j] + 1,false,v[2] + 1});
            q.push({i,nextr[i][j] - 1,false,v[2] + 1});
            q.push({prevc[i][j] + 1,j,false,v[2] + 1});
            q.push({nextc[i][j] - 1,j,false,v[2] + 1});
        }
    }

    printf("%d",d[gx][gy][false]);

}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |     scanf(" %d",&R);
      |     ~~~~~^~~~~~~~~~
portals.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     scanf(" %d",&C);
      |     ~~~~~^~~~~~~~~~
portals.cpp:34:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |             scanf(" %c",&c);
      |             ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 317 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 303 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 302 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 307 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 305 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -