Submission #71676009


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
const int maxn=1005;
string s[maxn];
const int x[4]={-1,0,1,0};
const int y[4]={0,1,0,-1};
int ans[maxn][maxn];
vector<pair<int,int>> loc[30];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int h,w;
    cin>>h>>w;
    for (int i=1;i<=h;i++)
    {
        cin>>s[i];
        s[i]=' '+s[i];
        for (int j=1;j<=w;j++)
        {
            if (s[i][j]>='a'&&s[i][j]<='z')
                loc[s[i][j]-'a'].push_back({i,j});
        }
    }
    deque<pair<int,int>> q;
    q.push_back({1,1});
    ans[1][1]=1;
    while(!q.empty())
    {
        auto now=q.front();
        q.pop_front();
        if (now.first==h&&now.second==w)
        {
            cout<<ans[h][w]-1;
            return 0;
        }
        if (now.first)
        {
            for (int i=0;i<4;i++)
            {
                pair<int,int> nxt={now.first+x[i],now.second+y[i]};
                if (nxt.first>0&&nxt.first<=h&&nxt.second>0&&nxt.second<=w&&!ans[nxt.first][nxt.second]&&s[nxt.first][nxt.second]!='#')
                {
                    ans[nxt.first][nxt.second]=ans[now.first][now.second]+1;
                    q.push_back(nxt);
                }
            }
            if (s[now.first][now.second]>='a'&&s[now.first][now.second]<='z'&&!ans[0][s[now.first][now.second]-'a'])
            {
                ans[0][s[now.first][now.second]-'a']=ans[now.first][now.second];
                q.push_front({0,s[now.first][now.second]-'a'});
            }
        }
        else
        {
            for (auto i:loc[now.second])
            {
                if (!ans[i.first][i.second])
                {
                    ans[i.first][i.second]=ans[now.first][now.second]+1;
                    q.push_back(i);
                }
            }
        }
    }
    cout<<-1;
}

Submission Info

Submission Time
Task D - Teleport Maze
User Alliy666
Language C++23 (GCC 15.2.0)
Score 400
Code Size 1927 Byte
Status AC
Exec Time 34 ms
Memory 24344 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 51
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 02_handmade_00.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3548 KiB
00_sample_01.txt AC 1 ms 3716 KiB
00_sample_02.txt AC 1 ms 3780 KiB
00_sample_03.txt AC 1 ms 3704 KiB
01_random_00.txt AC 1 ms 3780 KiB
01_random_01.txt AC 9 ms 9172 KiB
01_random_02.txt AC 15 ms 8772 KiB
01_random_03.txt AC 30 ms 23868 KiB
01_random_04.txt AC 18 ms 8868 KiB
01_random_05.txt AC 12 ms 11604 KiB
01_random_06.txt AC 22 ms 9300 KiB
01_random_07.txt AC 20 ms 8820 KiB
01_random_08.txt AC 1 ms 3736 KiB
01_random_09.txt AC 11 ms 6464 KiB
01_random_10.txt AC 22 ms 8568 KiB
01_random_11.txt AC 28 ms 20692 KiB
01_random_12.txt AC 25 ms 9156 KiB
01_random_13.txt AC 16 ms 8824 KiB
01_random_14.txt AC 34 ms 9348 KiB
01_random_15.txt AC 32 ms 8812 KiB
01_random_16.txt AC 1 ms 3672 KiB
01_random_17.txt AC 6 ms 6360 KiB
01_random_18.txt AC 3 ms 4616 KiB
01_random_19.txt AC 25 ms 17796 KiB
01_random_20.txt AC 14 ms 13444 KiB
01_random_21.txt AC 21 ms 14548 KiB
01_random_22.txt AC 4 ms 5204 KiB
01_random_23.txt AC 11 ms 11560 KiB
01_random_24.txt AC 1 ms 3716 KiB
01_random_25.txt AC 3 ms 5016 KiB
01_random_26.txt AC 3 ms 4760 KiB
01_random_27.txt AC 18 ms 14628 KiB
01_random_28.txt AC 23 ms 13808 KiB
01_random_29.txt AC 15 ms 11780 KiB
01_random_30.txt AC 15 ms 13292 KiB
01_random_31.txt AC 17 ms 13316 KiB
01_random_32.txt AC 1 ms 3720 KiB
01_random_33.txt AC 5 ms 6124 KiB
01_random_34.txt AC 3 ms 4728 KiB
01_random_35.txt AC 13 ms 11424 KiB
01_random_36.txt AC 4 ms 5128 KiB
01_random_37.txt AC 8 ms 9240 KiB
01_random_38.txt AC 14 ms 10656 KiB
01_random_39.txt AC 9 ms 10512 KiB
02_handmade_00.txt AC 15 ms 8696 KiB
02_handmade_01.txt AC 24 ms 24344 KiB
02_handmade_02.txt AC 3 ms 4804 KiB
02_handmade_03.txt AC 3 ms 4716 KiB
02_handmade_04.txt AC 9 ms 8556 KiB
02_handmade_05.txt AC 15 ms 8724 KiB
02_handmade_06.txt AC 29 ms 17040 KiB