답안 #379596

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
379596 2021-03-18T18:47:45 Z Kubin 포탈들 (BOI14_portals) C++17
100 / 100
382 ms 53116 KB
#include <bits/stdc++.h>

using namespace std;

const size_t N = 1024;

template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);

    size_t h, w;
    cin >> h >> w;

    static array<array<bool, N>, N> lock = {};
    size_t sy = 0, sx = 0, fy = 0, fx = 0;

    for(size_t y = 0; y <= h+1; y++)
        for(size_t x = 0; x <= w+1; x++)
            lock[y][x] = true;
    for(size_t y = 1; y <= h; y++)
    {
        string row;
        cin >> row;
        for(size_t x = 1; x <= w; x++)
        {
            auto c = row[x-1];
            lock[y][x] = c == '#';
            if(c == 'S')
                sy = y, sx = x;
            if(c == 'C')
                fy = y, fx = x;
        }
    }

    queue<pair<size_t, size_t>> que;
    static array<array<int, N>, N> wall_dist = {};
    auto wall_maybe = [&](size_t y, size_t x, int d) {
        if(d < wall_dist[y][x])
            wall_dist[y][x] = d, que.emplace(y, x);
    };
    for(size_t y = 0; y <= h+1; y++)
      for(size_t x = 0; x <= w+1; x++)
    {
        wall_dist[y][x] = w*h;
        if(lock[y][x])
            wall_maybe(y, x, 0);
    }

    while(not que.empty())
    {
        auto [y, x] = que.front(); que.pop();
        auto d = wall_dist[y][x] + 1;
        if(y) wall_maybe(y - 1, x, d);
        if(x) wall_maybe(y, x - 1, d);
        if(y<=h) wall_maybe(y + 1, x, d);
        if(x<=w) wall_maybe(y, x + 1, d);
    }

    /*
    for(size_t y = 0; y <= h+1; y++, cout << endl)
        for(size_t x = 0; x <= w+1; x++, cout << ' ')
            cout << wall_dist[y][x];
    */

    static array<array<array<size_t, 4>, N>, N> towards;
    constexpr size_t UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3;
    for(size_t y = 0; y <= h+1; y++)
        for(size_t x = 0; x <= w+1; x++)
            towards[y][x].fill(SIZE_MAX);

    for(size_t y = 1; y <= h; y++)
        for(size_t x = 1; x <= w; x++)
            if(not lock[y][x])
                towards[y][x][UP] = towards[y-1][x][UP] == SIZE_MAX ? y : towards[y-1][x][UP],
                towards[y][x][LEFT] = towards[y][x-1][LEFT] == SIZE_MAX ? x : towards[y][x-1][LEFT];
    for(size_t y = h+1; y --> 1; )
        for(size_t x = w+1; x --> 1; )
            if(not lock[y][x])
                towards[y][x][DOWN] = towards[y+1][x][DOWN] == SIZE_MAX ? y : towards[y+1][x][DOWN],
                towards[y][x][RIGHT] = towards[y][x+1][RIGHT] == SIZE_MAX ? x : towards[y][x+1][RIGHT];
    /*
    cout << "UP" << endl;
    for(size_t y = 0; y <= h+1; y++, cout << endl)
        for(size_t x = 0; x <= w+1; x++, cout << ' ')
            cout << (towards[y][x][UP]==SIZE_MAX ? "#" : to_string(towards[y][x][UP]));
    cout << "LEFT" << endl;
    for(size_t y = 0; y <= h+1; y++, cout << endl)
        for(size_t x = 0; x <= w+1; x++, cout << ' ')
            cout << (towards[y][x][LEFT]==SIZE_MAX ? "#" : to_string(towards[y][x][LEFT]));
    cout << "DOWN" << endl;
    for(size_t y = 0; y <= h+1; y++, cout << endl)
        for(size_t x = 0; x <= w+1; x++, cout << ' ')
            cout << (towards[y][x][DOWN]==SIZE_MAX ? "#" : to_string(towards[y][x][DOWN]));
    cout << "RIGHT" << endl;
    for(size_t y = 0; y <= h+1; y++, cout << endl)
        for(size_t x = 0; x <= w+1; x++, cout << ' ')
            cout << (towards[y][x][RIGHT]==SIZE_MAX ? "#" : to_string(towards[y][x][RIGHT]));
    */

    static array<array<int, N>, N> dist = {};
    for(size_t y = 0; y <= h+1; y++)
        for(size_t x = 0; x <= w+1; x++)
            dist[y][x] = w*h + lock[y][x];

    min_heap<tuple<int, size_t, size_t>> heap;
    static array<array<bool, N>, N> vis = {};
    auto maybe = [&](size_t y, size_t x, int d) {
        if(not lock[y][x] and d < dist[y][x])
            heap.emplace(dist[y][x] = d, y, x);
    };
    maybe(sy, sx, 0);
    while(not heap.empty())
    {
        auto [_, y, x] = heap.top(); heap.pop();
        if(vis[y][x])
            continue;
        vis[y][x] = true;
        auto d = dist[y][x], c = wall_dist[y][x];
        // cout << y << ", " << x << ": " << d << "/" << c << endl;
        maybe(y - 1, x, d + 1); maybe(y, x - 1, d + 1);
        maybe(y + 1, x, d + 1); maybe(y, x + 1, d + 1);
        maybe(towards[y][x][UP], x, d + c);
        maybe(towards[y][x][DOWN], x, d + c);
        maybe(y, towards[y][x][LEFT], d + c);
        maybe(y, towards[y][x][RIGHT], d + c);
    }
    /*
    for(size_t y = 0; y <= h+1; y++, cout << endl)
        for(size_t x = 0; x <= w+1; x++, cout << ' ')
            cout << (dist[y][x]>=w*h ? "#" : to_string(dist[y][x]));
    */

    cout << dist[fy][fx] << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 1132 KB Output is correct
