Submission #107709

# Submission time Handle Problem Language Result Execution time Memory
107709 2019-04-25T14:06:18 Z patrikpavic2 Portals (BOI14_portals) C++17
100 / 100
553 ms 38212 KB
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <cstdlib>
#include <map>
#include <queue>
#include <cstring>

using namespace std;

typedef long long int llint;
typedef pair < int,int> pii;
typedef pair < int , pii > ppii;

const int N = 1005;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int K = 1005;

int px[4] = {0, 0 , 1 , -1};
int py[4] = {1 ,-1 , 0 , 0};

priority_queue < ppii, vector < ppii >, greater < ppii > > st;
int mv[N][N][4][2];
int dis[N][N],n,m;
char s[N][N];

int diss(int x1,int y1,int x2,int y2){
    return abs(x1 - x2) + abs(y1 - y2);
}

void dickstra(int x,int y){
    st.push(make_pair(0,make_pair(x,y)));
    dis[x][y] = 0;
    while(!st.empty()){
        ppii cur = st.top();st.pop();
        int cx = cur.second.first;
        int cy = cur.second.second;
        int t = cur.first;
        for(int i = 0;i<4;i++){
            if(s[cx + px[i]][cy + py[i]] == '#') continue;
            if(dis[cx + px[i]][cy + py[i]] <= t + 1) continue;
            dis[cx + px[i]][cy + py[i]] = t  + 1;
            st.push(make_pair(t + 1, make_pair(cx + px[i], cy + py[i])));
        }

        for(int i = 0;i<4;i++){
            for(int j = 0;j<4;j++){
                if(i == j) continue;
                int ox = mv[cx][cy][i][0];
                int oy = mv[cx][cy][i][1];
                int nx = mv[cx][cy][j][0];
                int ny = mv[cx][cy][j][1];
                int nt = diss(cx, cy, ox, oy) + 1 + t;
                if(nx < 1 || nx > n-2 || ny < 1 || ny > m-2) continue;
                if(cx == nx && cy == ny) continue;
                if(dis[nx][ny] <= nt) continue;
                dis[nx][ny] = nt;
                st.push(make_pair(nt, make_pair(nx, ny)));
            }
        }
    }
}


int main(){
    memset(dis,INF,sizeof(dis));
    scanf("%d%d",&n,&m);
    n += 2, m += 2;
    for(int i = 0;i<n;i++){
        for(int j = 0;j<m;j++){
            if(i > 0 && j > 0 && i + 1 < n && j + 1 < m) scanf(" %c", &s[i][j]);
            else s[i][j] = '#';
        }
    }
    for(int i = 1;i<n-1;i++){
        for(int j = m-2;j>0;j--){
            if(s[i][j] == '#') continue;
            if(s[i][j+1] == '#'){mv[i][j][0][0] = i;mv[i][j][0][1] = j;}
            else{ mv[i][j][0][0] = mv[i][j+1][0][0];mv[i][j][0][1] = mv[i][j+1][0][1];}
        }
    }
    for(int i = 1;i<n-1;i++){
        for(int j = 1;j<m-1;j++){
            if(s[i][j] == '#') continue;
            if(s[i][j-1] == '#'){mv[i][j][1][0] = i;mv[i][j][1][1] = j;}
            else{ mv[i][j][1][0] = mv[i][j-1][1][0];mv[i][j][1][1] = mv[i][j-1][1][1];}
        }
    }
    for(int j = 1;j<m-1;j++){
        for(int i = 1;i<n-1;i++){
            if(s[i][j] == '#') continue;
            if(s[i-1][j] == '#'){mv[i][j][2][0] = i;mv[i][j][2][1] = j;}
            else {mv[i][j][2][0] = mv[i-1][j][2][0];mv[i][j][2][1] = mv[i-1][j][2][1];}
        }
    }
    for(int j = 1;j<m-1;j++){
        for(int i = n-2;i>0;i--){
            if(s[i][j] == '#') continue;
            if(s[i+1][j] == '#'){mv[i][j][3][0] = i;mv[i][j][3][1] = j;}
            else{mv[i][j][3][0] = mv[i+1][j][3][0];mv[i][j][3][1] = mv[i+1][j][3][1];}
        }
    }
    for(int j = 1;j<m-1;j++){
        for(int i = 1;i<n-1;i++){
            if(s[i][j] ==  'S') dickstra(i, j);
        }
    }
    for(int i = 1;i<n-1;i++){
        for(int j = 1;j<m-1;j++){
            if(s[i][j] == 'C'){
                if(dis[i][j] == INF) printf("nemoguce\n");
                else printf("%d ", dis[i][j]);
            }
        }
    }
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n,&m);
     ~~~~~^~~~~~~~~~~~~~
portals.cpp:73:63: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             if(i > 0 && j > 0 && i + 1 < n && j + 1 < m) scanf(" %c", &s[i][j]);
                                                          ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4224 KB Output is correct
