Submission #72575118


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
#define rep(i, n) for(int i = 0; i < (int) (n); i ++)
#define rrep(i, m, n) for(int i = (int) m; i < (int) n; i ++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

long long gcd(long long a, long long b){
    if(a % b == 0) return b;
    else return gcd(b, a % b);
}

long long lcm(long long a, long long b){
    return a * b / gcd(a, b);
}

template<typename T>
void v_cin(vector<T> &x){
    rep(i, x.size()) cin >> x[i];
}

template<typename T>
void vv_cin(vector<vector<T>> &x){
    rep(i, x.size()) rep(j, x[i].size()) cin >> x[i][j];
}

template <typename T>
void v_cout(vector<T> &x){
    rep(i, x.size()) cout << x[i] << (i < x.size() - 1 ? " " : "\n");
}

template <typename T>
void vv_cout(vector<vector<T>> &x){
    rep(i, x.size()) v_cout(x[i]);
}

// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint998244353;
// using mint = modint1000000007;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int H, W; cin >> H >> W;
    vector<vector<char>> S(H, vector<char> (W));
    vv_cin(S);

    vector<vector<int>> dist(H, vector<int> (W, 1e9));

    queue<pair<int, int>> Q;
    dist[0][0] = 0;
    Q.push({0, 0});
    while(!Q.empty()){
        auto [x, y] = Q.front(); Q.pop();
        rep(d, 4){
            int nx = x + dx[d], ny = y + dy[d];
            if(nx < 0 or nx >= H or ny < 0 or ny >= W){
                continue;
            }
            if(S[x][y] == S[nx][ny]) continue;
            if(dist[nx][ny] < 1e9) continue;
            dist[nx][ny] = dist[x][y] + 1;
            Q.push({nx, ny});
        }
    }

    cout << (dist[H - 1][W - 1] >= 1e9 ? -1 : dist[H - 1][W - 1]) << "\n";
}

Submission Info

Submission Time
Task F - EGFパス
User guild2026_102
Language C++23 (GCC 15.2.0)
Score 100
Code Size 2022 Byte
Status AC
Exec Time 39 ms
Memory 8684 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 25
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.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
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3560 KiB
00_sample_01.txt AC 1 ms 3664 KiB
00_sample_02.txt AC 1 ms 3608 KiB
01_random_00.txt AC 39 ms 8612 KiB
01_random_01.txt AC 28 ms 7168 KiB
01_random_02.txt AC 4 ms 4144 KiB
01_random_03.txt AC 12 ms 5164 KiB
01_random_04.txt AC 39 ms 8596 KiB
01_random_05.txt AC 39 ms 8604 KiB
01_random_06.txt AC 38 ms 8604 KiB
01_random_07.txt AC 8 ms 8404 KiB
01_random_08.txt AC 39 ms 8492 KiB
01_random_09.txt AC 8 ms 8400 KiB
01_random_10.txt AC 23 ms 8284 KiB
01_random_11.txt AC 22 ms 8284 KiB
01_random_12.txt AC 39 ms 8680 KiB
01_random_13.txt AC 38 ms 8684 KiB
01_random_14.txt AC 5 ms 4304 KiB
01_random_15.txt AC 19 ms 6084 KiB
01_random_16.txt AC 34 ms 7960 KiB
01_random_17.txt AC 29 ms 7324 KiB
01_random_18.txt AC 2 ms 3676 KiB
01_random_19.txt AC 39 ms 8624 KiB
01_random_20.txt AC 25 ms 6828 KiB
01_random_21.txt AC 7 ms 4084 KiB