Submission #18365487


Source Code Expand

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define IOS std::ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pb push_back
using namespace std;

// DEBUG TEMPLATE
void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}

void OFFLINE(){
    #ifndef ONLINE_JUDGE 
        freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
        #define debug(x...) cout << "[" << #x << "] = ["; _print(x)
        #else
            #define debug(x...)
    #endif
}

const int MOD = 1e9 + 7;
vector<vector<char>> grid(2001, vector<char>(2001));
vector<vector<int>> dis(2001, vector<int>(2001, INT_MAX));
vector<vector<pair<int, int>>> edges(26);
vector<pair<int, int>> dirs = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};

int r, c;
bool isValid(int x, int y) {
    return ((x > 0 && x <= r && y > 0 && y <= c) && grid[x][y] != '#' && grid[x][y] != '*');
}

int bfs(pair<int, int> src, pair<int, int> des) {

    queue<pair<int, int>> q;
    q.push(src);
    dis[src.F][src.S] = 0;
    grid[src.F][src.S] = '*';

    while(!q.empty()) {
        
        int cx = q.front().F;
        int cy = q.front().S;
        q.pop();

        char node = grid[cx][cy];
        grid[cx][cy] = '*';

        if(isalpha(node) && islower(node)) {
            for(auto x : edges[node - 'a']) {
                q.push(x);
                dis[x.F][x.S] = min(dis[x.F][x.S], dis[cx][cy] + 1);
                grid[x.F][x.S] = '*';
            }
        }

        for(int i = 0; i < 4; i++) {
            int nx = cx + dirs[i].F, ny = cy + dirs[i].S;
            if(isValid(nx, ny)) {
                q.push({nx, ny});
                dis[nx][ny] = min(dis[nx][ny], dis[cx][cy] + 1);
            }
        }
    }

    return dis[des.F][des.S];
}

int main(){

    IOS;
    OFFLINE();
    //check for corner test cases

    cin >> r >> c;
    pair<int, int> src, des;
    
    for(int i = 1; i <= r; i++) {
        for(int j = 1; j <= c; j++) {
            char node;
            cin >> node;
            grid[i][j] = node;
            if(node == 'S') {
                src.F = i, src.S = j;
            } else if(node == 'G') {
                des.F = i, des.S = j;
            } else if(isalpha(node)) {
                edges[node - 'a'].pb({i, j});
            }
        }
    }

    int ans = bfs(src, des);
    
    cout << ((ans == INT_MAX) ? -1 : ans) << "\n";

    //check for corner test cases
    return 0;
}

Submission Info

Submission Time
Task E - Third Avenue
User shrii_06
Language C++ (GCC 9.2.1)
Score 0
Code Size 3580 Byte
Status TLE
Exec Time 3343 ms
Memory 1090196 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 3
AC × 31
TLE × 7
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 22 ms 23228 KiB
random_01.txt AC 25 ms 23220 KiB
random_02.txt AC 21 ms 23168 KiB
random_03.txt AC 19 ms 23252 KiB
random_04.txt AC 21 ms 23172 KiB
random_05.txt AC 18 ms 23264 KiB
random_06.txt AC 17 ms 23276 KiB
random_07.txt AC 22 ms 23248 KiB
random_08.txt AC 21 ms 23224 KiB
random_09.txt AC 18 ms 23180 KiB
random_10.txt AC 20 ms 23260 KiB
random_11.txt AC 21 ms 23196 KiB
random_12.txt AC 17 ms 23196 KiB
random_13.txt AC 21 ms 23180 KiB
random_14.txt AC 18 ms 23256 KiB
random_15.txt AC 17 ms 23220 KiB
random_16.txt AC 690 ms 43712 KiB
random_17.txt AC 308 ms 27340 KiB
random_18.txt AC 172 ms 27020 KiB
random_19.txt AC 298 ms 31852 KiB
random_20.txt TLE 3312 ms 105324 KiB
random_21.txt AC 31 ms 23488 KiB
random_22.txt AC 34 ms 23408 KiB
random_23.txt AC 68 ms 27900 KiB
random_24.txt AC 80 ms 29256 KiB
random_25.txt TLE 3326 ms 526004 KiB
random_26.txt TLE 3318 ms 254552 KiB
random_27.txt TLE 3335 ms 865568 KiB
random_28.txt TLE 3328 ms 570828 KiB
random_29.txt AC 21 ms 23256 KiB
random_30.txt AC 47 ms 28976 KiB
random_31.txt TLE 3315 ms 192336 KiB
random_32.txt AC 590 ms 35244 KiB
random_33.txt TLE 3343 ms 1090196 KiB
random_34.txt AC 103 ms 23144 KiB
sample_01.txt AC 20 ms 23072 KiB
sample_02.txt AC 17 ms 23264 KiB
sample_03.txt AC 19 ms 23256 KiB