2 Correct 6 ms 4352 KB Output is correct
3 Correct 6 ms 4352 KB Output is correct
4 Correct 5 ms 4224 KB Output is correct
5 Correct 5 ms 4352 KB Output is correct
6 Correct 5 ms 4352 KB Output is correct
7 Correct 6 ms 4352 KB Output is correct
8 Correct 6 ms 4352 KB Output is correct
9 Correct 5 ms 4352 KB Output is correct
10 Correct 6 ms 4352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4272 KB Output is correct
2 Correct 6 ms 4352 KB Output is correct
3 Correct 5 ms 4352 KB Output is correct
4 Correct 5 ms 4224 KB Output is correct
5 Correct 5 ms 4352 KB Output is correct
6 Correct 6 ms 4352 KB Output is correct
7 Correct 5 ms 4352 KB Output is correct
8 Correct 6 ms 4480 KB Output is correct
9 Correct 6 ms 4608 KB Output is correct
10 Correct 10 ms 4608 KB Output is correct
11 Correct 7 ms 4608 KB Output is correct
12 Correct 6 ms 4608 KB Output is correct
13 Correct 6 ms 4608 KB Output is correct
14 Correct 6 ms 4352 KB Output is correct
15 Correct 6 ms 4608 KB Output is correct
16 Correct 5 ms 4224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4224 KB Output is correct
2 Correct 6 ms 4348 KB Output is correct
3 Correct 5 ms 4352 KB Output is correct
4 Correct 6 ms 4352 KB Output is correct
5 Correct 18 ms 6528 KB Output is correct
6 Correct 20 ms 6528 KB Output is correct
7 Correct 19 ms 6528 KB Output is correct
8 Correct 11 ms 6528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4224 KB Output is correct
2 Correct 5 ms 4352 KB Output is correct
3 Correct 6 ms 4352 KB Output is correct
4 Correct 6 ms 4352 KB Output is correct
5 Correct 6 ms 4352 KB Output is correct
6 Correct 7 ms 4352 KB Output is correct
7 Correct 6 ms 4352 KB Output is correct
8 Correct 7 ms 4352 KB Output is correct
9 Correct 6 ms 4608 KB Output is correct
10 Correct 7 ms 4608 KB Output is correct
11 Correct 6 ms 4608 KB Output is correct
12 Correct 7 ms 4736 KB Output is correct
13 Correct 7 ms 4608 KB Output is correct
14 Correct 16 ms 6528 KB Output is correct
15 Correct 22 ms 6528 KB Output is correct
16 Correct 18 ms 6500 KB Output is correct
17 Correct 19 ms 6528 KB Output is correct
18 Correct 21 ms 6524 KB Output is correct
19 Correct 26 ms 6656 KB Output is correct
20 Correct 20 ms 6528 KB Output is correct
21 Correct 16 ms 6528 KB Output is correct
22 Correct 19 ms 6528 KB Output is correct
23 Correct 18 ms 6528 KB Output is correct
24 Correct 17 ms 6528 KB Output is correct
25 Correct 6 ms 4224 KB Output is correct
26 Correct 7 ms 4608 KB Output is correct
27 Correct 5 ms 4224 KB Output is correct
28 Correct 11 ms 6528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4224 KB Output is correct
2 Correct 5 ms 4352 KB Output is correct
3 Correct 6 ms 4352 KB Output is correct
4 Correct 5 ms 4224 KB Output is correct
5 Correct 5 ms 4352 KB Output is correct
6 Correct 6 ms 4352 KB Output is correct
7 Correct 6 ms 4352 KB Output is correct
8 Correct 5 ms 4352 KB Output is correct
9 Correct 6 ms 4608 KB Output is correct
10 Correct 6 ms 4608 KB Output is correct
11 Correct 6 ms 4608 KB Output is correct
12 Correct 7 ms 4608 KB Output is correct
13 Correct 7 ms 4608 KB Output is correct
14 Correct 17 ms 6528 KB Output is correct
15 Correct 21 ms 6528 KB Output is correct
16 Correct 19 ms 6528 KB Output is correct
17 Correct 22 ms 6528 KB Output is correct
18 Correct 26 ms 6656 KB Output is correct
19 Correct 25 ms 6528 KB Output is correct
20 Correct 21 ms 6652 KB Output is correct
21 Correct 15 ms 6528 KB Output is correct
22 Correct 19 ms 6528 KB Output is correct
23 Correct 17 ms 6528 KB Output is correct
24 Correct 397 ms 37880 KB Output is correct
25 Correct 553 ms 38072 KB Output is correct
26 Correct 431 ms 38008 KB Output is correct
27 Correct 435 ms 38212 KB Output is correct
28 Correct 270 ms 37888 KB Output is correct
29 Correct 307 ms 37752 KB Output is correct
30 Correct 329 ms 37752 KB Output is correct
31 Correct 21 ms 6528 KB Output is correct
32 Correct 417 ms 37872 KB Output is correct
33 Correct 6 ms 4352 KB Output is correct
34 Correct 7 ms 4608 KB Output is correct
35 Correct 313 ms 31992 KB Output is correct
36 Correct 29 ms 4344 KB Output is correct
37 Correct 11 ms 6528 KB Output is correct
38 Correct 230 ms 37852 KB Output is correct
39 Correct 210 ms 23928 KB Output is correct