Submission #71680395


Source Code Expand

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <cassert>
#include <functional>
#include <chrono>
#include <random>


using namespace std;
#define ll long long
#define MOD 998244353
#define ld long double
#define INF 2251799813685248
#define vall(A) A.begin(),A.end()
#define gridinput(vv,H,W) for (ll i = 0; i < H; i++){string T; cin >> T; for(ll j = 0; j < W; j++){vv[i][j] = {T[j]};}}
#define adjustedgridinput(vv,H,W) for (ll i = 1; i <= H; i++){string T; cin >> T; for(ll j = 1; j <= W; j++){vv[i][j] = {T[j-1]};}}
#define vin(A) for (ll i = 0, sz = A.size(); i < sz; i++){cin >> A[i];}
#define vout(A) for(ll i = 0, sz = A.size(); i < sz; i++){cout << A[i] << " \n"[i == sz-1];}
#define adjustedvin(A) for (ll i = 1, sz = A.size(); i < sz; i++){cin >> A[i];}
#define adjustedvout(A) for(ll i = 1, sz = A.size(); i < sz; i++){cout << A[i] << " \n"[i == sz-1];}
#define vout2d(A,H,W) for (ll i = 0; i < H; i++){for (ll j = 0; j < W; j++){cout << A[i][j] << " \n"[j==W-1];}}
#define encode(i,j) ((i)<<32)+j
#define decode(v,w) (w ? (v)%4294967296 : (v)>>32)
vector<ll> pow2ll{1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,2147483648,4294967296};
vector<ll> pow10ll{1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000,1000000000000,10000000000000,100000000000000,1000000000000000,10000000000000000,100000000000000000,1000000000000000000};
vector<ll> di{0,1,0,-1};
vector<ll> dj{1,0,-1,0};




/// @brief a^bをmで割った余りを返す。bに関して対数時間で計算できる。
/// @param a 
/// @param b 
/// @param m 
/// @return a^b%m
ll modpow(ll a, ll b, ll m){
    ll t = a%m;
    ll ans = 1;
    while (b > 0){
        if (b%2){
            ans = (ans*t)%m;
        }
        b /= 2;
        t = (t*t)%m;
    }
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    ll H,W;
    cin >> H >> W;

    vector<vector<string>> A(H+2,vector<string>(W+2,"#"));
    vector<set<pair<ll,ll>>> tel(26);
    vector<bool> tel_used(26,0);

    for (ll i = 1; i <= H; i++){
        string T;
        cin >> T;
        for (ll j = 1; j <= W; j++){
            A[i][j] = T[j-1];
            if (A[i][j] != "." && A[i][j] != "#"){
                tel[A[i][j][0]-97].insert({i,j});
            }
        }
    }

    queue<pair<ll,ll>> Q;
    vector<vector<ll>> dist(H+2,vector<ll>(W+2, -1));
    dist[1][1] = 0;
    Q.push({1,1});

    while (!Q.empty()){
        ll i = Q.front().first;
        ll j = Q.front().second;

        Q.pop();

        if (A[i][j] == "."){
            for (ll k = 0; k < 4; k++){
                if (A[i+di[k]][j+dj[k]] != "#" && dist[i+di[k]][j+dj[k]] == -1){
                    dist[i+di[k]][j+dj[k]] = dist[i][j]+1;
                    Q.push({i+di[k], j+dj[k]});
                }
            }
            continue;
        }
        else{
            for (ll k = 0; k < 4; k++){//まず歩いてみる
                if (A[i+di[k]][j+dj[k]] != "#" && dist[i+di[k]][j+dj[k]] == -1){
                    dist[i+di[k]][j+dj[k]] = dist[i][j]+1;
                    Q.push({i+di[k], j+dj[k]});
                }
            }
            if (tel_used[A[i][j][0]-'a']){//もし使用済みなら
                continue;
            }
            else{
                tel_used[A[i][j][0]-'a'] = 1;
                for (auto v : tel[A[i][j][0]-'a']){
                    if (v == make_pair(i,j)){continue;}
                    if (dist[v.first][v.second] != -1){continue;}
                    dist[v.first][v.second] = dist[i][j] + 1;
                    Q.push(v);
                }
            }
        }
    }

    cout << dist[H][W] << "\n";

}

Submission Info

Submission Time
Task D - Teleport Maze
User a1073741824
Language C++23 (GCC 15.2.0)
Score 400
Code Size 4101 Byte
Status AC
Exec Time 549 ms
Memory 121556 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 3444 KiB
00_sample_01.txt AC 1 ms 3596 KiB
00_sample_02.txt AC 1 ms 3448 KiB
00_sample_03.txt AC 1 ms 3592 KiB
01_random_00.txt AC 1 ms 3716 KiB
01_random_01.txt AC 51 ms 27348 KiB
01_random_02.txt AC 63 ms 43072 KiB
01_random_03.txt AC 549 ms 120232 KiB
01_random_04.txt AC 63 ms 43252 KiB
01_random_05.txt AC 116 ms 55400 KiB
01_random_06.txt AC 69 ms 44708 KiB
01_random_07.txt AC 63 ms 43396 KiB
01_random_08.txt AC 1 ms 3768 KiB
01_random_09.txt AC 23 ms 17196 KiB
01_random_10.txt AC 65 ms 43052 KiB
01_random_11.txt AC 441 ms 104876 KiB
01_random_12.txt AC 78 ms 44000 KiB
01_random_13.txt AC 74 ms 43680 KiB
01_random_14.txt AC 76 ms 44544 KiB
01_random_15.txt AC 71 ms 43432 KiB
01_random_16.txt AC 1 ms 3780 KiB
01_random_17.txt AC 11 ms 10308 KiB
01_random_18.txt AC 28 ms 42788 KiB
01_random_19.txt AC 331 ms 89172 KiB
01_random_20.txt AC 200 ms 65768 KiB
01_random_21.txt AC 227 ms 71892 KiB
01_random_22.txt AC 39 ms 46044 KiB
01_random_23.txt AC 145 ms 57620 KiB
01_random_24.txt AC 1 ms 3576 KiB
01_random_25.txt AC 9 ms 8668 KiB
01_random_26.txt AC 28 ms 42796 KiB
01_random_27.txt AC 217 ms 73684 KiB
01_random_28.txt AC 188 ms 67984 KiB
01_random_29.txt AC 138 ms 58840 KiB
01_random_30.txt AC 166 ms 64812 KiB
01_random_31.txt AC 178 ms 65368 KiB
01_random_32.txt AC 1 ms 3572 KiB
01_random_33.txt AC 22 ms 21816 KiB
01_random_34.txt AC 26 ms 42788 KiB
01_random_35.txt AC 111 ms 58068 KiB
01_random_36.txt AC 36 ms 45472 KiB
01_random_37.txt AC 38 ms 45176 KiB
01_random_38.txt AC 75 ms 52868 KiB
01_random_39.txt AC 76 ms 51984 KiB
02_handmade_00.txt AC 64 ms 43020 KiB
02_handmade_01.txt AC 256 ms 121556 KiB
02_handmade_02.txt AC 25 ms 42732 KiB
02_handmade_03.txt AC 23 ms 42796 KiB
02_handmade_04.txt AC 30 ms 42796 KiB
02_handmade_05.txt AC 53 ms 42728 KiB
02_handmade_06.txt AC 261 ms 105920 KiB