Submission #608387

# Submission time Handle Problem Language Result Execution time Memory
608387 2022-07-27T07:23:14 Z nguyentu Portals (BOI14_portals) C++14
100 / 100
545 ms 112972 KB
#include <bits/stdc++.h>

#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;

#define ii pair<int , int>  
#define iv pair<ii , ii>
#define iii pair<int , ii>
#define fi first
#define se second
#define int long long
const int inf = 1e18 + 7;
const int MAX_N = 1e3 + 7 ;
const int MOD = 1e9 + 7;

int dx[] = {1 , -1 , 0 , 0};
int dy[] = {0 , 0 , 1 , -1};

char a[MAX_N][MAX_N];
int n , m;
int neleft[MAX_N][MAX_N] , neright[MAX_N][MAX_N];
int neup[MAX_N][MAX_N] , nedown[MAX_N][MAX_N];
int dist[MAX_N][MAX_N];
vector<int> b[MAX_N][MAX_N];

bool check(int x , int y) {
    return (1 <= x && 1 <= y && x <= n && y <= m);
}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    cin >> n >> m;   
    ii s , t;
    for (int i = 1 ; i <= n ; i++) {
        for (int j = 1 ; j <= m ; j++) {
            cin >> a[i][j];
            if (a[i][j] == 'S') s = ii(i , j);
            if (a[i][j] == 'C') t = ii(i , j); 
        }
    }

    for (int i = 1 ; i <= n ; i ++) { 
        for (int j = 1 ; j <= m ; j++) {
            if (a[i][j] == '#') {
                neleft[i][j] = j;
                neup[i][j] = i;
            }
            else {
                neleft[i][j] = neleft[i][j - 1];
                neup[i][j] = neup[i - 1][j];
            }
        }   
    }
    for (int i = 1 ; i <= m ; i++) {
        nedown[n + 1][i] = n + 1;
    }
    for (int i = n ; i >= 1 ; i--) {
        neright[i][m + 1] = m + 1;
        for (int j = m ; j >= 1 ; j--) {
            if (a[i][j] == '#') {
                neright[i][j] = j;
                nedown[i][j] = i;
            }
            else {
                neright[i][j] = neright[i][j + 1];
                nedown[i][j] = nedown[i + 1][j];
            }
        }
    }
    for (int i = 1 ; i <= n ;i ++) {
        for (int j = 1 ; j <= m ; j++) {
            b[i][j].push_back(j - neleft[i][j] - 1);
            b[i][j].push_back(neright[i][j] - j - 1);
            b[i][j].push_back(i - neup[i][j] - 1);
            b[i][j].push_back(nedown[i][j] - i - 1);
            sort(b[i][j].begin() , b[i][j].end());
            dist[i][j] = inf;
        }
    }

    priority_queue<iii , vector<iii> , greater<iii>> pq; 
    pq.push(iii(0 , s));
    dist[s.fi][s.se] = 0;
    while (!pq.empty()) {
        int w , x , y;
        w = pq.top().fi;
        x = pq.top().se.fi;
        y = pq.top().se.se;
        pq.pop(); 
        if (w != dist[x][y] || a[x][y] == '#') continue;
        for (int i = 0 ; i < 4 ; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (check(nx , ny) && (dist[x][y] + 1) < dist[nx][ny]) {
                dist[nx][ny] = dist[x][y] + 1;
                pq.push(iii(dist[nx][ny] , ii(nx , ny)));
            }
        }
        int val = b[x][y][0];
        for (int i = 0 ; i < 4 ; i++) {
            int nx = 0 , ny = 0;
            if (i == 0) {
                ny = neleft[x][y] + 1;
                nx = x;
            }
            if (i == 1) {
                ny = neright[x][y] - 1;
                nx = x;
            }
            if (i == 2) {
                ny = y;
                nx = neup[x][y] + 1;
            }
            if (i == 3) {
                ny = y;
                nx = nedown[x][y] - 1;
            }
            // if (nx == 1 && ny == 4) {
            //     cerr << x << " " << y << " " << dist[x][y] + val + 1 << " " << '\n';
            // }
            if (check(nx , ny) && (dist[x][y] + val + 1) < dist[nx][ny]) {
                dist[nx][ny] = dist[x][y] + val + 1;
                pq.push(iii(dist[nx][ny] , ii(nx , ny)));
            }
        }
    }
    //cout << neleft[1][1] << " " << neright[1][1] << " " << nedown[1][1] << " " << neup[1][1] << '\n';
    cout << dist[t.fi][t.se];
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 24148 KB Output is correct
2 Correct 12 ms 24276 KB Output is correct
3 Correct 12 ms 24276 KB Output is correct
4 Correct 13 ms 24216 KB Output is correct
5 Correct 12 ms 24300 KB Output is correct
6 Correct 12 ms 24372 KB Output is correct
7 Correct 13 ms 24368 KB Output is correct
8 Correct 13 ms 24388 KB Output is correct
9 Correct 12 ms 24276 KB Output is correct
10 Correct 14 ms 24148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24176 KB Output is correct
2 Correct 15 ms 24372 KB Output is correct
3 Correct 13 ms 24276 KB Output is correct
4 Correct 13 ms 24268 KB Output is correct
5 Correct 16 ms 24396 KB Output is correct
6 Correct 13 ms 24276 KB Output is correct
7 Correct 15 ms 24392 KB Output is correct
8 Correct 12 ms 24396 KB Output is correct
9 Correct 13 ms 25428 KB Output is correct
10 Correct 13 ms 25352 KB Output is correct
11 Correct 15 ms 25440 KB Output is correct
12 Correct 15 ms 25432 KB Output is correct
13 Correct 14 ms 25428 KB Output is correct
14 Correct 13 ms 24276 KB Output is correct
15 Correct 14 ms 25452 KB Output is correct
16 Correct 13 ms 24148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 24148 KB Output is correct
2 Correct 14 ms 24292 KB Output is correct
3 Correct 12 ms 24276 KB Output is correct
4 Correct 12 ms 24404 KB Output is correct
5 Correct 30 ms 31856 KB Output is correct
6 Correct 29 ms 31828 KB Output is correct
7 Correct 28 ms 31828 KB Output is correct
8 Correct 25 ms 31832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24152 KB Output is correct
2 Correct 13 ms 24336 KB Output is correct
3 Correct 13 ms 24276 KB Output is correct
4 Correct 13 ms 24172 KB Output is correct
5 Correct 13 ms 24364 KB Output is correct
6 Correct 13 ms 24276 KB Output is correct
7 Correct 13 ms 24276 KB Output is correct
8 Correct 13 ms 24276 KB Output is correct
9 Correct 15 ms 25428 KB Output is correct
10 Correct 14 ms 25428 KB Output is correct
11 Correct 14 ms 25428 KB Output is correct
12 Correct 14 ms 25412 KB Output is correct
13 Correct 14 ms 25428 KB Output is correct
14 Correct 26 ms 31820 KB Output is correct
15 Correct 27 ms 31820 KB Output is correct
16 Correct 30 ms 31836 KB Output is correct
17 Correct 27 ms 31828 KB Output is correct
18 Correct 28 ms 31832 KB Output is correct
19 Correct 34 ms 31912 KB Output is correct
20 Correct 28 ms 31936 KB Output is correct
21 Correct 28 ms 31832 KB Output is correct
22 Correct 29 ms 31876 KB Output is correct
23 Correct 27 ms 31808 KB Output is correct
24 Correct 27 ms 31876 KB Output is correct
25 Correct 12 ms 24276 KB Output is correct
26 Correct 14 ms 25364 KB Output is correct
27 Correct 14 ms 24148 KB Output is correct
28 Correct 24 ms 31852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 24148 KB Output is correct
2 Correct 13 ms 24272 KB Output is correct
3 Correct 13 ms 24276 KB Output is correct
4 Correct 13 ms 24264 KB Output is correct
5 Correct 14 ms 24276 KB Output is correct
6 Correct 13 ms 24276 KB Output is correct
7 Correct 14 ms 24404 KB Output is correct
8 Correct 15 ms 24276 KB Output is correct
9 Correct 13 ms 25428 KB Output is correct
10 Correct 14 ms 25424 KB Output is correct
11 Correct 14 ms 25428 KB Output is correct
12 Correct 14 ms 25424 KB Output is correct
13 Correct 14 ms 25420 KB Output is correct
14 Correct 25 ms 31828 KB Output is correct
15 Correct 27 ms 31800 KB Output is correct
16 Correct 28 ms 31820 KB Output is correct
17 Correct 29 ms 31768 KB Output is correct
18 Correct 32 ms 31848 KB Output is correct
19 Correct 29 ms 31840 KB Output is correct
20 Correct 32 ms 31944 KB Output is correct
21 Correct 28 ms 31816 KB Output is correct
22 Correct 33 ms 31828 KB Output is correct
23 Correct 29 ms 31836 KB Output is correct
24 Correct 413 ms 112820 KB Output is correct
25 Correct 545 ms 112972 KB Output is correct
26 Correct 429 ms 112716 KB Output is correct
27 Correct 408 ms 112852 KB Output is correct
28 Correct 320 ms 112400 KB Output is correct
29 Correct 342 ms 112512 KB Output is correct
30 Correct 391 ms 112592 KB Output is correct
31 Correct 30 ms 31816 KB Output is correct
32 Correct 418 ms 112596 KB Output is correct
33 Correct 15 ms 24276 KB Output is correct
34 Correct 14 ms 25424 KB Output is correct
35 Correct 374 ms 112588 KB Output is correct
36 Correct 13 ms 24148 KB Output is correct
37 Correct 23 ms 31788 KB Output is correct
38 Correct 254 ms 112464 KB Output is correct
39 Correct 355 ms 112520 KB Output is correct