Submission #18331447


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define vi vector<ll>
#define pb push_back
#define F first
#define S second
#define all(v) (v).begin(),(v).end()
#define pii pair<ll,ll>
#define vii vector<pii>
#define MOD 1000000007LL
#define calc_fact(n) tgamma(n+1)

signed main()
{
    FIO;
    ll n,m,x2,y2;
    cin>>n>>m;
    char mat[n][m];
    map<ll,vii> adj;
    queue<pair<pii,ll>> q;
    vector<vector<bool>> vis(n,vector<bool>(m,false));
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            cin>>mat[i][j];
            if(mat[i][j]<='z' and mat[i][j]>='a')adj[mat[i][j]-'a'].pb({i,j});
            if(mat[i][j]=='S'){q.push({{i,j},0});vis[i][j]=true;}
            if(mat[i][j]=='G')x2=i,y2=j;
            if(mat[i][j]=='#')vis[i][j]=true;
        }
    }
    while(!q.empty())
    {
        ll x=q.front().F.F,y=q.front().F.S,lev=q.front().S;q.pop();
        if(x==x2 and y==y2){cout<<lev;exit(0);}
        if(mat[x][y]<='z' and mat[x][y]>='a' and adj.find({mat[x][y]-'a'})!=adj.end())
        {
            for(auto p:adj[mat[x][y]-'a'])
            {
                if(!vis[p.F][p.S])
                {
                    vis[p.F][p.S]=true;
                    q.push({{p.F,p.S},lev+1});
                }
            }
            adj.erase(mat[x][y]-'a');
        }
        if(x+1<n and !vis[x+1][y])
        {
            vis[x+1][y]=true;
            q.push({{x+1,y},lev+1});
        }
        if(x-1>=0 and !vis[x-1][y])
        {
            vis[x-1][y]=true;
            q.push({{x-1,y},lev+1});
        }
        if(y+1<m and !vis[x][y+1])
        {
            vis[x][y+1]=true;
            q.push({{x,y+1},lev+1});
        }
        if(y-1>=0 and !vis[x][y-1])
        {
            vis[x][y-1]=true;
            q.push({{x,y-1},lev+1});
        }
    }
    cout<<"-1";
}

Submission Info

Submission Time
Task E - Third Avenue
User sunny_saraff
Language C++ (GCC 9.2.1)
Score 500
Code Size 2002 Byte
Status AC
Exec Time 116 ms
Memory 17256 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:39:9: warning: ‘x2’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   39 |         if(x==x2 and y==y2){cout<<lev;exit(0);}
      |         ^~
./Main.cpp:39:18: warning: ‘y2’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   39 |         if(x==x2 and y==y2){cout<<lev;exit(0);}
      |            ~~~~~~^~~~~~~~~

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 3560 KiB
random_01.txt AC 2 ms 3556 KiB
random_02.txt AC 2 ms 3612 KiB
random_03.txt AC 2 ms 3552 KiB
random_04.txt AC 2 ms 3508 KiB
random_05.txt AC 2 ms 3564 KiB
random_06.txt AC 3 ms 3588 KiB
random_07.txt AC 2 ms 3532 KiB
random_08.txt AC 2 ms 3584 KiB
random_09.txt AC 1 ms 3612 KiB
random_10.txt AC 2 ms 3596 KiB
random_11.txt AC 2 ms 3560 KiB
random_12.txt AC 2 ms 3464 KiB
random_13.txt AC 2 ms 3596 KiB
random_14.txt AC 2 ms 3528 KiB
random_15.txt AC 2 ms 3620 KiB
random_16.txt AC 11 ms 4012 KiB
random_17.txt AC 60 ms 7672 KiB
random_18.txt AC 31 ms 6852 KiB
random_19.txt AC 8 ms 4012 KiB
random_20.txt AC 51 ms 7132 KiB
random_21.txt AC 4 ms 3652 KiB
random_22.txt AC 5 ms 3796 KiB
random_23.txt AC 34 ms 9696 KiB
random_24.txt AC 37 ms 12176 KiB
random_25.txt AC 14 ms 4040 KiB
random_26.txt AC 2 ms 3684 KiB
random_27.txt AC 34 ms 5260 KiB
random_28.txt AC 4 ms 3604 KiB
random_29.txt AC 2 ms 3616 KiB
random_30.txt AC 38 ms 11296 KiB
random_31.txt AC 100 ms 14148 KiB
random_32.txt AC 116 ms 17256 KiB
random_33.txt AC 102 ms 8184 KiB
random_34.txt AC 78 ms 8072 KiB
sample_01.txt AC 2 ms 3564 KiB
sample_02.txt AC 2 ms 3516 KiB
sample_03.txt AC 2 ms 3496 KiB