Submission #18334705


Source Code Expand

#include "bits/stdc++.h"
using namespace std;
// Hi ☻
int main(){
    int n,m;
    scanf("%d%d",&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];
    vector<int> best(27,(int)1e9+7);
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            scanf(" %c",&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});
            }
        }
    }
    vector<pair<int,int>> dir = {{0,1},{1,0},{-1,0},{0,-1}};
    auto valid = [&](int i,int j) -> bool {
        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;
        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])){
            if(best[(gr[p.first][p.second]-'a')] > d+1){
                best[(gr[p.first][p.second]-'a')] = d+1;
                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});
                    }
                }
            }
        }
        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}});
                }
            }
        }
    }
    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 500
Code Size 2497 Byte
Status AC
Exec Time 852 ms
Memory 30872 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:6:10: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
    6 |     scanf("%d%d",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~
./Main.cpp:15:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   15 |             scanf(" %c",&gr[i][j]);
      |             ~~~~~^~~~~~~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 38
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 7 ms 3620 KiB
random_01.txt AC 2 ms 3568 KiB
random_02.txt AC 3 ms 3612 KiB
random_03.txt AC 2 ms 3676 KiB
random_04.txt AC 2 ms 3672 KiB
random_05.txt AC 2 ms 3676 KiB
random_06.txt AC 3 ms 3676 KiB
random_07.txt AC 2 ms 3632 KiB
random_08.txt AC 3 ms 3608 KiB
random_09.txt AC 2 ms 3616 KiB
random_10.txt AC 3 ms 3460 KiB
random_11.txt AC 3 ms 3696 KiB
random_12.txt AC 2 ms 3612 KiB
random_13.txt AC 2 ms 3664 KiB
random_14.txt AC 2 ms 3624 KiB
random_15.txt AC 5 ms 3616 KiB
random_16.txt AC 46 ms 5176 KiB
random_17.txt AC 254 ms 15012 KiB
random_18.txt AC 187 ms 10048 KiB
random_19.txt AC 39 ms 4856 KiB
random_20.txt AC 325 ms 14888 KiB
random_21.txt AC 10 ms 3932 KiB
random_22.txt AC 16 ms 4064 KiB
random_23.txt AC 123 ms 10984 KiB
random_24.txt AC 169 ms 15484 KiB
random_25.txt AC 49 ms 5280 KiB
random_26.txt AC 5 ms 3800 KiB
random_27.txt AC 220 ms 10972 KiB
random_28.txt AC 14 ms 4008 KiB
random_29.txt AC 5 ms 3724 KiB
random_30.txt AC 89 ms 14488 KiB
random_31.txt AC 852 ms 26744 KiB
random_32.txt AC 827 ms 30872 KiB
random_33.txt AC 581 ms 23384 KiB
random_34.txt AC 219 ms 23412 KiB
sample_01.txt AC 2 ms 3608 KiB
sample_02.txt AC 3 ms 3672 KiB
sample_03.txt AC 2 ms 3460 KiB