Submission #18322582


Source Code Expand

#include "bits/stdc++.h"
using namespace std;
// Hi ☻
int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int n,m;
    cin>>n>>m;
    vector<vector<bool>> vis(n,vector<bool>(m,0));
    vector<vector<char>> gr(n,vector<char>(m,0));
    vector<vector<int>> dist(n,vector<int>(m,(int)1e9+7));
    pair<int,int> start,end;
    vector<pair<int,int>> mp[27];
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>gr[i][j];
            if(gr[i][j] == 'S'){
                start.first = i;
                start.second = j;
            }else if(gr[i][j] == 'G'){
                end.first = i;
                end.second = j;
            }else if(isalpha(gr[i][j]) && islower(gr[i][j])){
                mp[(gr[i][j] - 'a')].push_back({i,j});
            }
        }
    }//cout<<"out"<<endl;
    vector<pair<int,int>> dir = {{0,1},{1,0},{-1,0},{0,-1}};
    auto valid = [&](int i,int j){
        if(i < 0 || j < 0 || i >= n || j >= m)return false;
        if(gr[i][j] == '#')return false;
        return true;
    };
    priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> pq;
    pq.push({0,start});
    dist[start.first][start.second] = 0;
    while(!pq.empty()){
        int d = pq.top().first;
        pair<int,int> p= pq.top().second;
        //cout<<p.first<<" "<<p.second<<endl;
        pq.pop();
        if(vis[p.first][p.second])continue;
        vis[p.first][p.second] = 1;
        if(isalpha(gr[p.first][p.second]) && islower(gr[p.first][p.second])){ // try teleporters if possible
            for(auto c:mp[(gr[p.first][p.second]-'a')]){
                if(c.first == p.first && c.second == p.second)continue;
                if(dist[c.first][c.second] > d+1){
                    dist[c.first][c.second] = d+1;
                    pq.push({dist[c.first][c.second],c});
                }
            }
        }
        // try walking
        for(auto dd:dir){
            int nx = p.first+dd.first;
            int ny = p.second+dd.second;
            if(valid(nx,ny)){
                if(dist[nx][ny] > d+1){
                    dist[nx][ny] = d+1;
                    pq.push({d+1,{nx,ny}});
                }
            }
        }
    }/*
    for(auto i:dist){
        for(auto j:i)cout<<j;
        cout<<endl;
    }*/
    if(dist[end.first][end.second] == (int)1e9+7){
        cout<<-1<<'\n';
    }else cout<<dist[end.first][end.second]<<'\n';
    return 0;
}

Submission Info

Submission Time
Task E - Third Avenue
User AmineTrabelsi
Language C++ (GCC 9.2.1)
Score 0
Code Size 2521 Byte
Status TLE
Exec Time 3308 ms
Memory 30960 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 3
AC × 35
TLE × 3
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.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, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 8 ms 3548 KiB
random_01.txt AC 2 ms 3508 KiB
random_02.txt AC 2 ms 3616 KiB
random_03.txt AC 2 ms 3576 KiB
random_04.txt AC 2 ms 3568 KiB
random_05.txt AC 2 ms 3620 KiB
random_06.txt AC 2 ms 3568 KiB
random_07.txt AC 3 ms 3596 KiB
random_08.txt AC 2 ms 3576 KiB
random_09.txt AC 2 ms 3664 KiB
random_10.txt AC 2 ms 3576 KiB
random_11.txt AC 2 ms 3612 KiB
random_12.txt AC 2 ms 3612 KiB
random_13.txt AC 2 ms 3664 KiB
random_14.txt AC 2 ms 3608 KiB
random_15.txt AC 3 ms 3684 KiB
random_16.txt AC 36 ms 5016 KiB
random_17.txt AC 322 ms 15224 KiB
random_18.txt AC 235 ms 9952 KiB
random_19.txt AC 32 ms 4808 KiB
random_20.txt AC 292 ms 15048 KiB
random_21.txt AC 8 ms 3876 KiB
random_22.txt AC 13 ms 4020 KiB
random_23.txt TLE 3308 ms 11080 KiB
random_24.txt TLE 3308 ms 12404 KiB
random_25.txt AC 39 ms 5192 KiB
random_26.txt AC 4 ms 3644 KiB
random_27.txt AC 183 ms 10472 KiB
random_28.txt AC 12 ms 3904 KiB
random_29.txt AC 6 ms 3680 KiB
random_30.txt TLE 3308 ms 14544 KiB
random_31.txt AC 1102 ms 26936 KiB
random_32.txt AC 2984 ms 30960 KiB
random_33.txt AC 470 ms 23432 KiB
random_34.txt AC 106 ms 23460 KiB
sample_01.txt AC 2 ms 3624 KiB
sample_02.txt AC 2 ms 3620 KiB
sample_03.txt AC 3 ms 3576 KiB