10 Correct 1 ms 1132 KB Output is correct
11 Correct 1 ms 1144 KB Output is correct
12 Correct 1 ms 1132 KB Output is correct
13 Correct 1 ms 1132 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 2 ms 1168 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 640 KB Output is correct
5 Correct 7 ms 4332 KB Output is correct
6 Correct 8 ms 4460 KB Output is correct
7 Correct 9 ms 4460 KB Output is correct
8 Correct 5 ms 4460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 1132 KB Output is correct
10 Correct 2 ms 1132 KB Output is correct
11 Correct 1 ms 1132 KB Output is correct
12 Correct 1 ms 1132 KB Output is correct
13 Correct 2 ms 1132 KB Output is correct
14 Correct 9 ms 4460 KB Output is correct
15 Correct 8 ms 4460 KB Output is correct
16 Correct 9 ms 4460 KB Output is correct
17 Correct 8 ms 4332 KB Output is correct
18 Correct 10 ms 4332 KB Output is correct
19 Correct 11 ms 4076 KB Output is correct
20 Correct 10 ms 4076 KB Output is correct
21 Correct 7 ms 4460 KB Output is correct
22 Correct 8 ms 4460 KB Output is correct
23 Correct 11 ms 4460 KB Output is correct
24 Correct 11 ms 4204 KB Output is correct
25 Correct 1 ms 492 KB Output is correct
26 Correct 1 ms 1132 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
28 Correct 5 ms 4480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 524 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 2 ms 1132 KB Output is correct
10 Correct 1 ms 1132 KB Output is correct
11 Correct 1 ms 1132 KB Output is correct
12 Correct 1 ms 1132 KB Output is correct
13 Correct 1 ms 1132 KB Output is correct
14 Correct 7 ms 4460 KB Output is correct
15 Correct 8 ms 4460 KB Output is correct
16 Correct 14 ms 4588 KB Output is correct
17 Correct 8 ms 4332 KB Output is correct
18 Correct 12 ms 4332 KB Output is correct
19 Correct 11 ms 4076 KB Output is correct
20 Correct 10 ms 4076 KB Output is correct
21 Correct 7 ms 4460 KB Output is correct
22 Correct 7 ms 4460 KB Output is correct
23 Correct 8 ms 4460 KB Output is correct
24 Correct 213 ms 49064 KB Output is correct
25 Correct 382 ms 44336 KB Output is correct
26 Correct 300 ms 44112 KB Output is correct
27 Correct 314 ms 43868 KB Output is correct
28 Correct 153 ms 50984 KB Output is correct
29 Correct 168 ms 53116 KB Output is correct
30 Correct 214 ms 50400 KB Output is correct
31 Correct 10 ms 4076 KB Output is correct
32 Correct 273 ms 43628 KB Output is correct
33 Correct 1 ms 492 KB Output is correct
34 Correct 1 ms 1132 KB Output is correct
35 Correct 217 ms 47724 KB Output is correct
36 Correct 1 ms 492 KB Output is correct
37 Correct 5 ms 4460 KB Output is correct
38 Correct 103 ms 49300 KB Output is correct
39 Correct 150 ms 52068 KB Output is correct