답안 #1035462

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1035462 2024-07-26T11:21:00 Z ByeWorld 포탈들 (BOI14_portals) C++14
100 / 100
769 ms 152968 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
// #define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
using namespace std;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
typedef pair<pii,int> iipi;
const int MAXN = 1e3+5;
const int INF = 1e9+10;
void chmn(int &a, int b){ a = min(a, b); }
vector <int> dx = {1, -1, 0, 0};
vector <int> dy = {0, 0, 1, -1};

int n, m, ex, ey, sx, sy;
char s[MAXN][MAXN];
int near[MAXN][MAXN], dis[MAXN][MAXN];
vector <iipi> adj[MAXN][MAXN]; 

bool valid(int x, int y){
    if(x<=0 || y<=0 || x>n || y>m || s[x][y]=='#') return 0;
    return 1;
}

void bfs(){
    for(int i=0; i<=n+1; i++)
        for(int j=0; j<=m+1; j++) near[i][j] = INF;
    queue <pii> q; 
    for(int i=0; i<=n+1; i++){
        for(int j=0; j<=m+1; j++){
            if(s[i][j]=='#') q.push({i, j}), near[i][j] = 0;
        }
    }

    while(!q.empty()){
        int x = q.front().fi, y = q.front().se; q.pop();
        for(int i=0; i<4; i++){
            int nx = x+dx[i], ny = y+dy[i];
            if(valid(nx,ny) && near[nx][ny] > near[x][y]+1){
                near[nx][ny] = near[x][y]+1;
                q.push({nx, ny});
            }
        }
    }
}
void add(int a, int b, int c, int d, int wei){
    if(!valid(a, b) || !valid(c, d)) return;
    adj[a][b].pb({{c, d}, wei});
}

priority_queue <ipii, vector<ipii>, greater<ipii>> pq;
void dji(){
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++) dis[i][j] = INF;
    pq.push({0, {sx, sy}}); dis[sx][sy] = 0;
    
    while(!pq.empty()){
        int dist = pq.top().fi, x = pq.top().se.fi, y = pq.top().se.se;
        pq.pop();
        if(dis[x][y] < dist) continue;
        // cout << x << ' ' << y<< ' ' << dis[x][y] << " xy\n";
        for(auto [id, wei] : adj[x][y]){
            int nx = id.fi, ny = id.se;
            if(valid(nx,ny) && dis[nx][ny] > dis[x][y] + wei){
                dis[nx][ny] = dis[x][y] + wei;
                pq.push({dis[nx][ny], {nx, ny}});
            }
        }
    }
}
signed main(){
    // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for(int i=0; i<=n+1; i++)
        for(int j=0; j<=m+1; j++) s[i][j] = '#';
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            cin >> s[i][j];
            if(s[i][j] == 'S') sx = i, sy = j;
            if(s[i][j] == 'C') ex = i, ey = j;
        }
    }
    bfs();
    for(int i=1; i<=n; i++){
        int las = 1;
        for(int j=1; j<=m; j++){
            if(s[i][j] == '#') las = j+1;
            else add(i, j, i, las, near[i][j]);
        }
    }
    for(int i=1; i<=n; i++){
        int las = m;
        for(int j=m; j>=1; j--){
            if(s[i][j] == '#') las = j-1;
            else add(i, j, i, las, near[i][j]);
        }
    }
    for(int j=1; j<=m; j++){
        int las = 1;
        for(int i=1; i<=n; i++){
            if(s[i][j] == '#') las = i+1;
            else {
                // cout << i << ' ' << j << ' ' << las << ' ' << j << ' ' << near[i][j] << "near\n";
                add(i, j, las, j, near[i][j]);
            }
        }
    }
    for(int j=1; j<=m; j++){
        int las = n;
        for(int i=n; i>=1; i--){
            if(s[i][j] == '#') las = i-1;
            else add(i, j, las, j, near[i][j]);
        }
    }
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            for(int x=0; x<4; x++)
                add(i, j, i+dx[x], j+dy[x], 1);
    dji();
    cout << dis[ex][ey] << '\n';

    // for(int i=1; i<=n; i++){
    //     for(int j=1; j<=m; j++){
    //         cout << near[i][j] << " \n"[j==m];
    //     }
    // }
}

Compilation message

