Submission #71676610


Source Code Expand

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
using P = pair<int, int>;

int h,w;
bool valid(int i, int j) {return 0 <= i && i < h && 0 <= j && j < w;}
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

int main(){
    cin >> h >> w;
    string S[h];
    for(int i = 0; i < h; i++) cin >> S[i];
    
    vector<bool> visited(26, false);
    vector<vector<P>> idx(26);
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if('a' <= S[i][j] && S[i][j] <= 'z'){
                idx[S[i][j]-'a'].push_back({i,j});
            }
        }
    }

    queue<P> que;
    vector<vector<int>> dist(h, vector<int>(w, -1));

    que.push({0,0});
    dist[0][0] = 0;

    while(!que.empty()){
        auto [x,y] = que.front();
        que.pop();

        for(int k = 0; k < 4; k++){
            int nx = x + dx[k];
            int ny = y + dy[k];
            if(valid(nx, ny) && S[nx][ny] != '#' && dist[nx][ny] == -1){
                que.push({nx, ny});
                dist[nx][ny] = dist[x][y] + 1;
            }
        }

        if('a' <= S[x][y] && S[x][y] <= 'z' && visited[S[x][y]] == false){
            for(auto [nx, ny] : idx[S[x][y]-'a']){
                if(dist[nx][ny] == -1){
                    que.push({nx, ny});
                    dist[nx][ny] = dist[x][y] + 1;
                }
                visited[S[x][y]] = true;
            }
        }
    }

    cout << dist[h-1][w-1] << endl;
}

Submission Info

Submission Time
Task D - Teleport Maze
User sakimori_coder
Language C++23 (GCC 15.2.0)
Score 400
Code Size 1501 Byte
Status AC
Exec Time 51 ms
Memory 25284 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 3384 KiB
00_sample_01.txt AC 1 ms 3512 KiB
00_sample_02.txt AC 1 ms 3516 KiB
00_sample_03.txt AC 1 ms 3460 KiB
01_random_00.txt AC 1 ms 3572 KiB
01_random_01.txt AC 20 ms 8212 KiB
01_random_02.txt AC 29 ms 9404 KiB
01_random_03.txt AC 47 ms 24620 KiB
01_random_04.txt AC 34 ms 9620 KiB
01_random_05.txt AC 46 ms 13720 KiB
01_random_06.txt AC 39 ms 10168 KiB
01_random_07.txt AC 33 ms 9616 KiB
01_random_08.txt AC 1 ms 3472 KiB
01_random_09.txt AC 14 ms 5520 KiB
01_random_10.txt AC 34 ms 9376 KiB
01_random_11.txt AC 47 ms 21604 KiB
01_random_12.txt AC 44 ms 9880 KiB
01_random_13.txt AC 43 ms 9788 KiB
01_random_14.txt AC 44 ms 10016 KiB
01_random_15.txt AC 41 ms 9636 KiB
01_random_16.txt AC 1 ms 3516 KiB
01_random_17.txt AC 7 ms 4556 KiB
01_random_18.txt AC 15 ms 9348 KiB
01_random_19.txt AC 48 ms 18784 KiB
01_random_20.txt AC 51 ms 14512 KiB
01_random_21.txt AC 50 ms 15356 KiB
01_random_22.txt AC 16 ms 9852 KiB
01_random_23.txt AC 48 ms 12704 KiB
01_random_24.txt AC 1 ms 3564 KiB
01_random_25.txt AC 4 ms 4336 KiB
01_random_26.txt AC 15 ms 9472 KiB
01_random_27.txt AC 38 ms 15368 KiB
01_random_28.txt AC 40 ms 14716 KiB
01_random_29.txt AC 38 ms 12856 KiB
01_random_30.txt AC 40 ms 14208 KiB
01_random_31.txt AC 39 ms 14264 KiB
01_random_32.txt AC 1 ms 3564 KiB
01_random_33.txt AC 9 ms 6252 KiB
01_random_34.txt AC 16 ms 9500 KiB
01_random_35.txt AC 27 ms 12296 KiB
01_random_36.txt AC 16 ms 9596 KiB
01_random_37.txt AC 19 ms 10016 KiB
01_random_38.txt AC 25 ms 11932 KiB
01_random_39.txt AC 24 ms 11524 KiB
02_handmade_00.txt AC 28 ms 9384 KiB
02_handmade_01.txt AC 36 ms 25284 KiB
02_handmade_02.txt AC 15 ms 9472 KiB
02_handmade_03.txt AC 15 ms 9396 KiB
02_handmade_04.txt AC 23 ms 9496 KiB
02_handmade_05.txt AC 30 ms 9416 KiB
02_handmade_06.txt AC 41 ms 17876 KiB