Submission #68752557


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

using LL = long long;
#define endl '\n'
using db = double;
template <class T>
using max_heap = priority_queue<T>;
template <class T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;

const int inf = 1e9;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<string> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];

    int sx, sy, tx, ty;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            if (a[i][j] == 'S')
                sx = i, sy = j;
            else if (a[i][j] == 'G')
                tx = i, ty = j;

    vector dis(n, vector(m, vector<int>(2, inf)));
    vector vis(n, vector(m, vector<int>(2)));

    dis[sx][sy][0] = 0;
    min_heap<array<int, 4>> q;
    q.push({0, sx, sy, 0});

    while (!q.empty())
    {
        auto [_, x, y, t] = q.top();
        q.pop();
        if (vis[x][y][t])
            continue;
        vis[x][y][t] = 1;
        for (int i = 0; i < 4; ++i)
        {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if (xx >= 0 && xx < n && yy >= 0 && yy < m)
            {
                if (a[xx][yy] != '#')
                {
                    int tar = t;
                    if (a[xx][yy] == '?')
                        tar ^= 1;
                    int ok = 1;
                    if (tar == 0 && a[xx][yy] == 'x')
                        ok = 0;
                    if (tar == 1 && a[xx][yy] == 'o')
                        ok = 0;
                    if (ok == 0)
                        continue;
                    if (dis[xx][yy][tar] > dis[x][y][t] + 1)
                    {
                        dis[xx][yy][tar] = dis[x][y][t] + 1;
                        q.push({dis[xx][yy][tar], xx, yy, tar});
                    }
                }
            }
        }
    }
    int ans = min(dis[tx][ty][0], dis[tx][ty][1]);
    if (ans == inf)
        ans = -1;
    cout << ans << endl;

    return 0;
}

Submission Info

Submission Time
Task D - Toggle Maze
User hialine
Language C++ 20 (gcc 12.2)
Score 400
Code Size 2164 Byte
Status AC
Exec Time 98 ms
Memory 31332 KiB

Compile Error

Main.cpp: In function ‘int main()’:
Main.cpp:75:45: warning: ‘ty’ may be used uninitialized [-Wmaybe-uninitialized]
   75 |     int ans = min(dis[tx][ty][0], dis[tx][ty][1]);
      |                                             ^
Main.cpp:26:21: note: ‘ty’ was declared here
   26 |     int sx, sy, tx, ty;
      |                     ^~
Main.cpp:75:41: warning: ‘tx’ may be used uninitialized [-Wmaybe-uninitialized]
   75 |     int ans = min(dis[tx][ty][0], dis[tx][ty][1]);
      |                                         ^
Main.cpp:26:17: note: ‘tx’ was declared here
   26 |     int sx, sy, tx, ty;
      |                 ^~
Main.cpp:39:11: warning: ‘sy’ may be used uninitialized [-Wmaybe-uninitialized]
   39 |     q.push({0, sx, sy, 0});
      |     ~~~~~~^~~~~~~~~~~~~~~~
Main.cpp:26:13: note: ‘sy’ was declared here
   26 |     int sx, sy, tx, ty;
      |             ^~
Main.cpp:39:11: warning: ‘sx’ may be used uninitialized [-Wmaybe-uninitialized]
   39 |     q.push({0, sx, sy, 0});
      |     ~~~~~~^~~~~~~~~~~~~~~~
