Submission #64556386


Source Code Expand

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T, typename U>
using ordered_multiset = tree<pair<T, U>, null_type, less<pair<T, U>>, rb_tree_tag, tree_order_statistics_node_update>;

using ll = long long;

void solve()
{
    ll h, w;
    cin >> h >> w;

    vector<string> grid(h);

    for (ll i = 0; i < h; i++)
    {
        cin >> grid[i];
    }

    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    a--, b--, c--, d--;

    ll src = w * a + b, dest = w * c + d;

    vector<vector<vector<ll>>> dist(h * w, vector<vector<ll>>(4, vector<ll>(2, LLONG_MAX)));
    set<array<ll, 4>> q;

    for (ll i = 0; i < 4; i++)
    {
        dist[src][i][0] = 0;
        q.insert(array<ll, 4> {dist[src][i][0], i, 0, src});
    }

    vector<ll> dx = {-1, 0, 1, 0};
    vector<ll> dy = {0, 1, 0, -1};

    while (!q.empty())
    {
        auto node = *q.begin();
        ll dir = node[1], op = node[2], u = node[3];
        q.erase(q.begin());

        ll x = u / w, y = u % w;

        for (ll i = 0; i < 4; i++)
        {
            ll nx = x + dx[i];
            ll ny = y + dy[i];

            if (nx >= 0 && nx < h && ny >= 0 && ny < w)
            {
                ll v = w * nx + ny;
                ll ndir = i, nop = 0;

                if (op)
                {
                    if (dir != ndir)
                    {
                        if (grid[nx][ny] == '#')
                        {
                            nop = 1;
                        }
                    }
                }

                else
                {
                    if (grid[nx][ny] == '#')
                    {
                        nop = 1;
                    }
                }

                if (dist[u][dir][op] + nop < dist[v][ndir][nop])
                {
                    q.erase(array<ll, 4>{dist[v][ndir][nop], ndir, nop, v});
                    dist[v][ndir][nop] = dist[u][dir][op] + nop;
                    q.insert(array<ll, 4>{dist[v][ndir][nop], ndir, nop, v});
                }
            }
        }
    }

    ll ans = LLONG_MAX;

    for (ll i = 0; i < 4; i++)
    {
        for (ll j = 0; j < 2; j++)
        {
            ans = min(ans, dist[dest][i][j]);
        }
    }

    cout << ans << '\n';
}

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

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    ll t = 1;
    // cin >> t;

    while (t--)
    {
        solve();
    }

    return 0;
}

Submission Info

Submission Time
Task D - Takahashi the Wall Breaker
User astronom1cal
Language C++ 23 (gcc 12.2)
Score 0
Code Size 2811 Byte
Status TLE
Exec Time 2131 ms
Memory 417456 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 4
AC × 33
TLE × 1
Set Name Test Cases
Sample example_00.txt, example_01.txt, example_02.txt, example_03.txt
All example_00.txt, example_01.txt, example_02.txt, example_03.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3460 KiB
example_01.txt AC 1 ms 3440 KiB
example_02.txt AC 1 ms 3440 KiB
example_03.txt AC 1 ms 3648 KiB
hand_00.txt AC 1759 ms 262924 KiB
hand_01.txt AC 1972 ms 263692 KiB
hand_02.txt AC 1980 ms 263440 KiB
hand_03.txt AC 1738 ms 262776 KiB
hand_04.txt AC 1740 ms 263260 KiB
hand_05.txt AC 1422 ms 301612 KiB
hand_06.txt AC 1404 ms 301732 KiB
hand_07.txt AC 1658 ms 417456 KiB
hand_08.txt AC 1202 ms 340188 KiB
hand_09.txt AC 1 ms 3508 KiB
random_00.txt AC 1546 ms 248120 KiB
random_01.txt AC 1660 ms 243728 KiB
random_02.txt AC 1830 ms 254416 KiB
random_03.txt AC 1833 ms 252484 KiB
random_04.txt AC 1706 ms 238052 KiB
random_05.txt AC 1684 ms 239668 KiB
random_06.txt AC 1945 ms 359192 KiB
random_07.txt AC 1834 ms 355764 KiB
random_08.txt AC 1798 ms 341308 KiB
random_09.txt AC 1969 ms 352228 KiB
random_10.txt AC 1779 ms 333848 KiB
random_11.txt TLE 2131 ms 356880 KiB
random_12.txt AC 1753 ms 253420 KiB
random_13.txt AC 1687 ms 251424 KiB
random_14.txt AC 1669 ms 245676 KiB
random_15.txt AC 1771 ms 258512 KiB
random_16.txt AC 1639 ms 252844 KiB
random_17.txt AC 1766 ms 247728 KiB
random_18.txt AC 1830 ms 249724 KiB
random_19.txt AC 1836 ms 253620 KiB