portals.cpp: In function 'void dji()':
portals.cpp:68:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   68 |         for(auto [id, wei] : adj[x][y]){
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 24152 KB Output is correct
2 Correct 17 ms 24152 KB Output is correct
3 Correct 10 ms 24056 KB Output is correct
4 Correct 10 ms 24156 KB Output is correct
5 Correct 11 ms 24296 KB Output is correct
6 Correct 11 ms 24156 KB Output is correct
7 Correct 12 ms 24156 KB Output is correct
8 Correct 13 ms 24152 KB Output is correct
9 Correct 11 ms 24152 KB Output is correct
10 Correct 10 ms 24156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 24080 KB Output is correct
2 Correct 11 ms 24156 KB Output is correct
3 Correct 11 ms 24156 KB Output is correct
4 Correct 11 ms 24156 KB Output is correct
5 Correct 17 ms 24156 KB Output is correct
6 Correct 11 ms 24168 KB Output is correct
7 Correct 11 ms 24152 KB Output is correct
8 Correct 10 ms 24156 KB Output is correct
9 Correct 12 ms 24920 KB Output is correct
10 Correct 12 ms 24844 KB Output is correct
11 Correct 11 ms 24668 KB Output is correct
12 Correct 17 ms 24824 KB Output is correct
13 Correct 19 ms 24668 KB Output is correct
14 Correct 10 ms 24008 KB Output is correct
15 Correct 11 ms 24668 KB Output is correct
16 Correct 11 ms 24156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24156 KB Output is correct
2 Correct 14 ms 24156 KB Output is correct
3 Correct 17 ms 24156 KB Output is correct
4 Correct 12 ms 24156 KB Output is correct
5 Correct 24 ms 28508 KB Output is correct
6 Correct 22 ms 28732 KB Output is correct
7 Correct 24 ms 29020 KB Output is correct
8 Correct 20 ms 29016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 24152 KB Output is correct
2 Correct 10 ms 24152 KB Output is correct
3 Correct 10 ms 24156 KB Output is correct
4 Correct 15 ms 24236 KB Output is correct
5 Correct 15 ms 24276 KB Output is correct
6 Correct 16 ms 24152 KB Output is correct
7 Correct 11 ms 24152 KB Output is correct
8 Correct 11 ms 24156 KB Output is correct
9 Correct 12 ms 24804 KB Output is correct
10 Correct 13 ms 24920 KB Output is correct
11 Correct 12 ms 24668 KB Output is correct
12 Correct 15 ms 24668 KB Output is correct
13 Correct 12 ms 24832 KB Output is correct
14 Correct 32 ms 28508 KB Output is correct
15 Correct 30 ms 28768 KB Output is correct
16 Correct 30 ms 29020 KB Output is correct
17 Correct 32 ms 28764 KB Output is correct
18 Correct 28 ms 29492 KB Output is correct
19 Correct 30 ms 30540 KB Output is correct
20 Correct 46 ms 30616 KB Output is correct
21 Correct 24 ms 28764 KB Output is correct
22 Correct 33 ms 28500 KB Output is correct
23 Correct 28 ms 28764 KB Output is correct
24 Correct 30 ms 30556 KB Output is correct
25 Correct 11 ms 24156 KB Output is correct
26 Correct 13 ms 24704 KB Output is correct
27 Correct 14 ms 24156 KB Output is correct
28 Correct 22 ms 29020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 24200 KB Output is correct
2 Correct 12 ms 24048 KB Output is correct
3 Correct 11 ms 24156 KB Output is correct
4 Correct 12 ms 24152 KB Output is correct
5 Correct 13 ms 24168 KB Output is correct
6 Correct 11 ms 24044 KB Output is correct
7 Correct 17 ms 24156 KB Output is correct
8 Correct 11 ms 24156 KB Output is correct
9 Correct 13 ms 24924 KB Output is correct
10 Correct 12 ms 24872 KB Output is correct
11 Correct 17 ms 24668 KB Output is correct
12 Correct 11 ms 24756 KB Output is correct
13 Correct 16 ms 24732 KB Output is correct
14 Correct 25 ms 28508 KB Output is correct
15 Correct 26 ms 28760 KB Output is correct
16 Correct 37 ms 29000 KB Output is correct
17 Correct 27 ms 28764 KB Output is correct
18 Correct 30 ms 29532 KB Output is correct
19 Correct 31 ms 30608 KB Output is correct
20 Correct 29 ms 30556 KB Output is correct
21 Correct 25 ms 28764 KB Output is correct
22 Correct 27 ms 28564 KB Output is correct
23 Correct 33 ms 28732 KB Output is correct
24 Correct 516 ms 115344 KB Output is correct
25 Correct 769 ms 152968 KB Output is correct
26 Correct 723 ms 152456 KB Output is correct
27 Correct 756 ms 152400 KB Output is correct
28 Correct 507 ms 101940 KB Output is correct
29 Correct 515 ms 103584 KB Output is correct
30 Correct 567 ms 105216 KB Output is correct
31 Correct 31 ms 30420 KB Output is correct
32 Correct 740 ms 152336 KB Output is correct
33 Correct 11 ms 24408 KB Output is correct
34 Correct 13 ms 24676 KB Output is correct
35 Correct 522 ms 124576 KB Output is correct
36 Correct 12 ms 24152 KB Output is correct
37 Correct 24 ms 29044 KB Output is correct
38 Correct 456 ms 114132 KB Output is correct
39 Correct 371 ms 93648 KB Output is correct