Main.cpp:26:9: note: ‘sx’ was declared here
   26 |     int sx, sy, tx, ty;
      |         ^~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 67
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_handmade_00.txt, 01_handmade_01.txt, 01_handmade_02.txt, 01_handmade_03.txt, 01_handmade_04.txt, 01_handmade_05.txt, 01_handmade_06.txt, 01_handmade_07.txt, 01_handmade_08.txt, 01_handmade_09.txt, 01_handmade_10.txt, 01_handmade_11.txt, 01_handmade_12.txt, 01_handmade_13.txt, 01_handmade_14.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt, 02_random_25.txt, 02_random_26.txt, 02_random_27.txt, 02_random_28.txt, 02_random_29.txt, 02_random_30.txt, 02_random_31.txt, 02_random_32.txt, 02_random_33.txt, 02_random_34.txt, 02_random_35.txt, 02_random_36.txt, 02_random_37.txt, 02_random_38.txt, 02_random_39.txt, 02_random_40.txt, 02_random_41.txt, 02_random_42.txt, 02_random_43.txt, 02_random_44.txt, 02_random_45.txt, 02_random_46.txt, 02_random_47.txt, 02_random_48.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3364 KiB
00_sample_01.txt AC 1 ms 3436 KiB
00_sample_02.txt AC 1 ms 3504 KiB
01_handmade_00.txt AC 73 ms 31196 KiB
01_handmade_01.txt AC 56 ms 31052 KiB
01_handmade_02.txt AC 39 ms 31136 KiB
01_handmade_03.txt AC 74 ms 31248 KiB
01_handmade_04.txt AC 1 ms 3508 KiB
01_handmade_05.txt AC 1 ms 3508 KiB
01_handmade_06.txt AC 1 ms 3476 KiB
01_handmade_07.txt AC 1 ms 3560 KiB
01_handmade_08.txt AC 1 ms 3428 KiB
01_handmade_09.txt AC 29 ms 30996 KiB
01_handmade_10.txt AC 30 ms 31092 KiB
01_handmade_11.txt AC 95 ms 31152 KiB
01_handmade_12.txt AC 84 ms 31208 KiB
01_handmade_13.txt AC 63 ms 31180 KiB
01_handmade_14.txt AC 55 ms 31204 KiB
02_random_00.txt AC 1 ms 3632 KiB
02_random_01.txt AC 48 ms 24012 KiB
02_random_02.txt AC 3 ms 4320 KiB
02_random_03.txt AC 56 ms 27444 KiB
02_random_04.txt AC 3 ms 6628 KiB
02_random_05.txt AC 64 ms 31224 KiB
02_random_06.txt AC 62 ms 26884 KiB
02_random_07.txt AC 69 ms 31212 KiB
02_random_08.txt AC 7 ms 10404 KiB
02_random_09.txt AC 69 ms 31212 KiB
02_random_10.txt AC 25 ms 31052 KiB
02_random_11.txt AC 63 ms 31144 KiB
02_random_12.txt AC 21 ms 31052 KiB
02_random_13.txt AC 21 ms 9444 KiB
02_random_14.txt AC 50 ms 25540 KiB
02_random_15.txt AC 3 ms 4148 KiB
02_random_16.txt AC 68 ms 31192 KiB
02_random_17.txt AC 2 ms 4512 KiB
02_random_18.txt AC 76 ms 31232 KiB
02_random_19.txt AC 4 ms 5340 KiB
02_random_20.txt AC 75 ms 31104 KiB
02_random_21.txt AC 1 ms 3600 KiB
02_random_22.txt AC 72 ms 31332 KiB
02_random_23.txt AC 2 ms 4524 KiB
02_random_24.txt AC 30 ms 22064 KiB
02_random_25.txt AC 9 ms 13088 KiB
02_random_26.txt AC 21 ms 14000 KiB
02_random_27.txt AC 8 ms 8196 KiB
02_random_28.txt AC 42 ms 31140 KiB
02_random_29.txt AC 15 ms 11120 KiB
02_random_30.txt AC 65 ms 31076 KiB
02_random_31.txt AC 18 ms 25452 KiB
02_random_32.txt AC 34 ms 31076 KiB
02_random_33.txt AC 25 ms 17048 KiB
02_random_34.txt AC 43 ms 31056 KiB
02_random_35.txt AC 12 ms 7672 KiB
02_random_36.txt AC 54 ms 19368 KiB
02_random_37.txt AC 29 ms 13384 KiB
02_random_38.txt AC 98 ms 31240 KiB
02_random_39.txt AC 73 ms 26732 KiB
02_random_40.txt AC 91 ms 31088 KiB
02_random_41.txt AC 1 ms 3768 KiB
02_random_42.txt AC 2 ms 3644 KiB
02_random_43.txt AC 1 ms 3640 KiB
02_random_44.txt AC 2 ms 3720 KiB
02_random_45.txt AC 2 ms 3596 KiB
02_random_46.txt AC 1 ms 3644 KiB
02_random_47.txt AC 2 ms 3648 KiB
02_random_48.txt AC 1 ms 3844 KiB