Submission #72574819


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vvii vector<vector<pair<int,int>>>
#define vvll vector<vector<pair<ll,ll>>>
#define vst vector<string>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end())
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=1005,INF=15<<26;

int dis[MAX][MAX];

vi dh={0,1,0,-1},dw={1,0,-1,0};

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int H,W;cin>>H>>W;
    vst S(H);
    for(int i=0;i<H;i++){
        cin>>S[i];
    }
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            dis[i][j]=INF;
        }
    }
    dis[0][0]=0;
    queue<pii> Q;
    Q.push(mp(0,0));
    while(!Q.empty()){
        auto [h,w]=Q.front();Q.pop();
        for(int k=0;k<4;k++){
            int toh=h+dh[k],tow=w+dw[k];
            if(toh<0||toh>=H||tow<0||tow>=W) continue;
            if(S[h][w]==S[toh][tow]) continue;
            if(chmin(dis[toh][tow],dis[h][w]+1)) Q.push(mp(toh,tow));
        }
    }
    
    if(dis[H-1][W-1]==INF) cout<<-1<<endl;
    else cout<<dis[H-1][W-1]<<endl;
}


Submission Info

Submission Time
Task F - EGFパス
User guild2026_099
Language C++ IOI-Style(GNU++20) (GCC 14.2.0)
Score 100
Code Size 1755 Byte
Status AC
Exec Time 40 ms
Memory 6860 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 0 ms 1740 KiB
00_sample_01.txt AC 0 ms 1740 KiB
00_sample_02.txt AC 0 ms 1740 KiB
01_random_00.txt AC 40 ms 6732 KiB
01_random_01.txt AC 29 ms 5452 KiB
01_random_02.txt AC 4 ms 5836 KiB
01_random_03.txt AC 12 ms 6092 KiB
01_random_04.txt AC 40 ms 6732 KiB
01_random_05.txt AC 40 ms 6732 KiB
01_random_06.txt AC 40 ms 6860 KiB
01_random_07.txt AC 3 ms 6732 KiB
01_random_08.txt AC 39 ms 6732 KiB
01_random_09.txt AC 3 ms 6732 KiB
01_random_10.txt AC 22 ms 6732 KiB
01_random_11.txt AC 23 ms 6732 KiB
01_random_12.txt AC 39 ms 6732 KiB
01_random_13.txt AC 39 ms 6860 KiB
01_random_14.txt AC 5 ms 4428 KiB
01_random_15.txt AC 19 ms 5708 KiB
01_random_16.txt AC 34 ms 6092 KiB
01_random_17.txt AC 30 ms 5452 KiB
01_random_18.txt AC 1 ms 1868 KiB
01_random_19.txt AC 39 ms 6860 KiB
01_random_20.txt AC 25 ms 6348 KiB
01_random_21.txt AC 7 ms 2636 KiB